Skip to content

Commit

Permalink
Split: add implicit penalizeNL and penalizeIf
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 25, 2024
1 parent b7cc8ec commit 1d4cc1c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
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 @@ -1276,8 +1274,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 @@ -56,7 +56,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

0 comments on commit 1d4cc1c

Please sign in to comment.