Skip to content

Commit

Permalink
BestFirstSearch: use overridden ScalafmtConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Apr 28, 2024
1 parent 76e106c commit 1e21c26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ private class BestFirstSearch private (
import TokenOps._
import TreeOps._
import formatOps._
import formatOps.runner.optimizer._

implicit val stateOrdering: Ordering[State] = State.Ordering

Expand All @@ -47,7 +46,7 @@ private class BestFirstSearch private (
var deepestYet = State.start
val best = mutable.Map.empty[Int, State]
val visits = new Array[Int](tokens.length)
var keepSlowStates = !pruneSlowStates
var keepSlowStates = !initStyle.runner.optimizer.pruneSlowStates

type StateHash = Long

Expand All @@ -61,7 +60,7 @@ private class BestFirstSearch private (
private def getBlockCloseToRecurse(ft: FormatToken, stop: Token)(implicit
style: ScalafmtConfig,
): Option[Token] =
if (recurseOnBlocks) {
if (style.runner.optimizer.recurseOnBlocks) {
val prev = tokens.prev(ft)
getEndOfBlock(prev, false).filter { close =>
// Block must span at least 3 lines to be worth recursing.
Expand Down Expand Up @@ -124,19 +123,21 @@ private class BestFirstSearch private (
if (splitToken.right.start > stop.start && leftTok.start < leftTok.end)
return curr

val noOptZone = noOptZones == null || noOptZones.contains(leftTok)
implicit val style = styleMap.at(splitToken)
import style.runner.optimizer._

val noOptZone = noOptZones == null || !useNoOptZones ||
noOptZones.contains(leftTok)

if (noOptZone || shouldEnterState(curr)) {
trackState(curr, depth, Q.length)

if (explored > runner.maxStateVisits) throw SearchStateExploded(
if (explored > style.runner.maxStateVisits) throw SearchStateExploded(
deepestYet,
formatWriter.mkString(deepestYet),
tokens(deepestYet.depth),
)

implicit val style = styleMap.at(splitToken)

if (curr.split != null && curr.split.isNL) {
val tokenHash = hash(leftTok)
if (
Expand Down Expand Up @@ -173,7 +174,7 @@ private class BestFirstSearch private (
val updateBest = !keepSlowStates && depth == 0 && split.isNL &&
!best.contains(curr.depth)
if (updateBest) best.update(curr.depth, nextState)
runner.event(Enqueue(split))
style.runner.event(Enqueue(split))
split.optimalAt match {
case Some(OptimalToken(token, killOnFail))
if acceptOptimalAtHints && optimalNotFound &&
Expand Down Expand Up @@ -236,12 +237,14 @@ private class BestFirstSearch private (
splits.sortBy(_.cost)
}

private def trackState(state: State, depth: Int, queueSize: Int): Unit = {
private def trackState(state: State, depth: Int, queueSize: Int)(implicit
style: ScalafmtConfig,
): Unit = {
if (state.depth > deepestYet.depth) deepestYet = state
runner.event(VisitToken(tokens(state.depth)))
style.runner.event(VisitToken(tokens(state.depth)))
visits(state.depth) += 1
explored += 1
runner.event(Explored(explored, depth, queueSize))
style.runner.event(Explored(explored, depth, queueSize))
}

/** Follow states having single active non-newline split
Expand All @@ -250,6 +253,7 @@ private class BestFirstSearch private (
private def traverseSameLine(state: State, depth: Int): State =
if (state.depth >= tokens.length) state
else {
implicit val style = styleMap.at(tokens(state.depth))
trackState(state, depth, 0)
val activeSplits = getActiveSplits(state, Int.MaxValue)

Expand All @@ -258,16 +262,15 @@ private class BestFirstSearch private (
val split = activeSplits.head
if (split.isNL) state
else {
runner.event(Enqueue(split))
implicit val style = styleMap.at(tokens(state.depth))
style.runner.event(Enqueue(split))
val nextState = state.next(split, false)
traverseSameLine(nextState, depth)
}
}
}

private def complete(state: State): Unit = runner
.event(CompleteFormat(explored, state, visits))
private def complete(state: State)(implicit style: ScalafmtConfig): Unit =
style.runner.event(CompleteFormat(explored, state, visits))

def getBestPath: SearchResult = {
val state = {
Expand All @@ -281,7 +284,7 @@ private class BestFirstSearch private (
}
}
if (null != state) {
complete(state)
complete(state)(initStyle)
SearchResult(state, reachedEOF = true)
} else {
val nextSplits = routes(deepestYet.depth)
Expand All @@ -295,11 +298,11 @@ private class BestFirstSearch private (
|policies=${deepestYet.policy.policies}
|nextSplits=$nextSplits
|splitsAfterPolicy=$splitsAfterPolicy""".stripMargin
if (runner.debug) logger.debug(
if (initStyle.runner.debug) logger.debug(
s"""|Failed to format
|$msg""".stripMargin,
)
complete(deepestYet)
complete(deepestYet)(initStyle)
SearchResult(deepestYet, reachedEOF = false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.scalafmt.config.IndentOperator
import org.scalafmt.config.Indents
import org.scalafmt.config.Newlines
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.config.ScalafmtRunner
import org.scalafmt.config.TrailingCommas
import org.scalafmt.internal.Length.Num
import org.scalafmt.internal.Policy.NoPolicy
Expand Down Expand Up @@ -37,7 +36,6 @@ class FormatOps(
private[internal] val (initStyle, ownersMap) =
getStyleAndOwners(topSourceTree, baseStyle)

val runner: ScalafmtRunner = initStyle.runner
implicit val dialect: Dialect = initStyle.dialect
implicit val (tokens: FormatTokens, styleMap: StyleMap) =
FormatTokens(topSourceTree.tokens, owners)(initStyle)
Expand Down Expand Up @@ -1037,12 +1035,12 @@ class FormatOps(
}

def getForceConfigStyle: (Set[TokenHash], Set[TokenHash]) = {
val maxDistance = runner.optimizer.forceConfigStyleMinSpan
val maxDistance = initStyle.runner.optimizer.forceConfigStyleMinSpan
if (maxDistance < 0) (Set.empty, Set.empty)
else {
val clearQueues = Set.newBuilder[TokenHash]
val forces = Set.newBuilder[TokenHash]
val minArgs = runner.optimizer.forceConfigStyleMinArgCount
val minArgs = initStyle.runner.optimizer.forceConfigStyleMinArgCount
def process(args: Seq[Tree], open: T, close: T): Unit =
if (
args.lengthCompare(minArgs) >= 0 &&
Expand Down

0 comments on commit 1e21c26

Please sign in to comment.