Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantBraces: rename some parameters, amend documentation #3030

Merged
merged 4 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2417,7 +2417,7 @@ List(1, 2, 3).map { x =>
}
```

#### Configuration options
#### `RedundantBraces`: `generalExpressions`

```scala mdoc:defaults
rewrite.redundantBraces.generalExpressions
Expand All @@ -2442,6 +2442,8 @@ str match {
}
```

#### `RedundantBraces`: `ifElseExpressions`

```scala mdoc:defaults
rewrite.redundantBraces.ifElseExpressions
```
Expand All @@ -2458,19 +2460,25 @@ if (a > b) {
}
```

#### `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
}
```

#### `RedundantBraces`: `includeUnitMethods`

```scala mdoc:defaults
rewrite.redundantBraces.includeUnitMethods
```
Expand All @@ -2491,6 +2499,8 @@ def x(): Unit = {
}
```

#### `RedundantBraces`: `stringInterpolation`

```scala mdoc:defaults
rewrite.redundantBraces.stringInterpolation
```
Expand All @@ -2502,9 +2512,13 @@ rewrite.redundantBraces.stringInterpolation = true
s"user id is ${id}"
```

`rewrite.redundantBraces.parensForOneLineApply` is `true` by default for
`edition` >= 2020-01. See also
[newlines.afterCurlyLambdaParams = squash](#newlinesaftercurlylambdaparams).
#### `RedundantBraces`: `parensForOneLineApply`

```scala mdoc:defaults
rewrite.redundantBraces.parensForOneLineApply
```

See also [newlines.afterCurlyLambdaParams = squash](#newlinesaftercurlylambdaparams).

```scala mdoc:scalafmt
rewrite.rules = [RedundantBraces]
Expand All @@ -2513,13 +2527,18 @@ rewrite.redundantBraces.parensForOneLineApply = true
xs.map { x => x + 1 }
```

#### `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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.scalafmt.config

import metaconfig._
import metaconfig.annotation.ExtraName

case class RedundantBracesSettings(
methodBodies: Boolean = true,
@ExtraName("methodBodies")
defnBodies: Boolean = true,
includeUnitMethods: Boolean = true,
maxLines: Int = 100,
@ExtraName("maxLines")
maxBreaks: Int = 100,
stringInterpolation: Boolean = false,
parensForOneLineApply: Option[Boolean] = None,
parensForOneLineApply: Boolean = true,
generalExpressions: Boolean = true,
ifElseExpressions: Boolean = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -439,10 +439,10 @@ 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.maxLines
case Some(s) => getTreeLineSpan(s) <= settings.maxBreaks
case _ => okIfMultipleStats
})

Expand All @@ -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 =
Expand Down