From 1c0c8025bf24e0797927a57cda2643a525f7ca1e Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:18:36 -0700 Subject: [PATCH] FormatOps: fix leading infix before fewer braces Specifically, just like after fewer braces, we shouldn't indent leading infix before fewer braces either. --- .../org/scalafmt/internal/FormatOps.scala | 4 ++- .../scala/org/scalafmt/util/TreeOps.scala | 11 ++++++++ .../test/resources/scala3/FewerBraces.stat | 25 +++++++++---------- .../resources/scala3/FewerBraces_keep.stat | 25 +++++++++---------- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index 993de99115..11fb59cbc9 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -508,7 +508,9 @@ class FormatOps( val mod = getMod(ft) val modOrNoSplit = if (mod != Space || isBeforeOp || useSpace) mod else NoSplit - Seq(InfixSplits.withNLIndent(Split(modOrNoSplit, 0))(app, ft)) + val split = Split(modOrNoSplit, 0) + if (isBeforeOp && isFewerBracesRhs(app.arg)) Seq(split) + else Seq(InfixSplits.withNLIndent(split)(app, ft)) } } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index cf972e248f..2d431dd761 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -1083,6 +1083,17 @@ object TreeOps { case _ => false }) + @tailrec + def isFewerBracesRhs( + tree: Tree + )(implicit dialect: Dialect, ftoks: FormatTokens): Boolean = + !ftoks.isEnclosedInMatching(tree) && (tree match { + case t: Term.Apply => isFewerBraces(t) + case t: Term.ApplyInfix => isFewerBracesRhs(t.lhs) + case Term.ArgClause(arg :: Nil, _) => isFewerBracesRhs(arg) + case _ => false + }) + def isParentAnApply(t: Tree): Boolean = t.parent.exists(_.is[Term.Apply]) diff --git a/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat b/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat index 34569e8492..dbeb6b004d 100644 --- a/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/FewerBraces.stat @@ -2271,16 +2271,15 @@ object a: x + 3 } >>> -Idempotency violated -=> Diff (- obtained, + expected) - x + 2 -- + mtd3 { x => -- x + 1 -- x + 2 -- x + 3 -- } -+ + mtd3 { x => -+ x + 1 -+ x + 2 -+ x + 3 -+ } +object a: + mtd1 { x => + x + 1 + } + + mtd2: x => + x + 1 + x + 2 + + mtd3 { x => + x + 1 + x + 2 + x + 3 + } diff --git a/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat index de48bf9b2f..f139c86826 100644 --- a/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat @@ -2248,16 +2248,15 @@ object a: x + 3 } >>> -Idempotency violated -=> Diff (- obtained, + expected) - x + 2 -- + mtd3 { x => -- x + 1 -- x + 2 -- x + 3 -- } -+ + mtd3 { x => -+ x + 1 -+ x + 2 -+ x + 3 -+ } +object a: + mtd1 { x => + x + 1 + } + + mtd2: x => + x + 1 + x + 2 + + mtd3 { x => + x + 1 + x + 2 + x + 3 + }