From e4ac81159c4117a440d9b499457fb597f12b4e34 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 6 Mar 2022 22:06:10 -0800 Subject: [PATCH] AvoidInfix: extract isWrapped, checkMatchingInfix Apply it both for primary infix expression as well as possible infix on the left-hand side. --- .../org/scalafmt/rewrite/AvoidInfix.scala | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 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 faaed64560..2c00345fe7 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 @@ -38,8 +38,7 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession { override def rewrite(tree: Tree): Unit = tree match { - case Term.ApplyInfix(lhs, op, _, args) - if InfixApp.isLeftAssoc(op.value) && matcher.matches(op.value) => + case x @ Term.ApplyInfix(lhs, op, _, args) if checkMatchingInfix(x) => val builder = Seq.newBuilder[TokenPatch] val opHead = op.tokens.head @@ -64,11 +63,7 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession { } val shouldWrapLhs = lhs match { - case Term.ApplyInfix(_, o, _, _) if !matcher.matches(o.value) && ! { - val head = lhs.tokens.head - head.is[Token.LeftParen] && - ctx.getMatchingOpt(head).contains(lhs.tokens.last) - } => + case y: Term.ApplyInfix if !isWrapped(y) && !checkMatchingInfix(y) => if (PlaceholderChecks.hasPlaceholder(lhs)) return true // foo _ compose bar => (foo _).compose(bar) @@ -100,4 +95,14 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession { case _ => } + private def checkMatchingInfix(ai: Term.ApplyInfix): Boolean = { + val op = ai.op.value + InfixApp.isLeftAssoc(op) && matcher.matches(op) + } + + private def isWrapped(t: Tree): Boolean = t.tokens.head match { + case h: Token.LeftParen => ctx.getMatchingOpt(h).contains(t.tokens.last) + case _ => false + } + }