Skip to content

Commit

Permalink
State: indicate if all alternatives were newlines
Browse files Browse the repository at this point in the history
In BestFirstSearch, let's only start a new generation on a newline split
if all its alternatives were also newlines.

Makes formatting of [partial] function parameters more consistent.
  • Loading branch information
kitbellew committed Jul 20, 2020
1 parent 5bb950d commit 2329133
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private class BestFirstSearch private (
val tokenHash = hash(splitToken.left)
if (
emptyQueueSpots.contains(tokenHash) ||
dequeueOnNewStatements &&
dequeueOnNewStatements && curr.allAltAreNL &&
dequeueSpots.contains(tokenHash) &&
(depth > 0 || !isInsideNoOptZone(splitToken))
)
Expand All @@ -191,10 +191,11 @@ private class BestFirstSearch private (
)
} else {
val actualSplit = getActiveSplits(curr, maxCost)
val allAltAreNL = actualSplit.forall(_.isNL)

var optimalNotFound = true
actualSplit.foreach { split =>
val nextState = curr.next(split)
val nextState = curr.next(split, allAltAreNL)
val updateBest = !keepSlowStates && depth == 0 &&
split.isNL && !best.contains(curr.depth)
if (updateBest) {
Expand Down Expand Up @@ -290,7 +291,7 @@ private class BestFirstSearch private (
else {
runner.event(Enqueue(split))
implicit val style = styleMap.at(tokens(state.depth))
val nextState = state.next(split)
val nextState = state.next(split, false)
traverseSameLine(nextState, depth)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final case class State(
indentation: Int,
pushes: Seq[ActualIndent],
column: Int,
allAltAreNL: Boolean,
delayedPenalty: Int, // apply if positive, ignore otherwise
formatOff: Boolean
) {
Expand All @@ -37,7 +38,8 @@ final case class State(
* Calculates next State given split at tok.
*/
def next(
initialNextSplit: Split
initialNextSplit: Split,
nextAllAltAreNL: Boolean
)(implicit style: ScalafmtConfig, fops: FormatOps): State = {
val tok = fops.tokens(depth)
val right = tok.right
Expand Down Expand Up @@ -117,6 +119,7 @@ final case class State(
nextIndent,
nextIndents,
nextStateColumn,
nextAllAltAreNL,
nextDelayedPenalty,
nextFormatOff
)
Expand Down Expand Up @@ -270,6 +273,7 @@ object State {
0,
Seq.empty,
0,
false,
0,
formatOff = false
)
Expand Down
3 changes: 1 addition & 2 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1780,8 +1780,7 @@ val a = b match {
}
>>>
val a = b match {
case i: EngineInstance =>
JObject(
case i: EngineInstance => JObject(
JField("id", JString(i.id)) :: JField("status", JString(i.status)) ::
JField("startTime", JString(i.startTime.toString)) ::
JField("endTime", JString(i.endTime.toString)) ::
Expand Down

0 comments on commit 2329133

Please sign in to comment.