Skip to content

Commit

Permalink
Split: keep 2 instances of FileLine, for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 16, 2024
1 parent 1b1d57d commit 4032d93
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.scalafmt.internal

import org.scalameta.FileLine

class FileLineStack private (
val fileLineHead: FileLine,
val fileLineLast: FileLine,
) extends Ordered[FileLineStack] {
override def toString: String = s"$fileLineHead->$fileLineLast"

def forThisLine(implicit
file: sourcecode.File,
line: sourcecode.Line,
): FileLineStack = new FileLineStack(
fileLineHead = fileLineHead,
fileLineLast = FileLine.generate,
)

override def compare(that: FileLineStack): Int = Integer
.compare(this.fileLineHead.line.value, that.fileLineHead.line.value)
}

object FileLineStack {
implicit def generate(implicit
file: sourcecode.File,
line: sourcecode.Line,
fileLineHead: FileLine,
): FileLineStack = new FileLineStack(fileLineHead, FileLine.generate)

def nextLine(implicit fl: FileLine): FileLine = {
val line = fl.line
new FileLine(fl.file, line.copy(value = line.value + 1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ class FormatOps(
.forThisLine(nextLine)
def getSplits(spaceSplit: Split, nlCost: Int = 1) = (
spaceSplit.withIndents(spaceIndents),
getNlSplit(1, nlCost)(nextLine(spaceSplit.fileLine)),
getNlSplit(1, nlCost)(spaceSplit.fileLine),
)
def getSlb(end: T, excl: TokenRanges)(implicit fileLine: FileLine) =
SingleLineBlock(end, exclude = excl, noSyntaxNL = true)
Expand Down Expand Up @@ -1938,7 +1938,7 @@ class FormatOps(
Seq(Split(Newline, 0).withIndent(indent).withPolicy(nlPolicy))
else Seq(
Split(Space, 0).withSingleLine(slbExpire.left).withIndent(indent),
Split(Newline, 1)(nextLine).withIndent(indent).withPolicy(nlPolicy),
Split(Newline, 1).withIndent(indent).withPolicy(nlPolicy),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,13 @@ class Router(formatOps: FormatOps) {
case _ => false
}
val bodyIsEmpty = isEmptyTree(body)
def baseSplit = Split(Space, 0)
def baseSplit(implicit l: FileLine) = Split(Space, 0)
def nlSplit(ft: FormatToken)(cost: Int)(implicit l: FileLine) =
Split(Newline2x(ft), cost)
CtrlBodySplits.checkComment(nlSplit(ft)) { nft =>
def withSlbSplit(implicit l: FileLine) = Seq(
baseSplit.withSingleLine(getLastNonTrivialToken(body)),
nlSplit(nft)(1)(nextLine),
nlSplit(nft)(1),
)
implicit val beforeMultiline = style.newlines.getBeforeMultiline
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ case class Split(
activeTags: Set[SplitTag] = Set.empty,
policy: Policy = NoPolicy,
optimalAt: Option[OptimalToken] = None,
)(implicit val fileLine: FileLine) {
)(implicit val fileLineStack: FileLineStack) {
import PolicyOps._

def withNoIndent: Split = mod match {
Expand All @@ -63,6 +63,9 @@ case class Split(
case _ => this
}

@inline
def fileLine: FileLine = fileLineStack.fileLineLast

@inline
def indentation: String = modExt.indentation

Expand Down Expand Up @@ -133,7 +136,8 @@ case class Split(
if (isIgnored) this else splitTag.fold(this)(preActivateFor)

def forThisLine(implicit fileLine: FileLine): Split =
if (isIgnored) this else copy()(fileLine = fileLine)
if (isIgnored) this
else copy()(fileLineStack.forThisLine(fileLine.file, fileLine.line))

def getCost(ifActive: Int => Int, ifIgnored: => Int): Int =
if (isIgnored) ifIgnored else ifActive(cost)
Expand Down Expand Up @@ -319,22 +323,23 @@ case class Split(
else ""
}
val opt = optimalAt.fold("")(", opt=" + _)
s"""$prefix${modExt
.mod}:[$fileLine](cost=$cost, indents=$indentation, $policy$opt)"""
s"""$prefix$mod:[$fileLineStack](cost=$cost, indents=$indentation, $policy$opt)"""
}
}

object Split {

private val ignoredTags = Set[SplitTag](null)

def ignored(implicit fileLine: FileLine) = Split(ModExt(NoSplit), 0).ignored
def ignored(implicit fileLineStack: FileLineStack) = Split(ModExt(NoSplit), 0)
.ignored

def apply(ignore: Boolean, cost: Int)(modExt: => ModExt)(implicit
fileLine: FileLine,
fileLineStack: FileLineStack,
): Split = if (ignore) ignored else Split(modExt, cost)

def opt(mod: Modification, cost: Int)(implicit fileLine: FileLine): Split =
if (mod eq null) ignored else Split(mod, cost)
def opt(mod: Modification, cost: Int)(implicit
fileLineStack: FileLineStack,
): Split = if (mod eq null) ignored else Split(mod, cost)

}
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,7 @@ object State {
private def compareSplitOrigin(s1: State, s2: State): Int = {
// We assume the same number of splits, see compareSplitsLength
// Break ties by the last split's line origin.
val r = Integer
.compare(s1.split.fileLine.line.value, s2.split.fileLine.line.value)
val r = s1.split.fileLineStack.compare(s2.split.fileLineStack)
if (r != 0 || s1.prev.depth == 0) r
else compareSplitOrigin(s1.prev, s2.prev)
}
Expand Down

0 comments on commit 4032d93

Please sign in to comment.