Fix selection boundary handling in TokenStreamCreator and PrettyPrintBuffer. #1337
All Vouch review confidence checks passed
Residual risk 0.27 = Change Risk (0.69) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.69 · 463 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.367
Shallow Review Depth
shallow_review_depth
0.289
Sensitive Paths
sensitive_paths
0.069
Real PR Conversation Timeline
1 comments
"Review active on #1337"
general
weight: 0.35
dylansturg
Summary
This commit fixes four selection edge cases in
TokenStreamCreatorandPrettyPrintBufferwhen formatting or linting line ranges (--lines):1.
// swift-format-ignorenodes crossing the end of a selection:When a node preceded by
// swift-format-ignorebegan inside theselection and extended past the selection's end,
TokenStreamCreator.visitPreprinted the entire node verbatim andskipped its children, so
visit(_ token:)never ran for tokens atthe selection boundary. As a result,
.disableFormattingwas neveremitted and formatting remained enabled for the rest of the file.
Fix
visitPreto checkselection.contains(nodeEnd.utf8Offset)after emitting the verbatim node and append
.disableFormatting(nodeEnd)when the selection ended inside theignored node.
2.
CommentMovingRewriterinside nested expressions with selections:When an infix expression inside a parenthesized or function-call
expression contained a comment immediately after an operator (for
example,
let x = a + // comment\n b),CommentMovingRewritersynthesizes tokens with empty source trivia and
INVALIDsourcelocations while moving the comment to the enclosing expression.
In
whitespaceOnlylint mode,extractLeadingTriviaemitted no.spacetoken before the rewritten token because its synthesizedleadingTriviawas empty, collapsing= ato=ain the formattedbuffer and desynchronizing
WhitespaceLinter. In addition, when aselection ended inside the rewritten expression, the synthesized
tokens had
endPositionBeforeTrailingTrivia == .invalid, so.disableFormattingcould not fire until the next original token.Fix
CommentMovingRewriterto transfer any horizontal whitespacefrom the inner expression's first token onto the moved token when its
leading trivia is stripped, and emit
generateDisableFormattingIfNecessary(rewrittenExpr.endPositionBeforeTrailingTrivia)after walking the rewritten expression.
3. Trailing comments after infix operators in
whitespaceOnlylint mode:When an infix operator is followed by an end-of-line comment (for
example,
let x = a + // comment\n b),CommentMovingRewriterstrips the operator's trailing trivia (including the comment and its
newline) and moves the comment after
b. InwhitespaceOnlymode,PrettyPrinterskips printing the moved comment so non-whitespacecharacters stay aligned with the source, which previously caused
+and
bto collapse onto the same line without a newline break andemitted a spurious
add 1 line breakfinding beforea. Preserve asingle
.newlines(1)in the operator's trailing trivia whenstripping a trailing comment that ends with a newline so
extractLeadingTriviastill sees a line break between+andb.4. Line numbers for pretty-printer lint findings after overlong
unselected lines:
When formatting was disabled across an unselected prefix containing
lines longer than
lineLength,PrettyPrintBufferstill ranemitTokensand wrapped those unselected tokens in its internal linetracking before
.enableFormattingreplacedoutputwith theverbatim source prefix. Every extra wrap in the unselected prefix
incremented
PrettyPrintBuffer.lineNumber, shifting the line numbersof subsequent
[LineLength]or[BlankLines]findings inside theselection. Recompute
lineNumberfrom the actual newline count inoutput(1 + number of\nbytes) insidewriteVerbatimAfterEnablingFormatting.Adds regression tests in
SwiftFormatterSelectionTestsandSwiftLinterSelectionTestsfor all four cases.