Skip to content

Commit

Permalink
RemoveScala3OptionalBraces: () to fewer braces
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 7, 2024
1 parent 337c794 commit 0552010
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens)
): Option[Replacement] = Option {
ft.right match {
case x: Token.LeftBrace // skip empty brace pairs
if !ftoks.nextNonComment(ftoks.next(ft)).right.is[Token.RightBrace] =>
if !ftoks.nextNonCommentAfter(ft).right.is[Token.RightBrace] =>
ft.meta.rightOwner match {
case t: Term.Block if t.stats.nonEmpty => onLeftForBlock(t)
case t: Template.Body if !t.isEmpty =>
Expand All @@ -68,6 +68,12 @@ private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens)
if ftoks.prevNonComment(ft).left.is[Token.Equals] => removeToken
case _ => null
}
case _: Token.LeftParen
if !ftoks.nextNonCommentAfter(ft).right.is[Token.RightParen] =>
ft.meta.rightOwner match {
case t: Term.ArgClause => onLeftForArgClause(t)
case _ => null
}
case _ => null
}
}
Expand Down Expand Up @@ -187,7 +193,10 @@ private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens)
private[rewrite] def onLeftForArgClause(
tree: Term.ArgClause,
)(implicit ft: FormatToken, style: ScalafmtConfig): Replacement = {
val ok = style.dialect.allowFewerBraces &&
def okLeftDelim = ft.right.is[Token.LeftBrace] ||
style.rewrite.scala3.removeOptionalBraces.fewerBracesParensToo &&
(style.dialect.allowInfixOperatorAfterNL || style.newlines.formatInfix)
val ok = style.dialect.allowFewerBraces && okLeftDelim &&
style.rewrite.scala3.removeOptionalBraces.fewerBracesMaxSpan > 0 &&
isSeqSingle(tree.values)
if (!ok) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2314,7 +2314,6 @@ object a:
)
>>>
object a:
println(
a
+ b
)
println:
a
+ b
Original file line number Diff line number Diff line change
Expand Up @@ -2043,4 +2043,5 @@ object a:
)
>>>
object a:
println(a + b)
println:
a + b
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,6 @@ object a:
)
>>>
object a:
println(
a
+ b
)
println:
a
+ b
Original file line number Diff line number Diff line change
Expand Up @@ -2323,4 +2323,5 @@ object a:
)
>>>
object a:
println(a + b)
println:
a + b
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,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, 1502266, "total explored")
assertEquals(explored, 1502258, "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 0552010

Please sign in to comment.