Skip to content

Commit

Permalink
Use FormatToken in policies, indents etc.
Browse files Browse the repository at this point in the history
This way, the checks are more precise, and we reduce the probability of
comparing with a token that has been removed.
  • Loading branch information
kitbellew committed Nov 13, 2024
1 parent 7e46a26 commit 09fc3af
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 579 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ private class BestFirstSearch private (range: Set[Range])(implicit

var stats = new StateStats(tokens, initStyle.runner)

private def getBlockCloseToRecurse(ft: FT, stop: T)(implicit
private def getBlockCloseToRecurse(ft: FT, stop: Int)(implicit
style: ScalafmtConfig,
): Option[T] = getEndOfBlock(ft, parensToo = true).collect {
case close if close.left != stop && {
): Option[Int] = getEndOfBlock(ft, parensToo = true).collect {
case close if close.idx < stop && {
// Block must span at least 3 lines to be worth recursing.
tokens.width(ft, close) > style.maxColumn * 3
} => close.left
} => close.idx
}

private val memo = mutable.Map.empty[Long, Option[State]]

def shortestPathMemo(
start: State,
stop: T,
stop: Int,
depth: Int,
isOpt: Boolean,
): Option[Option[State]] = {
Expand All @@ -69,7 +69,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit
*/
def shortestPath(
start: State,
stop: T,
stop: Int,
depth: Int = 0,
isOpt: Boolean = false,
): Either[State, State] = {
Expand All @@ -85,8 +85,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit

val splitToken = tokens(idx)
val leftTok = splitToken.left
if (splitToken.right.start > stop.start && leftTok.start < leftTok.end)
return Right(curr)
if (splitToken.idx >= stop && !leftTok.isEmpty) return Right(curr)

implicit val style = styleMap.at(splitToken)
import style.runner.optimizer
Expand Down Expand Up @@ -187,7 +186,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit
) tokens(nextNextState.depth)
else {
val optEnd = end
if (optEnd ne null) optEnd else tokens(opt.token)
if (optEnd ne null) optEnd else opt.token
}
}

Expand All @@ -196,18 +195,17 @@ private class BestFirstSearch private (range: Set[Range])(implicit
queue: StateQueue,
style: ScalafmtConfig,
): Either[State, State] = {
val optEnd = tokens(opt.token)
val nextNextState =
if (opt.token.end <= tokens(nextState.depth).left.end) nextState
if (opt.token.idx <= nextState.depth) nextState
else if (
tokens.width(tokens(nextState.depth), optEnd) > 3 * style.maxColumn
) return Left(killOnFail(opt.killOnFail)(optEnd))
tokens.width(tokens(nextState.depth), opt.token) > 3 * style.maxColumn
) return Left(killOnFail(opt.killOnFail)(opt.token))
else {
val res =
shortestPath(nextState, opt.token, queue.nested + 1, isOpt = true)
shortestPath(nextState, opt.token.idx, queue.nested + 1, isOpt = true)
res match {
case Right(x) => x
case Left(x) => return Left(killOnFail(opt, optEnd, x))
case Left(x) => return Left(killOnFail(opt, opt.token, x))
}
}
def checkPenalty(state: State, orElse: => Either[State, State]) =
Expand All @@ -216,7 +214,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit
else orElse
traverseSameLine(nextNextState) match {
case x @ Left(s) =>
if (s eq null) Left(killOnFail(opt, optEnd, nextNextState))
if (s eq null) Left(killOnFail(opt, opt.token, nextNextState))
else checkPenalty(s, x)
case x @ Right(s) => checkPenalty(s, if (opt.recurseOnly) Left(s) else x)
}
Expand Down Expand Up @@ -270,8 +268,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit
def getBestPath: SearchResult = {
initStyle.runner.event(FormatEvent.Routes(routes))
val state = {
val endToken = topSourceTree.tokens.last
def run = shortestPath(State.start, endToken)
def run = shortestPath(State.start, Int.MaxValue)
run.getOrElse {
stats.retry.flatMap { x =>
stats = x
Expand Down
Loading

0 comments on commit 09fc3af

Please sign in to comment.