← Back to Pull requests

Fix selection boundary handling in TokenStreamCreator and PrettyPrintBuffer. #1337

Closed
unknown closed this pull request without merging on Nov 10, 2026 23:10 UTC
View on GitHub
unknown unknown commented on Nov 10, 2026 21:42 UTC
Author

Summary

This commit fixes four selection edge cases in TokenStreamCreator and
PrettyPrintBuffer when formatting or linting line ranges (--lines):

1. // swift-format-ignore nodes crossing the end of a selection:
When a node preceded by // swift-format-ignore began inside the
selection and extended past the selection's end,
TokenStreamCreator.visitPre printed the entire node verbatim and
skipped its children, so visit(_ token:) never ran for tokens at
the selection boundary. As a result, .disableFormatting was never
emitted and formatting remained enabled for the rest of the file.
Fix visitPre to check selection.contains(nodeEnd.utf8Offset)
after emitting the verbatim node and append
.disableFormatting(nodeEnd) when the selection ended inside the
ignored node.

2. CommentMovingRewriter inside 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), CommentMovingRewriter
synthesizes tokens with empty source trivia and INVALID source
locations while moving the comment to the enclosing expression.
In whitespaceOnly lint mode, extractLeadingTrivia emitted no
.space token before the rewritten token because its synthesized
leadingTrivia was empty, collapsing = a to =a in the formatted
buffer and desynchronizing WhitespaceLinter. In addition, when a
selection ended inside the rewritten expression, the synthesized
tokens had endPositionBeforeTrailingTrivia == .invalid, so
.disableFormatting could not fire until the next original token.
Fix CommentMovingRewriter to transfer any horizontal whitespace
from 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 whitespaceOnly lint mode:
When an infix operator is followed by an end-of-line comment (for
example, let x = a + // comment\n b), CommentMovingRewriter
strips the operator's trailing trivia (including the comment and its
newline) and moves the comment after b. In whitespaceOnly mode,
PrettyPrinter skips printing the moved comment so non-whitespace
characters stay aligned with the source, which previously caused +
and b to collapse onto the same line without a newline break and
emitted a spurious add 1 line break finding before a. Preserve a
single .newlines(1) in the operator's trailing trivia when
stripping a trailing comment that ends with a newline so
extractLeadingTrivia still sees a line break between + and b.

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, PrettyPrintBuffer still ran
emitTokens and wrapped those unselected tokens in its internal line
tracking before .enableFormatting replaced output with the
verbatim source prefix. Every extra wrap in the unselected prefix
incremented PrettyPrintBuffer.lineNumber, shifting the line numbers
of subsequent [LineLength] or [BlankLines] findings inside the
selection. Recompute lineNumber from the actual newline count in
output (1 + number of \n bytes) inside
writeVerbatimAfterEnablingFormatting.

Adds regression tests in SwiftFormatterSelectionTests and
SwiftLinterSelectionTests for all four cases.

Diff: 463 lines (+347 / -116) across 5 files Reviewer: dylansturg Review duration: 87m
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
0.69
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 68.8% (HIGH) · Higher = larger blast radius
68.8%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 27.2% · Safe to merge
27.2%