Skip to content

Commit

Permalink
Infix applications to count towards valign depth, fixes #531
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Oct 23, 2016
1 parent c2e952a commit 5fb7927
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
18 changes: 18 additions & 0 deletions core/src/main/scala/org/scalafmt/internal/FormatOps.scala
Expand Up @@ -43,6 +43,7 @@ class FormatOps(val tree: Tree, val initStyle: ScalafmtConfig) {
val dequeueSpots = getDequeueSpots(tree) ++ statementStarts.keys
val matchingParentheses = getMatchingParentheses(tree.tokens)
val styleMap = new StyleMap(tokens, initStyle)
private val vAlignDepthCache = mutable.Map.empty[Tree, Int]

@inline
def owners(token: Token): Tree = ownersMap(hash(token))
Expand Down Expand Up @@ -551,4 +552,21 @@ class FormatOps(val tree: Tree, val initStyle: ScalafmtConfig) {
case d @ Decision(t @ FormatToken(_, `close`, _), s) =>
d.onlyNewlines
}, close.end)

// Returns the depth of this node in the AST, used to prevent false positives.
final def vAlignDepth(tree: Tree): Int = {
vAlignDepthCache.getOrElseUpdate(tree, vAlignDepthUnCached(tree))
}

private final def vAlignDepthUnCached(tree: Tree): Int = {
val count: Int = tree match {
// infix applications don't count towards the length, context #531
case _: Term.ApplyInfix => 0
case _ => 1
}
tree.parent match {
case Some(parent) => count + vAlignDepth(parent)
case None => count
}
}
}
Expand Up @@ -216,7 +216,7 @@ class FormatWriter(formatOps: FormatOps) {
val row2Owner = getAlignOwner(row2.formatToken)
val row1Owner = getAlignOwner(row1.formatToken)
def sameLengthToRoot =
lengthToRoot(row1Owner) == lengthToRoot(row2Owner)
vAlignDepth(row1Owner) == vAlignDepth(row2Owner)
key(row1.formatToken.right) == key(row2.formatToken.right) &&
sameLengthToRoot && {
val eofParents = parents(owners(endOfLine.right))
Expand Down
6 changes: 0 additions & 6 deletions core/src/main/scala/org/scalafmt/util/TreeOps.scala
Expand Up @@ -31,12 +31,6 @@ object TreeOps {
import LoggerOps._
import TokenOps._

@tailrec
final def lengthToRoot(tree: Tree, accum: Int = 0): Int = tree.parent match {
case Some(parent) => lengthToRoot(parent, accum + 1)
case None => 1 + accum
}

def getEnumStatements(enums: Seq[Enumerator]): Seq[Enumerator] = {
val ret = Seq.newBuilder[Enumerator]
enums.zipWithIndex.foreach {
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/resources/align/AlignTokens.stat
Expand Up @@ -401,3 +401,15 @@ object M {
object M {
implicit class TimestampOps[@specialized(Int, Long) A](val i: A) {}
}
<<< #531
Seq(
"com.github.seratch" %% "awscala" % "0.5.7",
"org.scalatest" % "scalatest_2.11" % "3.0.0" % Test,
"org.scalamock" %% "scalamock-scalatest-support" % "3.2.2" % Test
)
>>>
Seq(
"com.github.seratch" %% "awscala" % "0.5.7",
"org.scalatest" % "scalatest_2.11" % "3.0.0" % Test,
"org.scalamock" %% "scalamock-scalatest-support" % "3.2.2" % Test
)

0 comments on commit 5fb7927

Please sign in to comment.