Skip to content

Commit

Permalink
Router: binpack: refactor policies, newline split
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 10, 2024
1 parent bcd80a0 commit c5122ce
Showing 1 changed file with 28 additions and 25 deletions.
Expand Up @@ -1132,7 +1132,7 @@ class Router(formatOps: FormatOps) {
def noSplitPolicy: Policy =
if (slbOnly) slbPolicy
else {
val penalizeOpens = bracketPenalty.fold(Policy.noPolicy) { p =>
val opensPolicy = bracketPenalty.fold(Policy.noPolicy) { p =>
Policy.before(close) {
case Decision(ftd @ FormatToken(o: T.LeftBracket, _, m), s)
if isParamClauseSite(m.leftOwner) && styleMap.at(o)
Expand All @@ -1145,7 +1145,7 @@ class Router(formatOps: FormatOps) {
if (oneline && isSingleArg) NoPolicy
else SingleLineBlock(x.tokens.last, noSyntaxNL = true)
}
argPolicy & (penalizeOpens | penalizeBrackets)
argPolicy & (opensPolicy | penalizeBrackets)
}
val rightIsComment = right.is[T.Comment]
val mustUseNL = onlyConfigStyle ||
Expand All @@ -1154,16 +1154,17 @@ class Router(formatOps: FormatOps) {
val noSplitModification =
if (rightIsComment && !mustUseNL) getMod(ft) else baseNoSplitMod
val nlMod = if (rightIsComment && mustUseNL) getMod(ft) else Newline
val nlDanglePolicy =
if (mustDangle) decideNewlinesOnlyBeforeClose(close) else NoPolicy
def getDanglePolicy(implicit fileLine: FileLine) =
decideNewlinesOnlyBeforeClose(close)
val nlPolicy = if (mustDangle) getDanglePolicy else NoPolicy
def nlCost = bracketPenalty.getOrElse(1)

Seq(
Split(mustUseNL, 0)(noSplitModification)
.withOptimalToken(close, ignore = !slbOnly, killOnFail = true)
.withPolicy(noSplitPolicy).withIndents(noSplitIndents),
Split(nlMod, if (mustUseNL || slbOnly) 0 else nlCost)
.withPolicy(nlDanglePolicy & nlOnelinePolicy & penalizeBrackets)
.withPolicy(nlPolicy & nlOnelinePolicy & penalizeBrackets)
.withIndent(indent),
)
}
Expand Down Expand Up @@ -1255,7 +1256,6 @@ class Router(formatOps: FormatOps) {
if (oneline) nextCommaOneline.orElse(optClose)
else if (style.newlines.source.eq(Newlines.fold)) None
else findComma(ft).orElse(optClose)
def unindentPolicy = unindentAtExclude(exclude, Num(-indentLen))
def scajaJsPolicy = Policy(Policy.End.On(close)) {
case d: Decision if d.formatToken.right eq close => d.noNewlines
}
Expand All @@ -1270,19 +1270,25 @@ class Router(formatOps: FormatOps) {
SingleLineBlock(slbEnd, noSyntaxNL = true)
}
} else penalizeNewlinesPolicy
def indentOncePolicy =
if (style.binPack.indentCallSiteOnce) {
val trigger = getIndentTrigger(leftOwner)
Policy.on(close) {
case Decision(FormatToken(LeftParenOrBracket(), _, m), s)
if isArgClauseSite(m.leftOwner) =>
s.map(x => if (x.isNL) x else x.switch(trigger, false))
}
val indentPolicy =
if (noSplitIndents.nonEmpty) {
def unindentPolicy =
if (isSingleArg) unindentAtExclude(exclude, Num(-indentLen))
else NoPolicy
def indentOncePolicy =
if (style.binPack.indentCallSiteOnce) {
val trigger = getIndentTrigger(leftOwner)
Policy.on(close) {
case Decision(FormatToken(LeftParenOrBracket(), _, m), s)
if isArgClauseSite(m.leftOwner) =>
s.map(x => if (x.isNL) x else x.switch(trigger, false))
}
} else NoPolicy
unindentPolicy & indentOncePolicy
} else NoPolicy
baseNoSplit.withOptimalTokenOpt(opt).withPolicy(noSplitPolicy)
.andPolicy(unindentPolicy, !isSingleArg || noSplitIndents.isEmpty)
.andPolicy(indentOncePolicy, noSplitIndents.isEmpty)
.andPolicy(scajaJsPolicy, !flags.scalaJsStyle)
.andPolicy(indentPolicy)
}

val nlPolicy = {
Expand Down Expand Up @@ -1314,15 +1320,12 @@ class Router(formatOps: FormatOps) {
val nlMod =
if (rightIsComment && nlOnly) getMod(ft)
else NewlineT(alt = if (singleLineOnly) Some(NoSplit) else None)
Seq(
noSplit,
Split(nlMod, bracketPenalty * (if (oneline) 4 else 2))
.withIndent(indent)
.withSingleLineOpt(if (singleLineOnly) Some(close) else None)
.andPolicy(nlPolicy)
.andPolicy(penalizeNewlinesPolicy, singleLineOnly)
.andPolicyOpt(singleArgAsInfix.map(InfixSplits(_, ft).nlPolicy)),
)
val nlSplit = Split(nlMod, bracketPenalty * (if (oneline) 4 else 2))
.withIndent(indent)
.withSingleLineOpt(if (singleLineOnly) Some(close) else None)
.andPolicy(nlPolicy & penalizeNewlinesPolicy, singleLineOnly)
.andPolicyOpt(singleArgAsInfix.map(InfixSplits(_, ft).nlPolicy))
Seq(noSplit, nlSplit)

// Closing def site ): ReturnType
case FormatToken(left, _: T.Colon, DefDefReturnTypeRight(returnType))
Expand Down

0 comments on commit c5122ce

Please sign in to comment.