Skip to content

Commit

Permalink
BestFirstSearch: invert {always -> possibly}Better
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 12, 2024
1 parent eeff171 commit 8c2dd6d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import metaconfig._
* @param pruneSlowStates
* Eliminate solutions that move slower than other solutions.
* - If a solution reaches a point X first and other solution that reaches
* the same point later, the first solution is preferred if it can be
* verified to be always better (see
* [[org.scalafmt.internal.State.alwaysBetter]]).
* the same point later, the second solution is ignored unless it can be
* verified not to be worse (see
* [[org.scalafmt.internal.State.possiblyBetter]]).
* - Note. This affects the output positively because it breaks a tie between
* two equally expensive solutions by eliminating the slower one.
* - Example: solution 1 is preferred even though both solutions cost the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit
def shouldEnterState(curr: State): Boolean = keepSlowStates ||
curr.policy.noDequeue ||
// TODO(olafur) document why/how this optimization works.
!best.get(curr.depth).exists(_.alwaysBetter(curr))
best.get(curr.depth).forall(curr.possiblyBetter)

private def getBlockCloseToRecurse(ft: FormatToken, stop: Token)(implicit
style: ScalafmtConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ final case class State(
@inline
def mod: Modification = modExt.mod

def alwaysBetter(other: State): Boolean = this.cost <= other.cost &&
this.indentation <= other.indentation
def possiblyBetter(other: State): Boolean = this.cost < other.cost ||
this.indentation < other.indentation

/** Calculates next State given split at tok.
*/
Expand Down

0 comments on commit 8c2dd6d

Please sign in to comment.