From 8c2dd6de344105fbaa19c596507b4809edd40199 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 9 Sep 2024 00:37:15 -0700 Subject: [PATCH] BestFirstSearch: invert {always -> possibly}Better --- .../main/scala/org/scalafmt/config/ScalafmtOptimizer.scala | 6 +++--- .../main/scala/org/scalafmt/internal/BestFirstSearch.scala | 2 +- .../shared/src/main/scala/org/scalafmt/internal/State.scala | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtOptimizer.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtOptimizer.scala index b52d4c0de2..928e2ae0fd 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtOptimizer.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtOptimizer.scala @@ -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 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala index be23e6ff9f..d79462cdb6 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/BestFirstSearch.scala @@ -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, diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala index 4aca682118..bcdea6f75d 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/State.scala @@ -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. */