Skip to content

Commit

Permalink
Treat Pat.ExtractInfix like Term.ApplyInfix. Fixes #408
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Aug 22, 2016
1 parent a9c10e7 commit 75ce79d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
13 changes: 8 additions & 5 deletions core/src/main/scala/org/scalafmt/internal/FormatOps.scala
Expand Up @@ -364,26 +364,29 @@ class FormatOps(val tree: Tree,
lastToken(owners(getSelectsLastToken(lastDot)))
}

def infixSplit(owner: Term.ApplyInfix, formatToken: FormatToken)(
implicit line: sourcecode.Line): Split = {
def infixSplit(
owner: Tree,
op: Term.Name,
rhsArgs: Seq[Tree],
formatToken: FormatToken)(implicit line: sourcecode.Line): Split = {
val modification = newlines2Modification(
formatToken.between,
rightIsComment = formatToken.right.isInstanceOf[Comment])
val indent = {
if ((style.unindentTopLevelOperators ||
isTopLevelInfixApplication(owner)) &&
(style.indentOperatorsIncludeFilter
.findFirstIn(owner.op.tokens.head.syntax)
.findFirstIn(op.tokens.head.syntax)
.isEmpty ||
style.indentOperatorsExcludeFilter
.findFirstIn(owner.op.tokens.head.syntax)
.findFirstIn(op.tokens.head.syntax)
.isDefined)) 0
else if (!modification.isNewline &&
!isAttachedComment(formatToken.right, formatToken.between)) 0
else 2
}
val expire = (for {
arg <- owner.args.lastOption
arg <- rhsArgs.lastOption
token <- arg.tokens.lastOption
} yield token).getOrElse(owner.tokens.last)
Split(modification, 0).withIndent(Num(indent), expire, ExpiresOn.Left)
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/scala/org/scalafmt/internal/Router.scala
Expand Up @@ -11,6 +11,7 @@ import org.scalafmt.internal.Length.Num
import org.scalafmt.Error.UnexpectedTree
import org.scalafmt.internal.Policy.NoPolicy
import org.scalafmt.util.Delim
import org.scalafmt.util.InfixApplication
import org.scalafmt.util.Keyword
import org.scalafmt.util.Literal
import org.scalafmt.util.LoggerOps
Expand Down Expand Up @@ -1030,12 +1031,13 @@ class Router(formatOps: FormatOps) {
// Infix operator.
case tok @ FormatToken(op @ Ident(_), right, between)
if isApplyInfix(op, leftOwner) =>
val owner = leftOwner.parent.get.asInstanceOf[Term.ApplyInfix]
Seq(infixSplit(owner, formatToken))
// TODO(olafur) move extractor into pattern match.
val InfixApplication(_, op, args) = leftOwner.parent.get
Seq(infixSplit(leftOwner, op, args, formatToken))
case FormatToken(left, op @ Ident(_), between)
if isApplyInfix(op, rightOwner) =>
val owner = rightOwner.parent.get.asInstanceOf[Term.ApplyInfix]
Seq(infixSplit(owner, formatToken))
val InfixApplication(_, op, args) = rightOwner.parent.get
Seq(infixSplit(rightOwner, op, args, formatToken))

// Pat
case tok @ FormatToken(Ident("|"), _, _)
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/scala/org/scalafmt/util/TreeExtractors.scala
@@ -0,0 +1,14 @@
package org.scalafmt.util

import scala.collection.immutable.Seq
import scala.meta.Pat
import scala.meta.Term
import scala.meta.Tree

object InfixApplication {
def unapply(tree: Tree): Option[(Tree, Term.Name, Seq[Tree])] = tree match {
case infix: Term.ApplyInfix => Some((infix.lhs, infix.op, infix.args))
case infix: Pat.ExtractInfix => Some((infix.lhs, infix.ref, infix.rhs))
case _ => None
}
}
3 changes: 2 additions & 1 deletion core/src/main/scala/org/scalafmt/util/TreeOps.scala
Expand Up @@ -425,11 +425,12 @@ object TreeOps {
final def isApplyInfix(op: Token.Ident, owner: Tree): Boolean =
owner.parent.exists {
case infix: Term.ApplyInfix => infix.op == owner
case infix: Pat.ExtractInfix => infix.ref == owner
case _ => false
}

@tailrec
final def isTopLevelInfixApplication(child: Term.ApplyInfix): Boolean =
final def isTopLevelInfixApplication(child: Tree): Boolean =
child.parent match {
case Some(parent: Term.ApplyInfix) => isTopLevelInfixApplication(parent)
case Some(_: Term.Block | _: Term.If | _: Term.While | _: Source) => true
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/resources/default/ApplyInfix.stat
Expand Up @@ -310,3 +310,15 @@ object ExternForwarder {

>>>
x
<<< #408
1 match {
case accountIdStr :: userIdStr :: connectorIdStr :: timestampStr :: eventClassStr :: contactIdStr
:: contactPointIdStr :: contactPointValue :: remoteContactId :: to :: subject :: remoteId
:: remoteGroupId :: timeZoneStr :: rest =>
}
>>>
1 match {
case accountIdStr :: userIdStr :: connectorIdStr :: timestampStr :: eventClassStr :: contactIdStr
:: contactPointIdStr :: contactPointValue :: remoteContactId :: to :: subject :: remoteId
:: remoteGroupId :: timeZoneStr :: rest =>
}

0 comments on commit 75ce79d

Please sign in to comment.