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

State: indicate if all alternatives were newlines #2103

Merged
merged 2 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
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 @@ -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 @@ -834,22 +834,22 @@ class FormatOps(

def noOptimizationZones(tree: Tree): Set[Token] = {
val result = Set.newBuilder[Token]
var inside = false
var expire = tree.tokens.head
var expire: Token = null
tree.tokens.foreach {
case t if !inside && ((t, ownersMap(hash(t))) match {
case (T.LeftParen(), _: Term.Apply | _: Init) =>
// TODO(olafur) https://github.com/scalameta/scalameta/issues/345
val x = true
x
// Type compounds can be inside defn.defs
case (T.LeftBrace(), Type.Refine(_, _)) => true
case _ => false
}) =>
inside = true
expire = matching(t)
case x if x == expire => inside = false
case x if inside => result += x
case x if expire ne null =>
if (x eq expire) expire = null else result += x
case t: T.LeftParen =>
owners(t) match {
// TODO(olafur) https://github.com/scalameta/scalameta/issues/345
case _: Term.Apply | _: Init => expire = matching(t)
case _ =>
}
case t: T.LeftBrace =>
owners(t) match {
// Type compounds can be inside defn.defs
case _: Type.Refine => expire = matching(t)
case _ =>
}
case _ =>
}
result.result()
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 @@ -1768,8 +1768,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