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: no need to bind a new var for FormatTokens #4036

Merged
merged 1 commit into from
Jun 8, 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 @@ -1558,15 +1558,15 @@ class Router(formatOps: FormatOps) {
case _ => Some(false)
}.isDefined => Seq(Split(NoSplit, 0))

case t @ FormatToken(left, _: T.Dot, _)
case FormatToken(left, _: T.Dot, _)
if rightOwner.is[Term.Select] ||
(rightOwner.is[Term.Match] && dialect.allowMatchAsOperator) =>
val enclosed = style.encloseSelectChains
val (expireTree, nextSelect) =
findLastApplyAndNextSelect(rightOwner, enclosed)
val thisSelect = rightOwner match {
case x: Term.Select => SelectLike(x)
case x: Term.Match => SelectLike(x, getKwMatchAfterDot(t))
case x: Term.Match => SelectLike(x, getKwMatchAfterDot(ft))
}
val (prevSelect, prevApply) =
findPrevSelectAndApply(thisSelect.qual, enclosed)
Expand Down Expand Up @@ -1613,6 +1613,7 @@ class Router(formatOps: FormatOps) {
}
}
}
val ftAfterRight = tokens(ft, 2)
val baseSplits = style.newlines.getSelectChains match {
case Newlines.classic =>
def getNlMod = {
Expand Down Expand Up @@ -1648,7 +1649,7 @@ class Router(formatOps: FormatOps) {
}
val newlinePolicy = breakOnNextDot & penalizeBreaks
val ignoreNoSplit = nlOnly ||
t.hasBreak &&
hasBreak &&
(left.is[T.Comment] || style.optIn.breakChainOnFirstMethodDot)
val chainLengthPenalty =
if (
Expand All @@ -1661,21 +1662,20 @@ class Router(formatOps: FormatOps) {
// many arguments on the same line can be hard to read. By not
// putting a newline before the dot, we force the argument list
// to break into multiple lines.
tokens(t, 2).meta.rightOwner match {
ftAfterRight.meta.rightOwner match {
case Member.ArgClause(v) => math.max(0, v.length - 1)
case _ => 0
}
else 0
// when the flag is on, penalize break, to avoid idempotence issues;
// otherwise, after the break is chosen, the flag prohibits nosplit
val nlBaseCost =
if (style.optIn.breakChainOnFirstMethodDot && t.noBreak) 3
else 2
if (style.optIn.breakChainOnFirstMethodDot && noBreak) 3 else 2
val nlCost = nlBaseCost + nestedPenalty + chainLengthPenalty
val nlMod = getNlMod
Seq(
Split(!prevChain, 1) { // must come first, for backwards compat
if (style.optIn.breaksInsideChains) NoSplit.orNL(t.noBreak)
if (style.optIn.breaksInsideChains) NoSplit.orNL(noBreak)
else nlMod
}.withPolicy(newlinePolicy)
.onlyFor(SplitTag.SelectChainSecondNL),
Expand All @@ -1686,10 +1686,10 @@ class Router(formatOps: FormatOps) {
)
} else {
val isComment = left.is[T.Comment]
val doBreak = nlOnly || isComment && t.hasBreak
val doBreak = nlOnly || isComment && hasBreak
Seq(
Split(!prevChain, 1) {
if (style.optIn.breaksInsideChains) NoSplit.orNL(t.noBreak)
if (style.optIn.breaksInsideChains) NoSplit.orNL(noBreak)
else if (doBreak) Newline
else getNlMod
}.withPolicy(breakOnNextDot)
Expand All @@ -1698,10 +1698,10 @@ class Router(formatOps: FormatOps) {
)
}

case _ if left.is[T.Comment] => Seq(Split(Space.orNL(t.noBreak), 0))
case _ if left.is[T.Comment] => Seq(Split(Space.orNL(noBreak), 0))

case Newlines.keep =>
if (t.noBreak) Seq(
if (noBreak) Seq(
Split(NoSplit, 0),
Split(Newline, Constants.ExceedColumnPenalty * 3),
)
Expand All @@ -1728,7 +1728,7 @@ class Router(formatOps: FormatOps) {
else {
val end = nextSelect
.fold(expire)(x => getLastNonTrivialToken(x.qual))
def exclude = insideBracesBlock(t, end, true)
def exclude = insideBracesBlock(ft, end, true)
noSplitBase.withSingleLine(end, exclude)
}
Seq(noSplit, nlSplitBase)
Expand Down Expand Up @@ -1761,7 +1761,7 @@ class Router(formatOps: FormatOps) {
val spcPolicy = delayedBreakPolicyOpt
val nlPolicy = if (noIndent) spcPolicy else None
val splits =
if (nextNonCommentSameLine(tokens(t, 2)).right.is[T.Comment])
if (nextNonCommentSameLine(ftAfterRight).right.is[T.Comment])
// will break
baseSplits.map(_.withIndent(nlIndent).andFirstPolicyOpt(nlPolicy))
else {
Expand Down
Loading