Skip to content

Commit

Permalink
State: keep explicit penalty to detect overflow
Browse files Browse the repository at this point in the history
Using cost and delayed penalty proved to be insufficient.
  • Loading branch information
kitbellew committed Dec 28, 2021
1 parent 96736fc commit 0ec390e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ private class BestFirstSearch private (
if (null == nextNextState) null
else traverseSameLine(nextNextState, depth)
if (null != furtherState) {
val delta = furtherState.totalCost - nextNextState.totalCost
if (delta > Constants.ExceedColumnPenalty)
val overflow =
furtherState.appliedPenalty > nextNextState.appliedPenalty
if (overflow)
Q.enqueue(nextNextState)
else {
optimalNotFound = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.meta.tokens.Token

import org.scalafmt.util.LoggerOps

class PolicySummary(val policies: Seq[Policy]) {
class PolicySummary(val policies: Seq[Policy]) extends AnyVal {
import LoggerOps._

@inline def noDequeue = policies.exists(_.noDequeue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final case class State(
pushes: Seq[ActualIndent],
column: Int,
allAltAreNL: Boolean,
appliedPenalty: Int, // penalty applied from overflow
delayedPenalty: Int // apply if positive, ignore otherwise
) {

Expand Down Expand Up @@ -117,6 +118,7 @@ final case class State(
nextIndents,
nextStateColumn,
nextAllAltAreNL,
appliedPenalty + penalty,
nextDelayedPenalty
)
}
Expand Down Expand Up @@ -258,7 +260,6 @@ final case class State(
}
}

def totalCost: Int = cost + math.max(0, delayedPenalty)
}

object State {
Expand All @@ -273,6 +274,7 @@ object State {
Seq.empty,
0,
false,
0,
0
)

Expand Down
3 changes: 2 additions & 1 deletion scalafmt-tests/src/test/resources/default/String.stat
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ object a {
>>>
object a {
intercept[TestException] {
val ct = Thread.currentThread() // Ensure that the thunk is executed in the tests thread
val ct =
Thread.currentThread() // Ensure that the thunk is executed in the tests thread
val ct =
Thread.currentThread() // Ensure that the thunk is executed in the tests thread
}
Expand Down
4 changes: 2 additions & 2 deletions scalafmt-tests/src/test/resources/scalajs/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,8 @@ object a {
>>>
object a {
@tailrec
def constructOptimized(
revAlts: List[(js.Tree, js.Tree)], elsep: js.Tree): js.Tree = {
def constructOptimized(revAlts: List[(js.Tree, js.Tree)],
elsep: js.Tree): js.Tree = {
revAlts match {
case foo =>
// cannot use flatMap due to tailrec
Expand Down

0 comments on commit 0ec390e

Please sign in to comment.