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

Split: add implicit penalizeNL and penalizeIf #4598

Merged
merged 1 commit into from
Nov 25, 2024
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 @@ -1410,7 +1410,7 @@ class FormatOps(
private object CallSite {

private val penalizeOpenNL: Policy.Pf = { case Decision(_, s) =>
s.map(x => if (x.isNL) x.withPenalty(1) else x)
s.penalizeNL(1)
}

@tailrec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ class Router(formatOps: FormatOps) {
}
else if (!style.newlines.sourceIgnored && !isTripleQuote(m.left.text))
Policy.onLeft(end, "INTERP-KEEP-NONL") {
case Decision(x, ss) if x.noBreak =>
ss.map(s => if (s.isNL) s.withPenalty(penalty) else s)
case Decision(x, ss) if x.noBreak => ss.penalizeNL(penalty)
}
else Policy ?
(style.newlines.inInterpolation eq
Newlines.InInterpolation.allow) &&
Policy.onLeft(end, "INTERP-ALLOW-NL", rank = -1) {
case Decision(_, ss) => ss
.map(s => if (s.isNL) s.withPenalty(1) else s)
case Decision(_, ss) => ss.penalizeNL(1)
}
}
val split = Split(NoSplit, 0, policy = policy)
Expand Down Expand Up @@ -1282,8 +1280,7 @@ class Router(formatOps: FormatOps) {
if isParamClauseSite(m.leftOwner) &&
styleMap.at(o).binPack.bracketDefnSite
.exists(_ != BinPack.Site.Never) =>
if (isRightCommentThenBreak(ftd)) s
else s.map(x => if (x.isNL) x.withPenalty(p) else x)
if (isRightCommentThenBreak(ftd)) s else s.penalizeNL(p)
}
}
getNoSplit(nextComma.map(getSlbEndOnLeft))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,11 @@ object Split {
fileLineStack: FileLineStack,
): Split = if (mod eq null) ignored else Split(mod, cost)

implicit class ImplicitSeqSplit(private val obj: Seq[Split]) extends AnyVal {
def penalize(penalty: Int): Seq[Split] = obj.map(_.withPenalty(penalty))
def penalizeIf(penalty: Int)(f: Split => Boolean): Seq[Split] = obj
.map(x => if (f(x)) x.withPenalty(penalty) else x)
def penalizeNL(penalty: Int): Seq[Split] = penalizeIf(penalty)(_.isNL)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ object PolicyOps {
private val checkSyntax = noSyntaxNL || !style.newlines.ignoreInSyntax
override val f: Policy.Pf = {
case Decision(ft, s) if penalizeLambdas || !ft.left.is[T.RightArrow] =>
if (checkSyntax && ft.leftHasNewline) s.map(_.withPenalty(penalty))
else s.map(x => if (x.isNL) x.withPenalty(penalty) else x)
if (checkSyntax && ft.leftHasNewline) s.penalize(penalty)
else s.penalizeNL(penalty)
}
override def prefix: String = s"PNL+$penalty"
}
Expand Down Expand Up @@ -52,7 +52,7 @@ object PolicyOps {
val nonBoolPenalty = if (TokenOps.isBoolOperator(l)) 0 else 5
val penalty = TreeOps.nestedSelect(m.leftOwner) +
TreeOps.nestedApplies(m.rightOwner) + nonBoolPenalty
s.map(x => if (x.isNL) x.withPenalty(penalty) else x)
s.penalizeNL(penalty)
}

/** Forces all splits up to including expire to be on a single line.
Expand Down