From 61d04fb2fbb1fd1e39963bbc9f57d00f273802fc Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:55:14 -0800 Subject: [PATCH 1/4] RedundantBraces: break documentation into sections --- docs/configuration.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index d6f2b7208a..3619becf19 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2417,7 +2417,7 @@ List(1, 2, 3).map { x => } ``` -#### Configuration options +#### `RedundantBraces`: `generalExpressions` ```scala mdoc:defaults rewrite.redundantBraces.generalExpressions @@ -2442,6 +2442,8 @@ str match { } ``` +#### `RedundantBraces`: `ifElseExpressions` + ```scala mdoc:defaults rewrite.redundantBraces.ifElseExpressions ``` @@ -2458,6 +2460,8 @@ if (a > b) { } ``` +#### `RedundantBraces`: `methodBodies` + ```scala mdoc:defaults rewrite.redundantBraces.methodBodies ``` @@ -2471,6 +2475,8 @@ def f() = { } ``` +#### `RedundantBraces`: `includeUnitMethods` + ```scala mdoc:defaults rewrite.redundantBraces.includeUnitMethods ``` @@ -2491,6 +2497,8 @@ def x(): Unit = { } ``` +#### `RedundantBraces`: `stringInterpolation` + ```scala mdoc:defaults rewrite.redundantBraces.stringInterpolation ``` @@ -2502,6 +2510,8 @@ rewrite.redundantBraces.stringInterpolation = true s"user id is ${id}" ``` +#### `RedundantBraces`: `parensForOneLineApply` + `rewrite.redundantBraces.parensForOneLineApply` is `true` by default for `edition` >= 2020-01. See also [newlines.afterCurlyLambdaParams = squash](#newlinesaftercurlylambdaparams). @@ -2513,6 +2523,8 @@ rewrite.redundantBraces.parensForOneLineApply = true xs.map { x => x + 1 } ``` +#### `RedundantBraces`: `maxLines` + ```scala mdoc:defaults rewrite.redundantBraces.maxLines ``` From 928f988be1fc9cfe7a0ac5c9b06ecffb76654296 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:57:14 -0800 Subject: [PATCH 2/4] RedundantBraces: parensForOneLineApply not Option --- docs/configuration.md | 8 +++++--- .../org/scalafmt/config/RedundantBracesSettings.scala | 2 +- .../main/scala/org/scalafmt/internal/FormatWriter.scala | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 3619becf19..f2e83fece3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2512,9 +2512,11 @@ s"user id is ${id}" #### `RedundantBraces`: `parensForOneLineApply` -`rewrite.redundantBraces.parensForOneLineApply` is `true` by default for -`edition` >= 2020-01. See also -[newlines.afterCurlyLambdaParams = squash](#newlinesaftercurlylambdaparams). +```scala mdoc:defaults +rewrite.redundantBraces.parensForOneLineApply +``` + +See also [newlines.afterCurlyLambdaParams = squash](#newlinesaftercurlylambdaparams). ```scala mdoc:scalafmt rewrite.rules = [RedundantBraces] diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala index 57e3a55ff9..acdb1578d0 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala @@ -7,7 +7,7 @@ case class RedundantBracesSettings( includeUnitMethods: Boolean = true, maxLines: Int = 100, stringInterpolation: Boolean = false, - parensForOneLineApply: Option[Boolean] = None, + parensForOneLineApply: Boolean = true, generalExpressions: Boolean = true, ifElseExpressions: Boolean = false ) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index e99fa8ab49..78199f6c4b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -147,7 +147,7 @@ class FormatWriter(formatOps: FormatOps) { checkInsertBraces(result) if ( initStyle.rewrite.rules.contains(RedundantBraces) && - !initStyle.rewrite.redundantBraces.parensForOneLineApply.contains(false) + initStyle.rewrite.redundantBraces.parensForOneLineApply ) replaceRedundantBraces(result) From b0af3b08453ee9d9c5efebc341a9830541eedca5 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:32:08 -0800 Subject: [PATCH 3/4] RedundantBraces: rename maxLines to maxBreaks In reality, the parameter has always been implemented to constrain the difference between last line and first line, so calling it `maxLines` was misleading. --- docs/configuration.md | 9 ++++++--- .../org/scalafmt/config/RedundantBracesSettings.scala | 4 +++- .../main/scala/org/scalafmt/config/ScalafmtConfig.scala | 2 +- .../scala/org/scalafmt/rewrite/RedundantBraces.scala | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index f2e83fece3..a8f86c8050 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2525,15 +2525,18 @@ rewrite.redundantBraces.parensForOneLineApply = true xs.map { x => x + 1 } ``` -#### `RedundantBraces`: `maxLines` +#### `RedundantBraces`: `maxBreaks` + +This parameter limits the number of line breaks inside the input body. Prior to +v3.3.2, was incorrectly called `maxLines`. ```scala mdoc:defaults -rewrite.redundantBraces.maxLines +rewrite.redundantBraces.maxBreaks ``` ```scala mdoc:scalafmt rewrite.rules = [RedundantBraces] -rewrite.redundantBraces.maxLines = 3 +rewrite.redundantBraces.maxBreaks = 3 --- def f() = { collection diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala index acdb1578d0..aba00c73e7 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala @@ -1,11 +1,13 @@ package org.scalafmt.config import metaconfig._ +import metaconfig.annotation.ExtraName case class RedundantBracesSettings( methodBodies: Boolean = true, includeUnitMethods: Boolean = true, - maxLines: Int = 100, + @ExtraName("maxLines") + maxBreaks: Int = 100, stringInterpolation: Boolean = false, parensForOneLineApply: Boolean = true, generalExpressions: Boolean = true, diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala index becac1164d..0b88595d27 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala @@ -422,7 +422,7 @@ object ScalafmtConfig { addIf(rewrite.insertBraces.minLines != 0 && rewrite.scala3.insertEndMarkerMinLines != 0) addIf(rewrite.insertBraces.minLines != 0 && rewrite.scala3.removeOptionalBraces == RemoveOptionalBraces.oldSyntaxToo) if (rewrite.insertBraces.minLines != 0 && rewrite.rules.contains(RedundantBraces)) - addIf(rewrite.insertBraces.minLines <= rewrite.redundantBraces.maxLines) + addIf(rewrite.insertBraces.minLines < rewrite.redundantBraces.maxBreaks) } // scalafmt: {} if (allErrors.isEmpty) Configured.ok(cfg) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala index 0d0fe8943f..4fd1eb1ccb 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala @@ -442,7 +442,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { settings.methodBodies && (getTreeSingleStat(b) match { case Some(_: Term.PartialFunction) => false case Some(_: Term.Block) => true - case Some(s) => getTreeLineSpan(s) <= settings.maxLines + case Some(s) => getTreeLineSpan(s) <= settings.maxBreaks case _ => okIfMultipleStats }) @@ -466,7 +466,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { private def getSingleStatIfLineSpanOk(b: Term.Block)(implicit style: ScalafmtConfig ): Option[Stat] = - getBlockSingleStat(b).filter(getTreeLineSpan(_) <= settings.maxLines) + getBlockSingleStat(b).filter(getTreeLineSpan(_) <= settings.maxBreaks) // special case for Select which might contain a space instead of dot private def isPrefixExpr(expr: Tree): Boolean = From ff49a1e37018564fa9d354c6a35ecfb1c952772a Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 4 Jan 2022 19:07:35 -0800 Subject: [PATCH 4/4] RedundantBraces: rename methodBodies to defnBodies --- docs/configuration.md | 8 +++++--- .../org/scalafmt/config/RedundantBracesSettings.scala | 3 ++- .../main/scala/org/scalafmt/rewrite/RedundantBraces.scala | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index a8f86c8050..9543d8b4b8 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2460,15 +2460,17 @@ if (a > b) { } ``` -#### `RedundantBraces`: `methodBodies` +#### `RedundantBraces`: `defnBodies` + +In v3.3.2, this parameter superseded a boolean `methodBodies`. ```scala mdoc:defaults -rewrite.redundantBraces.methodBodies +rewrite.redundantBraces.defnBodies ``` ```scala mdoc:scalafmt rewrite.rules = [RedundantBraces] -rewrite.redundantBraces.methodBodies = true +rewrite.redundantBraces.defnBodies = true --- def f() = { 1 + 1 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala index aba00c73e7..62256e95dd 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/RedundantBracesSettings.scala @@ -4,7 +4,8 @@ import metaconfig._ import metaconfig.annotation.ExtraName case class RedundantBracesSettings( - methodBodies: Boolean = true, + @ExtraName("methodBodies") + defnBodies: Boolean = true, includeUnitMethods: Boolean = true, @ExtraName("maxLines") maxBreaks: Int = 100, diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala index 4fd1eb1ccb..0d3c6d585b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala @@ -149,7 +149,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { !x.tokens.exists(_.is[Token.RightArrow])) => removeToken case t: Ctor.Secondary - if t.stats.isEmpty && style.rewrite.redundantBraces.methodBodies => + if t.stats.isEmpty && style.rewrite.redundantBraces.defnBodies => val prevIsEquals = ftoks.prevNonComment(ft).left.is[Token.Equals] if (prevIsEquals) removeToken else replaceWithEquals case _ => null @@ -309,7 +309,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { case Type.Name("Unit") => true case _ => false } - settings.methodBodies && + settings.defnBodies && checkBlockAsBody(b, d.body) && !isProcedureSyntax(d) && !disqualifiedByUnit @@ -439,7 +439,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { b: Term, okIfMultipleStats: => Boolean )(implicit style: ScalafmtConfig): Boolean = - settings.methodBodies && (getTreeSingleStat(b) match { + settings.defnBodies && (getTreeSingleStat(b) match { case Some(_: Term.PartialFunction) => false case Some(_: Term.Block) => true case Some(s) => getTreeLineSpan(s) <= settings.maxBreaks