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: minor refactor in .select handling rule #3495

Merged
merged 1 commit into from
Mar 9, 2023
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 @@ -235,14 +235,6 @@ class FormatOps(
TokenRanges(TokenRange(token, other))
}

def getExcludeIf(
end: T,
cond: T => Boolean = _.is[T.RightBrace]
): TokenRanges =
if (cond(end)) // allow newlines in final {} block
parensTuple(end)
else TokenRanges.empty

def insideBlock[A](start: FormatToken, end: T)(implicit
classifier: Classifier[T, A]
): TokenRanges =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1781,12 +1781,12 @@ class Router(formatOps: FormatOps) {
// This policy will apply to both the space and newline splits, otherwise
// the newline is too cheap even it doesn't actually prevent other newlines.
val penalizeBreaks = PenalizeAllNewlines(chainExpire, 2)
def slbPolicy =
SingleLineBlock(
chainExpire,
getExcludeIf(chainExpire),
noSyntaxNL = true
)
def slbPolicy(implicit fileLine: FileLine) = {
val exclude = // allow newlines in final {} block
if (chainExpire.is[T.RightBrace]) parensTuple(chainExpire)
else TokenRanges.empty
SingleLineBlock(chainExpire, exclude, noSyntaxNL = true)
}
val newlinePolicy = breakOnNextDot & penalizeBreaks
val ignoreNoSplit = nlOnly || t.hasBreak &&
(left.is[T.Comment] || style.optIn.breakChainOnFirstMethodDot)
Expand Down Expand Up @@ -1897,13 +1897,19 @@ class Router(formatOps: FormatOps) {

// trigger indent only on the first newline
val nlIndent = Indent(indentLen, expire, After)
val spcIndent = nextDotIfSig
.fold(Indent.empty)(x => Indent(indentLen, x.left, ExpiresOn.Before))
val willBreak = nextNonCommentSameLine(tokens(t, 2)).right.is[T.Comment]
val splits = baseSplits.map { s =>
if (willBreak || s.isNL) s.withIndent(nlIndent)
else s.andFirstPolicyOpt(delayedBreakPolicyOpt).withIndent(spcIndent)
}
val splits =
if (nextNonCommentSameLine(tokens(t, 2)).right.is[T.Comment])
baseSplits.map(_.withIndent(nlIndent)) // will break
else {
val spcIndent = nextDotIfSig.fold(Indent.empty) { x =>
Indent(indentLen, x.left, ExpiresOn.Before)
}
baseSplits.map { s =>
if (s.isNL) s.withIndent(nlIndent)
else
s.andFirstPolicyOpt(delayedBreakPolicyOpt).withIndent(spcIndent)
}
}

if (prevSelect.isEmpty) splits
else baseSplits ++ splits.map(_.onlyFor(SplitTag.SelectChainFirstNL))
Expand Down