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 d773f5174..21c9bbf27 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 45b45c97e..845443d7d 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 af68c0651..c464cb1ce 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 47829457e..1dda6ad54 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 391f36193..9873d9b98 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 55d26ffdb..1244a8364 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 :'(