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

Documentation for RedundantBraces settings #1945

Merged
merged 4 commits into from
May 4, 2020
Merged
Changes from 3 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
119 changes: 107 additions & 12 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1111,20 +1111,113 @@ List(1, 2, 3).map { x =>
}
```

Configuration options and default values:
#### Configuration options

// TODO(olafur): multiline defaults
```scala mdoc:defaults
rewrite.redundantBraces.generalExpressions
```

```scala mdoc:scalafmt
rewrite.rules = [RedundantBraces]
rewrite.redundantBraces.generalExpressions = true
---

if (a > b) {
doSomething()
} else {
doAnything()
}

while (x < 10) {
x += 1
}

str match {
case "a" => {
println("ok")
}
case _ => {
println("not ok")
}
}
```

- `rewrite.redundantBraces.maxLines = 100`
- `rewrite.redundantBraces.includeUnitMethods = true`
- `rewrite.redundantBraces.methodBodies = true`
- `rewrite.redundantBraces.stringInterpolation = true`
- `rewrite.redundantBraces.generalExpressions = false` (disabled by default due
to #1147)
- `rewrite.redundantBraces.parensForOneLineApply`
- since v2.4.2
- by default, `true` in edition 2020-01
- turns `foo { bar => baz }` into `foo(bar => baz)`
```scala mdoc:defaults
rewrite.redundantBraces.methodBodies
```

```scala mdoc:scalafmt
rewrite.rules = [RedundantBraces]
rewrite.redundantBraces.methodBodies = true
---
def f() = {
1 + 1
}
```

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

Affects only functions with **explicitly** specified `Unit` type

```scala mdoc:scalafmt
rewrite.rules = [RedundantBraces]
rewrite.redundantBraces.methodBodies = true
rewrite.redundantBraces.includeUnitMethods = false
---
def f() = {
1 + 1
}

def x(): Unit = {
println("example")
}
```

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

```scala mdoc:scalafmt
rewrite.rules = [RedundantBraces]
rewrite.redundantBraces.stringInterpolation = true
---
s"user id is ${id}"
```

`rewrite.redundantBraces.parensForOneLineApply` is `true` by default for `edition` >= 2020-01. See also [newlines.afterCurlyLambda](http://localhost:3000/scalafmt/docs/configuration.html#newlinesaftercurlylambda)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess you could mention that it only works if afterCurlyLambda = squash specifically.


```scala mdoc:scalafmt
rewrite.rules = [RedundantBraces]
rewrite.redundantBraces.parensForOneLineApply = true
---
xs.map { x => x + 1 }
```

```scala mdoc:defaults
rewrite.redundantBraces.maxLines
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rant. this parameter has an off-by-one bug. it's not maxLines, it's maxBreaks (i.e., lines - 1).

```

```scala mdoc:scalafmt
rewrite.rules = [RedundantBraces]
rewrite.redundantBraces.maxLines = 3
---
def f() = {
collection
.map(x => x + 1)
.filter(_ < 10)
.map(_ * 2)
}

def f() = {
collection
.map(x => x + 1)
.filter(_ < 10)
.map(_ * 2)
.headOption
}
```

### `RedundantParens`

Expand All @@ -1135,6 +1228,8 @@ for {
a <- b
if (a.nonEmpty)
} yield a

val z = (insertData *> readDatabase(id))
```

### `SortModifiers`
Expand Down