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

Router: binpack: refactor policies, newline split #3971

Merged
merged 1 commit into from
May 10, 2024
Merged
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 @@ -1125,7 +1125,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 @@ -1138,7 +1138,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 @@ -1148,16 +1148,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 @@ -1250,7 +1251,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 @@ -1265,19 +1265,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 @@ -1309,15 +1315,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
Loading