Skip to content

Commit

Permalink
Rewrite: use scalameta PlaceholderChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 21, 2022
1 parent caeb521 commit 638b497
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalafmt.rewrite

import scala.meta._
import scala.meta.internal.trees.PlaceholderChecks

import org.scalafmt.config.FilterMatcher
import org.scalafmt.config.RewriteSettings
Expand Down Expand Up @@ -50,7 +51,7 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession {
val last = args.head.tokens.last
val opLast = op.tokens.last
if (!ctx.isMatching(head, last)) {
if (RewriteCtx.hasPlaceholder(args.head)) return
if (PlaceholderChecks.hasPlaceholder(args.head)) return
builder += TokenPatch.AddRight(opLast, "(", keepTok = true)
builder += TokenPatch.AddRight(last, ")", keepTok = true)
} else {
Expand All @@ -68,7 +69,7 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession {
head.is[Token.LeftParen] &&
ctx.getMatchingOpt(head).contains(lhs.tokens.last)
} =>
if (RewriteCtx.hasPlaceholder(lhs)) return
if (PlaceholderChecks.hasPlaceholder(lhs)) return
true
// foo _ compose bar => (foo _).compose(bar)
// new Foo compose bar => (new Foo).compose(bar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {

case p: Term.ApplyInfix =>
stat match {
case t: Term.ApplyInfix if !RewriteCtx.hasPlaceholder(t) =>
case t: Term.ApplyInfix =>
val useRight = isSingleElement(p.args, b)
SyntacticGroupOps.groupNeedsParenthesis(
TreeSyntacticGroup(p),
Expand Down Expand Up @@ -492,9 +492,8 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {

// special case for Select which might contain a space instead of dot
private def isPrefixExpr(expr: Tree): Boolean =
RewriteCtx.isSimpleExprOr(expr) {
case t: Term.Select if !RewriteCtx.hasPlaceholder(expr) =>
ftoks(t.name.tokens.head, -1).left.is[Token.Dot]
RewriteCtx.isSimpleExprOr(expr) { case t: Term.Select =>
ftoks(t.name.tokens.head, -1).left.is[Token.Dot]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class RedundantParens(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {

case _: Lit | _: Name | _: Term.Interpolate => true

case Term.Apply(f, List(b @ (_: Term.Block | _: Term.PartialFunction)))
case Term.Apply(_, List(b @ (_: Term.Block | _: Term.PartialFunction)))
if b.tokens.headOption.exists(_.is[Token.LeftBrace]) =>
!RewriteCtx.hasPlaceholder(f)
true

case _: Term.PartialFunction => true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.scalafmt.rewrite

import scala.annotation.tailrec
import scala.collection.mutable

import metaconfig.ConfCodecEx
Expand Down Expand Up @@ -85,13 +84,12 @@ case class RewriteCtx(

// special case for Select which might contain a space instead of dot
def isPrefixExpr(expr: Tree): Boolean =
RewriteCtx.isSimpleExprOr(expr) {
case t: Term.Select if !RewriteCtx.hasPlaceholder(expr) =>
val maybeDot = tokenTraverser.findBefore(t.name.tokens.head) {
case Trivia() => None
case x => Some(x.is[Token.Dot])
}
maybeDot.isDefined
RewriteCtx.isSimpleExprOr(expr) { case t: Term.Select =>
val maybeDot = tokenTraverser.findBefore(t.name.tokens.head) {
case Trivia() => None
case x => Some(x.is[Token.Dot])
}
maybeDot.isDefined
}

}
Expand Down Expand Up @@ -185,42 +183,16 @@ object RewriteCtx {
)(orElse: PartialFunction[Tree, Boolean]): Boolean =
expr match {
case _: Lit | _: Name | _: Term.Interpolate => true
case _: Term.New | _: Term.NewAnonymous => !hasPlaceholder(expr)
case _: Term.Apply | _: Term.ApplyUnary => !hasPlaceholder(expr)
case _: Term.New | _: Term.NewAnonymous => true
case _: Term.Apply | _: Term.ApplyUnary => true
case _ => orElse.applyOrElse(expr, (_: Tree) => false)
}

@inline
def isPostfixExpr(expr: Tree)(implicit style: ScalafmtConfig): Boolean =
isSimpleExprOr(expr) {
case _: Term.Select | _: Term.ApplyInfix =>
!hasPlaceholder(expr)
case _: Term.Match if style.dialect.allowMatchAsOperator =>
!hasPlaceholder(expr)
case _: Term.Select | _: Term.ApplyInfix => true
case _: Term.Match if style.dialect.allowMatchAsOperator => true
}

def hasPlaceholder(expr: Tree): Boolean = {
val queue = new mutable.Queue[Tree]
queue += expr
@tailrec
def iter: Boolean =
queue.nonEmpty && {
queue.dequeue() match {
case _: Term.Placeholder => true
case t: Term.ApplyInfix =>
queue ++= (t.lhs +: t.args)
iter
case t: Term.Apply =>
queue ++= (t.fun +: t.args)
iter
case t: Term.Select =>
queue += t.qual
iter
case _ =>
iter
}
}
iter
}

}
2 changes: 1 addition & 1 deletion scalafmt-tests/src/test/resources/rewrite/AvoidInfix.stat
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object a {
_ compareTo _
_ compareTo _.bar
_.compareTo(_.bar)
_.compareTo((_: Int) * 2)
_ compareTo (_: Int) * 2
list.foo(_ fun _.bar)
list.foo(_.fun(_.bar))
_.foo bar _.baz
Expand Down

0 comments on commit 638b497

Please sign in to comment.