Skip to content

Commit

Permalink
Split: block optimal token with non-zero cost
Browse files Browse the repository at this point in the history
The BestFirstSearch algorithm doesn't allow this anyway.
  • Loading branch information
kitbellew committed May 19, 2024
1 parent eae83cd commit f11d206
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,8 @@ class FormatOps(
if (breakMany) TokenRanges.empty
else insideBracesBlock(nextFT, expire, true)
Split(ModExt(newStmtMod.getOrElse(spaceMod)), cost)
.withSingleLine(expire, exclude)
.withSingleLineNoOptimal(expire, exclude)
.withOptimalToken(expire, ignore = cost != 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ class Router(formatOps: FormatOps) {
Seq(
Split(Newline, nestedPenalty + Constants.ExceedColumnPenalty)
.withPolicy(newlinePolicy).withIndent(indent, close, Before),
Split(NoSplit, nestedPenalty).withSingleLine(breakToken)
Split(NoSplit, nestedPenalty).withSingleLineNoOptimal(breakToken)
.andPolicy(newlinePolicy & newlineAfterAssignDecision),
)
}
Expand Down Expand Up @@ -1276,7 +1276,7 @@ class Router(formatOps: FormatOps) {
else NewlineT(alt = if (singleLineOnly) Some(NoSplit) else None)
val nlSplit = Split(nlMod, bracketPenalty * (if (oneline) 4 else 2))
.withIndent(indent)
.withSingleLineOpt(if (singleLineOnly) Some(close) else None)
.withSingleLineNoOptimal(close, ignore = !singleLineOnly)
.andPolicy(nlPolicy & penalizeNewlinesPolicy, singleLineOnly)
.andPolicyOpt(singleArgAsInfix.map(InfixSplits(_, ft).nlPolicy))
Seq(noSplit, nlSplit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,17 @@ case class Split(
): Split =
if (ignore) this else withOptimalAt(Some(OptimalToken(token, killOnFail)))

def withOptimalAt(optimalAt: => Option[OptimalToken]): Split = {
def withOptimalAt(fOptimalAt: => Option[OptimalToken]): Split = {
require(this.optimalAt.isEmpty)
if (isIgnored) this else copy(optimalAt = optimalAt)
if (isIgnored) this
else {
val optimalAt = fOptimalAt
if (optimalAt.isEmpty) this
else {
require(cost == 0, s"can't set optimal, cost=$cost")
copy(optimalAt = optimalAt)
}
}
}

def withPolicy(newPolicy: => Policy, ignore: Boolean = false): Split =
Expand All @@ -168,15 +176,6 @@ case class Split(
rank,
)

def withSingleLineOpt(
expire: Option[Token],
exclude: => TokenRanges = TokenRanges.empty,
noSyntaxNL: Boolean = false,
killOnFail: Boolean = false,
rank: Int = 0,
)(implicit fileLine: FileLine, style: ScalafmtConfig): Split = expire
.fold(this)(withSingleLine(_, exclude, noSyntaxNL, killOnFail, rank))

def withSingleLineAndOptimal(
expire: Token,
optimal: Token,
Expand All @@ -193,8 +192,10 @@ case class Split(
exclude: => TokenRanges = TokenRanges.empty,
noSyntaxNL: Boolean = false,
rank: Int = 0,
ignore: Boolean = false,
)(implicit fileLine: FileLine, style: ScalafmtConfig): Split = withPolicy(
SingleLineBlock(expire, exclude, noSyntaxNL = noSyntaxNL, rank = rank),
ignore = ignore,
)

def withPolicyOpt(newPolicy: => Option[Policy]): Split =
Expand Down

0 comments on commit f11d206

Please sign in to comment.