From 0552010e5e76e76fc8a647e83ce830ae3cc0e447 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 6 Nov 2024 23:02:26 -0800 Subject: [PATCH] RemoveScala3OptionalBraces: `()` to fewer braces --- .../rewrite/RemoveScala3OptionalBraces.scala | 13 +++++++++++-- .../src/test/resources/scala3/FewerBraces.stat | 7 +++---- .../src/test/resources/scala3/FewerBraces_fold.stat | 3 ++- .../src/test/resources/scala3/FewerBraces_keep.stat | 7 +++---- .../test/resources/scala3/FewerBraces_unfold.stat | 3 ++- .../src/test/scala/org/scalafmt/FormatTests.scala | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala index d773f5174c..21c9bbf271 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala @@ -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 => @@ -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 } } @@ -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 diff --git a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces.stat b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces.stat index 45b45c97e9..845443d7da 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces.stat @@ -2314,7 +2314,6 @@ object a: ) >>> object a: - println( - a - + b - ) + println: + a + + b diff --git a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_fold.stat b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_fold.stat index af68c06511..c464cb1ce0 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_fold.stat @@ -2043,4 +2043,5 @@ object a: ) >>> object a: - println(a + b) + println: + a + b diff --git a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_keep.stat b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_keep.stat index 47829457e6..1dda6ad54b 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_keep.stat @@ -2290,7 +2290,6 @@ object a: ) >>> object a: - println( - a - + b - ) + println: + a + + b diff --git a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_unfold.stat b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_unfold.stat index 391f36193a..9873d9b983 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/FewerBraces_unfold.stat @@ -2323,4 +2323,5 @@ object a: ) >>> object a: - println(a + b) + println: + a + b diff --git a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala index 55d26ffdbf..1244a83648 100644 --- a/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala +++ b/scalafmt-tests/shared/src/test/scala/org/scalafmt/FormatTests.scala @@ -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 :'(