← Back to Pull requests

Fix multiline string literal handling with partial selections and whitespace-only linting. #1335

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

Summary

This PR fixes two bugs in how swift-format handles multiline string literals ("""...""") when formatting or linting line ranges (--lines) or running WhitespaceLinter:

1. Preserve multiline string literals that are only partially selected

When formatting or linting a line range (--lines) that overlaps only part of a multiline string literal, TokenStreamCreator previously only checked selection boundaries in visit(_ token: TokenSyntax), which is invoked for openingQuote and closingQuote while StringSegmentSyntax and ExpressionSegmentSyntax return .skipChildren:

// Input (`swift-format format --lines 4:4`):
func foo() {
  let s = """
    hello
    """
}

// Previous output (corrupts the runtime String value to "  hello"):
func foo() {
  let s = """
    hello
  """
}

Because the closing """ token on line 4 entered visit(_ token:) while lines 2–3 were outside the selection, the pretty printer re-indented only the closing delimiter to 2 spaces while leaving the segment lines at 4 spaces, silently changing the runtime String value. Conversely, a selection starting on the opening """ and ending mid-string remained enabled through the rest of the literal because segments never called generateDisableFormattingIfNecessary.

To avoid changing a multiline string literal's content when its closing quote indentation and segment lines are not formatted together, this change treats multiline StringLiteralExprSyntax nodes as atomic for selection formatting and linting: if a multiline string literal is not completely contained within a single selection range (from openingQuote through closingQuote), formatting stays disabled across the entire literal and its segments so partially selected multiline strings are left untouched.

2. Avoid emitting or stripping multiline string backslash continuations in whitespace-only mode

When reflowMultilineStringLiterals is .onlyLinesOverLength or .always, TokenStreamCreator splits multiline string segments into word tokens separated by .break(..., newlines: .escaped) tokens (which print \ in PrettyPrinter) and strips existing \ line continuations under .always.

During swift-format lint, PrettyPrinter runs with whitespaceOnly: true and WhitespaceLinter asserts that all non-whitespace UTF-8 code units in the formatted output match the original source text. Inserting or removing \ line-continuation characters or wrapping string segments via .escaped breaks in whitespaceOnly mode desynchronizes WhitespaceLinter (triggering the "Non-whitespace characters do not match" assertion in debug builds or emitting misaligned whitespace findings).

This change forces configuration.reflowMultilineStringLiterals = .never inside PrettyPrinter when whitespaceOnly is true, and guards \ emission on .escaped breaks with !whitespaceOnly. Because reflowing a multiline string literal requires adding or removing \ characters (a non-whitespace edit), WhitespaceLinter leaves segment line wrapping unchanged while continuing to lint indentation and surrounding whitespace.

Diff: 393 lines (+294 / -99) across 4 files Reviewer: dylansturg Review duration: 31m
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 393 lines diff across 4 files
0.43
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.358
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1335"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
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: 43.1% (MEDIUM) · Higher = larger blast radius
43.1%
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: 17% · Safe to merge
17%