From eef3bebfa0d26b5f0fc7b02c59778249467ef2a5 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 25 Aug 2021 07:42:07 -0700 Subject: [PATCH 1/2] Add tests with redundant braces and partial funcs --- .../resources/rewrite/RedundantBraces.stat | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat index d1b070e0b8..22eb914c5f 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat @@ -1082,3 +1082,87 @@ object a { y } } +<<< single-block partial function with parens one-arg apply +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 } } } + ) +} +>>> +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 } } + ) +} +<<< single-block partial function with block one-arg apply +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 } } } + } +} +>>> +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 } + } +} From cc2c490af1dfcf46d7d88a16652bd93b8f7ce724 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Thu, 26 Aug 2021 18:59:33 -0700 Subject: [PATCH 2/2] RedundantBraces: remove braces around partial func --- .../scalafmt/rewrite/RedundantBraces.scala | 45 +++++++++++++---- .../resources/rewrite/RedundantBraces.stat | 48 +++++-------------- 2 files changed, 49 insertions(+), 44 deletions(-) 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 2ed180d26b..2adf1e8077 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 @@ -85,20 +85,31 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { ): Replacement = { val rt = ft.right val rtOwner = ft.meta.rightOwner - val ok = okToReplaceFunctionInSingleArgApply(rtOwner).exists { - case (lp, func) => lp.eq(rt) && func.tokens.last.is[Token.RightBrace] + def lpFunction = okToReplaceFunctionInSingleArgApply(rtOwner).map { + case (lp, f) if lp.eq(rt) && f.tokens.last.is[Token.RightBrace] => + replaceToken("{", Some(rtOwner)) { + new Token.LeftBrace(rt.input, rt.dialect, rt.start) + } + case _ => null } - if (ok) replaceToken("{", Some(rtOwner)) { - new Token.LeftBrace(rt.input, rt.dialect, rt.start) + // single-arg apply of a partial function + // a({ case b => c; d }) change to a { case b => c; d } + def lpPartialFunction = rtOwner match { + case ta @ Term.Apply(_, List(arg)) => + getOpeningParen(ta).map { lp => + val ko = lp.ne(rt) || getBlockNestedPartialFunction(arg).isEmpty + if (ko) null else removeToken + } + case _ => None } - else null + + lpFunction.orElse(lpPartialFunction).orNull } private def onRightParen( left: Replacement )(implicit ft: FormatToken): (Replacement, Replacement) = - if (left.exists(_.right.is[Token.LeftBrace])) (left, removeToken) - else null + (left, removeToken) private def onLeftBrace(implicit ft: FormatToken, @@ -114,7 +125,8 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { else if (okToReplaceFunctionInSingleArgApply(t)) replace else removeToken case t: Term.Block => - if (okToReplaceBlockInSingleArgApply(t)) replace + if (getBlockNestedPartialFunction(t).isDefined) removeToken + else if (okToReplaceBlockInSingleArgApply(t)) replace else if (processBlock(t)) removeToken else null case _: Term.Interpolate => @@ -410,6 +422,23 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { case _ => okIfMultipleStats }) + private def getBlockNestedPartialFunction( + tree: Tree + ): Option[Term.PartialFunction] = tree match { + case x: Term.PartialFunction => Some(x) + case x: Term.Block => getBlockNestedPartialFunction(x) + case _ => None + } + + @tailrec + private def getBlockNestedPartialFunction( + tree: Term.Block + ): Option[Term.PartialFunction] = getBlockSingleStat(tree) match { + case Some(x: Term.PartialFunction) => Some(x) + case Some(x: Term.Block) => getBlockNestedPartialFunction(x) + case _ => None + } + private def getSingleStatIfLineSpanOk(b: Term.Block)(implicit style: ScalafmtConfig ): Option[Stat] = diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat index 22eb914c5f..ff2176c52b 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat @@ -1105,24 +1105,12 @@ 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 } } <<< single-block partial function with block one-arg apply object a { @@ -1147,22 +1135,10 @@ 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 } }