Skip to content

Commit

Permalink
RedundantBraces: replace ( with { more often
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 22, 2024
1 parent af59071 commit 815d2f8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,42 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
private def onLeftParen(implicit ft: FT, style: ScalafmtConfig): Replacement = {
val rt = ft.right

def replaceWithBrace(owner: Option[Tree] = None, claim: List[Int] = Nil) = {
def replaceWithBrace(claim: List[Int], owner: Option[Tree] = None) = {
val lb = new T.LeftBrace(rt.input, rt.dialect, rt.start)
replaceToken("{", owner = owner, claim = claim)(lb)
}

ft.rightOwner match {
case ta @ Term.ArgClause(arg :: Nil, _)
if !ta.parent.is[Init] && getOpeningParen(ta).contains(rt) =>
def replaceWithNextBrace() = ftoks.nextNonCommentAfter(ft) match {
def replaceWithNextBrace(claimToo: List[Int] = Nil) = ftoks
.nextNonCommentAfter(ft) match {
case FT(lt, _: T.LeftBrace, m) =>
val claim = m.idx :: Nil
val claim = m.idx :: claimToo
if (lt eq rt) removeToken(claim = claim)
else replaceWithBrace(owner = Some(m.rightOwner), claim = claim)
case _ => replaceWithBrace(Some(arg))
case _ => replaceWithBrace(owner = Some(arg), claim = claimToo)
}
arg match {
// single-arg apply of a partial function or optionally any arg
// a({ case b => c; d }) change to a { case b => c; d }
case _: Term.PartialFunction => replaceWithNextBrace()
case b: Term.Block =>
val ok = getTreeSingleExpr(b).is[Term.PartialFunction]
if (ok) replaceWithNextBrace() else null
val stat = getBlockSingleStat(b)
stat.flatMap(getTreeSingleExpr) match {
case Some(f: Term.FunctionTerm) =>
getBlockToReplaceAsFuncBodyInSingleArgApply(ta, f) match {
case Some((xb, xlb)) =>
if (canRewriteStatWithParens(xb)) null
else replaceWithNextBrace(claimToo = xlb.idx - 1 :: Nil)
case _ => replaceWithNextBrace()
}
case Some(s: Term)
if okLineSpan(b) &&
!stat.exists(ftoks.tokenAfter(_).right.is[T.Semicolon]) &&
canRewriteStatWithParens(s) => null
case _ => replaceWithNextBrace()
}
case f: Term.FunctionTerm =>
getBlockToReplaceAsFuncBodyInSingleArgApply(ta, f) match {
case Some((_, xlb)) => replaceWithBrace(claim = xlb.idx - 1 :: Nil)
Expand Down
100 changes: 42 additions & 58 deletions scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ object a {
}
>>>
object a {
foo({ bar = baz })
foo { bar = baz }
foo({ bar = baz }, qux)
}
<<< #3296
Expand Down Expand Up @@ -1612,10 +1612,10 @@ foo.mtd({
},
)
>>>
foo.mtd({
foo.mtd {
x + 1
x + 2
})
}
<<< rewrite with trailing commas: block simple, !allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = false
Expand All @@ -1626,10 +1626,10 @@ foo.mtd({
},
)
>>>
foo.mtd({
foo.mtd {
x + 1
x + 2
})
}
<<< rewrite with trailing commas: func, allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = true
Expand All @@ -1640,10 +1640,10 @@ foo.mtd1({ x =>
},
)
>>>
foo.mtd1({ x =>
foo.mtd1 { x =>
x + 1
x + 2
})
}
<<< rewrite with trailing commas: func, !allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = false
Expand All @@ -1654,10 +1654,10 @@ foo.mtd1({ x =>
},
)
>>>
foo.mtd1({ x =>
foo.mtd1 { x =>
x + 1
x + 2
})
}
<<< rewrite with trailing commas: func in parens, infix after, allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = true
Expand Down Expand Up @@ -1720,12 +1720,10 @@ foo.mtd(
},
)
>>>
foo.mtd(
{ x =>
x + 1
x + 2
},
)
foo.mtd { x =>
x + 1
x + 2
}
<<< rewrite with trailing commas: func in parens and braces, !allowFolding
rewrite.rules = [RedundantBraces, RedundantParens]
rewrite.trailingCommas.style = keep
Expand All @@ -1738,12 +1736,10 @@ foo.mtd(
},
)
>>>
foo.mtd(
{ x =>
x + 1
x + 2
},
)
foo.mtd { x =>
x + 1
x + 2
}
<<< rewrite with trailing commas: func in parens and braces, infix after, allowFolding
rewrite.rules = [RedundantBraces, RedundantParens]
rewrite.trailingCommas.style = keep
Expand All @@ -1769,22 +1765,16 @@ object a {
}
>>>
object a {
mtd1(
{ x =>
x + 1
},
) + mtd2(
{ x =>
x + 1
x + 2
},
) + mtd3(
{ x =>
x + 1
x + 2
x + 3
},
)
mtd1 { x =>
x + 1
} + mtd2 { x =>
x + 1
x + 2
} + mtd3 { x =>
x + 1
x + 2
x + 3
}
}
<<< rewrite with trailing commas: func in parens and braces, infix after, !allowFolding
rewrite.rules = [RedundantBraces, RedundantParens]
Expand All @@ -1811,22 +1801,16 @@ object a {
}
>>>
object a {
mtd1(
{ x =>
x + 1
},
) + mtd2(
{ x =>
x + 1
x + 2
},
) + mtd3(
{ x =>
x + 1
x + 2
x + 3
},
)
mtd1 { x =>
x + 1
} + mtd2 { x =>
x + 1
x + 2
} + mtd3 { x =>
x + 1
x + 2
x + 3
}
}
<<< rewrite with trailing commas: partial func, allowFolding
rewrite.trailingCommas.style = keep
Expand Down Expand Up @@ -1867,11 +1851,11 @@ foo.mtd({
},
)
>>>
foo.mtd({
foo.mtd {
bar match {
case x => x + 1
}
})
}
<<< rewrite with trailing commas: match, !allowFolding
rewrite.trailingCommas.style = keep
rewrite.trailingCommas.allowFolding = false
Expand All @@ -1883,11 +1867,11 @@ foo.mtd({
},
)
>>>
foo.mtd({
foo.mtd {
bar match {
case x => x + 1
}
})
}
<<< with infix (from sbt-scalafmt plugin)
maxColumn = 80
newlines.source = fold
Expand Down Expand Up @@ -2233,7 +2217,7 @@ run { worklist.push(startBlock); }
<<< #4133 can't rewrite with parens if semicolon, without redundant parens
run( { worklist.push(startBlock); } )
>>>
run({ worklist.push(startBlock); })
run { worklist.push(startBlock); }
<<< #4133 rewrites with assignment and infix chain
maxColumn = 76
rewrite.redundantBraces {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ object a {
}
>>>
object a {
val switchVersion: Command = Command.args("+~", "<version> <args>")({
val switchVersion: Command = Command.args("+~", "<version> <args>") {
(initialState: State, args: Seq[String]) =>
def run = ???
})
}
}
<<< block within parens: single-stat func with multi-stat body
object a {
Expand All @@ -391,11 +391,11 @@ object a {
}
>>>
object a {
val switchVersion: Command = Command.args("+~", "<version> <args>")({
val switchVersion: Command = Command.args("+~", "<version> <args>") {
(initialState: State, args: Seq[String]) =>
val requestedVersionPrefix = args.head
def run = ???
})
}
}
<<< block within parens: single-stat defn
object a {
Expand All @@ -405,9 +405,9 @@ object a {
}
>>>
object a {
val switchVersion: Command = Command.args("+~", "<version> <args>")({
val switchVersion: Command = Command.args("+~", "<version> <args>") {
val foo = ???
})
}
}
<<< block within parens: multi-stat
object a {
Expand All @@ -419,9 +419,9 @@ object a {
}
>>>
object a {
val switchVersion: Command = Command.args("+~", "<version> <args>")({
val switchVersion: Command = Command.args("+~", "<version> <args>") {
val foo = 0
val bar = 1
foo + bar
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1203371, "total explored")
assertEquals(explored, 1203082, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit 815d2f8

Please sign in to comment.