From 1f7194f1d28d01f2903845939d2bd5d49b168458 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 31 May 2022 10:00:51 -0700 Subject: [PATCH 1/2] AvoidInfix: test enclosed with RedundantParens --- .../src/test/resources/rewrite/AvoidInfix.stat | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat b/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat index 588748ee9a..c4c1767f11 100644 --- a/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat +++ b/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat @@ -410,3 +410,19 @@ rewrite.neverInfix.includeFilters = [ "[^*]+" ] foo baz (_) >>> foo.baz(_) +<<< enclosed in parens on separate lines, with RedundantParens +rewrite.rules = [AvoidInfix, RedundantParens] +=== +object a { + val foo = bar + ( + a baz b + ) +} +>>> +object a { + val foo = bar + + a.baz(b) + +} From 2b5a14653a1aed1b3ea810dd250f14eef9f035cb Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 1 Jun 2022 06:48:43 -0700 Subject: [PATCH 2/2] AvoidInfix: don't duplicate RedundantParens logic --- .../scala/org/scalafmt/rewrite/AvoidInfix.scala | 14 -------------- .../src/test/resources/rewrite/AvoidInfix.stat | 2 -- 2 files changed, 16 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/AvoidInfix.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/AvoidInfix.scala index 83eeb2663f..8559703017 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/AvoidInfix.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/AvoidInfix.scala @@ -93,20 +93,6 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession { builder += TokenPatch.AddRight(lhsLast, ")", keepTok = true) } - // remove parens if enclosed - for { - parent <- tree.parent - if !parent.is[Term.ApplyInfix] && !parent.is[Term.Apply] - if ctx.style.rewrite.rules.contains(RedundantParens) - infixTokens = tree.tokens - head <- infixTokens.headOption if head.is[Token.LeftParen] - last <- infixTokens.lastOption if last.is[Token.RightParen] - if ctx.isMatching(head, last) - } yield { - builder += TokenPatch.Remove(head) - builder += TokenPatch.Remove(last) - } - ctx.addPatchSet(builder.result(): _*) case _ => diff --git a/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat b/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat index c4c1767f11..5bc2c84a68 100644 --- a/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat +++ b/scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat @@ -422,7 +422,5 @@ object a { >>> object a { val foo = bar - a.baz(b) - }