From 5046b8db867d92e109e944881908a896257be5ff Mon Sep 17 00:00:00 2001 From: Albert Meltzer Date: Sun, 18 Feb 2024 07:01:23 -0800 Subject: [PATCH] RedundantBraces: remove inner braces, not outer --- .../org/scalafmt/internal/FormatWriter.scala | 3 ++ .../scala/org/scalafmt/internal/Router.scala | 6 ++-- .../scalafmt/rewrite/RedundantBraces.scala | 34 ++++++++----------- .../resources/rewrite/RedundantBraces.stat | 24 +++++++++---- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index 9fbe2270a6..14a3090dc5 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -165,6 +165,9 @@ class FormatWriter(formatOps: FormatOps) { b.parent.exists(_.tokens.last.start == rb.start) case f: Term.FunctionTerm => checkApply(f) && RedundantBraces.canRewriteFuncWithParens(f) + case t @ TreeOps.SingleArgInBraces(arg) => + TreeOps.isParentAnApply(t) && + RedundantBraces.canRewriteStatWithParens(arg) case _ => false } if (ok) { diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 709aa8ed4e..1d3d58a13a 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -273,10 +273,12 @@ class Router(formatOps: FormatOps) { else Some(false) (expire, arrow.map(_.left), 0, nlOnly) case Some(t: Case) if t.cond.isEmpty && (leftOwner match { - case Term.PartialFunction(`t` :: Nil) => true + case x: Term.PartialFunction => + isSingleElement(x.cases, t) case x: Term.Match => isSingleElement(x.cases, t) && getMatchDot(x).isDefined - case _ => false + case _: Term.Try => false + case _ => tokens.tokenAfter(t).right eq close }) => val arrow = getCaseArrow(t) val nlOnly = diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala index 3d8355e7f9..4ac7e13af9 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala @@ -147,27 +147,29 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { onLeftBrace(ft.meta.rightOwner) } - @tailrec private def onLeftBrace(owner: Tree)(implicit ft: FormatToken, session: Session, style: ScalafmtConfig ): Replacement = { owner match { - case t: Term.ArgClause => - t.values match { - case arg :: Nil if t.pos.start == arg.pos.start => onLeftBrace(arg) - case _ => null - } case t: Term.FunctionTerm if t.tokens.last.is[Token.RightBrace] => if (!okToRemoveFunctionInApplyOrInit(t)) null else if (okToReplaceFunctionInSingleArgApply(t)) replaceWithLeftParen else removeToken + case t: Term.PartialFunction if t.parent.exists { p => + SingleArgInBraces.orBlock(p).contains(t) && + t.pos.start != p.pos.start + } => + removeToken case t: Term.Block => - if (getBlockNestedPartialFunction(t).isDefined) removeToken - else if (okToReplaceBlockInSingleArgApply(t)) replaceWithLeftParen - else if (processBlock(t)) removeToken - else null + t.parent match { + case Some(f: Term.FunctionTerm) + if okToReplaceFunctionInSingleArgApply(f) => + replaceWithLeftParen + case _ => + if (processBlock(t)) removeToken else null + } case _: Term.Interpolate if style.rewrite.redundantBraces.stringInterpolation && processInterpolation => @@ -239,15 +241,6 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { }) } - private def okToReplaceBlockInSingleArgApply( - b: Term.Block - )(implicit style: ScalafmtConfig): Boolean = - b.parent.exists { - case f: Term.FunctionTerm => - okToReplaceFunctionInSingleArgApply(f) - case _ => false - } - private def okToReplaceFunctionInSingleArgApply(f: Term.FunctionTerm)(implicit style: ScalafmtConfig ): Boolean = @@ -346,7 +339,8 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { // Example: as.map { _.toString } // Leave this alone for now. // In future there should be an option to surround such expressions with parens instead of braces - isSeqMulti(t.values) && okToRemoveBlockWithinApply(b) + if (isSeqMulti(t.values)) okToRemoveBlockWithinApply(b) + else (t.pos.start != b.pos.start) && SingleArgInBraces.inBraces(t) case d: Defn.Def => def disqualifiedByUnit = diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat index ef43893387..7ed3efc86b 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat @@ -1135,12 +1135,24 @@ object a { } >>> object a { - val foo = bar { case x => y } - val foo = bar { case x => y } - val foo = bar { case x => y } - val foo = bar.baz { case x => y } - val foo = bar.baz { case x => y } - val foo = bar.baz { case x => y } + val foo = bar { case x => + y + } + val foo = bar { case x => + y + } + val foo = bar { case x => + y + } + val foo = bar.baz { case x => + y + } + val foo = bar.baz { case x => + y + } + val foo = bar.baz { case x => + y + } } <<< init-only secondary ctors class a(vi: Int, vs: String) {