Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BestFirstSearch: remove a useless optimization #4421

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,43 +242,11 @@ private class BestFirstSearch private (range: Set[Range])(implicit
case Seq() => Left(null) // dead end if empty
case Seq(split) =>
if (split.isNL) Right(state)
else {
implicit val nextState: State = getNext(state, split)
traverseSameLine(nextState)
}
case ss
if state.appliedPenalty == 0 &&
RightParenOrBracket(splitToken.right) =>
traverseSameLineZeroCost(ss, state)
else traverseSameLine(getNext(state, split))
case ss => stateAsOptimal(state, ss).toRight(state)
}
}

@tailrec
private def traverseSameLineZeroCost(
splits: Seq[Split],
state: State,
nlState: => Option[State] = None,
)(implicit style: ScalafmtConfig, queue: StateQueue): Either[State, State] = {
def newNlState: Option[State] = stateAsOptimal(state, splits).orElse(nlState)
splits.filter(_.costWithPenalty == 0) match {
case Seq(split) if !split.isNL =>
val nextState: State = getNext(state, split)
if (nextState.split.costWithPenalty > 0) newNlState.toRight(state)
else if (nextState.depth >= tokens.length) Right(nextState)
else {
val nextToken = tokens(nextState.depth)
if (RightParenOrBracket(nextToken.right)) {
implicit val style: ScalafmtConfig = styleMap.at(nextToken)
val nextSplits =
getActiveSplits(nextToken, nextState, maxCost = Int.MaxValue)
traverseSameLineZeroCost(nextSplits, nextState, newNlState)
} else newNlState.toRight(nextState)
}
case _ => newNlState.toRight(state)
}
}

def getBestPath: SearchResult = {
initStyle.runner.event(FormatEvent.Routes(routes))
val state = {
Expand Down
Loading