Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AvoidInfix: refactor, use :: match #3147

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import scala.meta.internal.trees.PlaceholderChecks
import org.scalafmt.config.FilterMatcher
import org.scalafmt.config.RewriteSettings
import org.scalafmt.util.InfixApp
import org.scalafmt.util.TreeOps

object AvoidInfix extends RewriteFactory {

Expand Down Expand Up @@ -45,22 +44,23 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession {
val opHead = op.tokens.head
builder += TokenPatch.AddLeft(opHead, ".", keepTok = true)

if (TreeOps.isSeqSingle(args)) { // otherwise, definitely enclosed
val rhs = args.head
rhs.tokens.headOption.foreach { head =>
val last = args.head.tokens.last
val opLast = op.tokens.last
if (!ctx.isMatching(head, last)) {
if (PlaceholderChecks.hasPlaceholder(args.head)) return
builder += TokenPatch.AddRight(opLast, "(", keepTok = true)
builder += TokenPatch.AddRight(last, ")", keepTok = true)
} else {
// move delimiter (before comment or newline)
builder +=
TokenPatch.AddRight(opLast, head.syntax, keepTok = true)
builder += TokenPatch.Remove(head)
args match {
case rhs :: Nil =>
rhs.tokens.headOption.foreach { head =>
val last = rhs.tokens.last
val opLast = op.tokens.last
if (!ctx.isMatching(head, last)) {
if (PlaceholderChecks.hasPlaceholder(rhs)) return
builder += TokenPatch.AddRight(opLast, "(", keepTok = true)
builder += TokenPatch.AddRight(last, ")", keepTok = true)
} else {
// move delimiter (before comment or newline)
builder +=
TokenPatch.AddRight(opLast, head.syntax, keepTok = true)
builder += TokenPatch.Remove(head)
}
}
}
case _ => // otherwise, definitely enclosed
}

val shouldWrapLhs = lhs match {
Expand Down