Skip to content

Commit

Permalink
RemoveScala3OptionalBraces: add fewerBracesXxStats
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew authored and Albert Meltzer committed Mar 10, 2024
1 parent 8c00f0e commit b522e67
Show file tree
Hide file tree
Showing 8 changed files with 1,265 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3698,6 +3698,13 @@ The section contains the following settings (available since v3.8.1):
- other flags below might extend rewrites to other cases
- `oldSyntaxToo`
- if `true`, applies also to expressions using deprecated syntax
- `fewerBracesMinStats` and `fewerBracesMaxStats`
- will apply the rewrite to last curried single-argument group if
it is enclosed in curly braces (or would be rewritten to curly
braces by the `RedundantBraces` rule)
- will only apply the rewrite if the number of statements (including
nested ones) within the argument falls between the two values
- this rule is disabled if `fewerBracesMaxStats == 0`

Prior to v3.8.1, `rewrite.scala3.removeOptionalBraces` was a flag which
took three possible values (with their equivalent current settings shown):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ object RewriteScala3Settings {

case class RemoveOptionalBraces(
enabled: Boolean = true,
fewerBracesMinStats: Int = 2,
fewerBracesMaxStats: Int = 0,
oldSyntaxToo: Boolean = false
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ object ScalafmtConfig {
addIf(rewrite.insertBraces.minLines < rewrite.redundantBraces.maxBreaks)
addIf(align.beforeOpenParenDefnSite && !align.closeParenSite)
addIf(align.beforeOpenParenCallSite && !align.closeParenSite)
addIf(rewrite.scala3.removeOptionalBraces.fewerBracesMinStats <= 0)
if (rewrite.scala3.removeOptionalBraces.fewerBracesMaxStats != 0) {
addIf(rewrite.scala3.removeOptionalBraces.fewerBracesMaxStats < 0)
addIf(rewrite.scala3.removeOptionalBraces.fewerBracesMinStats > rewrite.scala3.removeOptionalBraces.fewerBracesMaxStats)
}
}
// scalafmt: {}
if (allErrors.isEmpty) Configured.ok(cfg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.meta.tokens.Token
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.internal.FormatToken
import org.scalafmt.internal.FormatTokens
import org.scalafmt.util.TreeOps
import org.scalafmt.util.TreeOps._

object RemoveScala3OptionalBraces extends FormatTokensRewrite.RuleFactory {

Expand Down Expand Up @@ -103,7 +103,7 @@ private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens)
case _: Token.KwIf => true
case _: Token.KwThen => true
case _: Token.KwElse =>
!TreeOps.isTreeMultiStatBlock(t.elsep) ||
!isTreeMultiStatBlock(t.elsep) ||
ftoks.tokenAfter(t.cond).right.is[Token.KwThen]
case _: Token.RightParen => allowOldSyntax
case _ => false
Expand Down
317 changes: 317 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1802,3 +1802,320 @@ object a:
object a:
foo:
erased bar => baz
<<< rewrite to fewer braces: block simple
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 2
fewerBracesMaxStats = 2
}
===
foo
.mtd1 {
x + 1
}
.mtd2 {
x + 1
x + 2
}
.mtd3 {
x + 1
x + 2
x + 3
}
>>>
foo
.mtd1 {
x + 1
}
.mtd2 {
x + 1
x + 2
}
.mtd3 {
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 3
fewerBracesMaxStats = 3
}
===
foo
.mtd1 { x =>
x + 1
}
.mtd2 { x =>
x + 1
x + 2
}
.mtd3 { x =>
x + 1
x + 2
x + 3
}
>>>
foo
.mtd1 { x =>
x + 1
}
.mtd2 { x =>
x + 1
x + 2
}
.mtd3 { x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func in parens
rewrite.rules = [RedundantBraces]
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 3
fewerBracesMaxStats = 3
}
===
foo
.mtd1(
x => {
x + 1
}
)
.mtd2(
x => {
x + 1
x + 2
}
)
.mtd3(
x => {
x + 1
x + 2
x + 3
}
)
>>>
foo
.mtd1 { x =>
x + 1
}
.mtd2 { x =>
x + 1
x + 2
}
.mtd3 { x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: func in parens and braces
rewrite.rules = [RedundantBraces, RedundantParens]
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 3
fewerBracesMaxStats = 3
}
===
foo
.mtd1(
{ x =>
x + 1
}
)
.mtd2(
{ x =>
x + 1
x + 2
}
)
.mtd3(
{ x =>
x + 1
x + 2
x + 3
}
)
>>>
foo
.mtd1 { x =>
x + 1
}
.mtd2 { x =>
x + 1
x + 2
}
.mtd3 { x =>
x + 1
x + 2
x + 3
}
<<< rewrite to fewer braces: partial func
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 2
fewerBracesMaxStats = 2
}
===
foo
.mtd1 {
case x => x + 1
}
.mtd2 {
case x => x + 1
case y => y + 1
}
.mtd3 {
case x => x + 1
case y => y + 1
case z => z + 1
}
>>>
foo
.mtd1 { case x =>
x + 1
}
.mtd2 {
case x => x + 1
case y => y + 1
}
.mtd3 {
case x => x + 1
case y => y + 1
case z => z + 1
}
<<< rewrite to fewer braces: partial func in parens
rewrite.rules = [RedundantBraces]
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 2
fewerBracesMaxStats = 2
}
===
foo
.mtd1(
{
case x => x + 1
}
)
.mtd2(
{
case x => x + 1
case y => y + 1
}
)
.mtd3(
{
case x => x + 1
case y => y + 1
case z => z + 1
}
)
>>>
foo
.mtd1 { case x =>
x + 1
}
.mtd2 {
case x => x + 1
case y => y + 1
}
.mtd3 {
case x => x + 1
case y => y + 1
case z => z + 1
}
<<< rewrite to fewer braces: match
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 3
fewerBracesMaxStats = 3
}
===
foo
.mtd1 {
bar match {
case x => x + 1
}
}
.mtd2 {
bar match {
case x => x + 1
case y => y + 1
}
}
.mtd3 {
bar match {
case x => x + 1
case y => y + 1
case z => z + 1
}
}
>>>
foo
.mtd1 {
bar match
case x => x + 1
}
.mtd2 {
bar match
case x => x + 1
case y => y + 1
}
.mtd3 {
bar match
case x => x + 1
case y => y + 1
case z => z + 1
}
<<< rewrite to fewer braces: block complex
rewrite.scala3.removeOptionalBraces = {
enabled = yes
fewerBracesMinStats = 4
fewerBracesMaxStats = 4
}
===
foo
.mtd1 {
x + 1
def x = {
x + 3
}
}
.mtd2 {
x + 1
def x = {
x + 3
x + 4
}
}
.mtd3 {
x + 1
def x = {
x + 3
x + 4
}
x + 5
}
>>>
foo
.mtd1 {
x + 1
def x =
x + 3
}
.mtd2 {
x + 1
def x =
x + 3
x + 4
}
.mtd3 {
x + 1
def x =
x + 3
x + 4
x + 5
}
Loading

0 comments on commit b522e67

Please sign in to comment.