← Back to Pull requests

Format and lint blank lines when selections start or end on blank lines #1338

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

Summary

When formatting or linting a line range (--lines) that includes blank
lines before the first selected token (or selects only blank lines),
swift-format format and swift-format lint previously ignored
maximumBlankLines unless the token preceding the blank lines was also
selected, and leaked formatting onto the unselected line following a
selected blank line:

  // Input (`swift-format format --lines 3:6` or `--lines 4:6`):
  func foo() {
      let   a   =   1



      let   b   =   2
  }

  // Previous output (excess blank lines on lines 3-5 were not collapsed):
  func foo() {
      let   a   =   1



    let b = 2
  }

  // Expected output:
  func foo() {
      let   a   =   1

    let b = 2
  }

Several interactions caused this:
1. In SwiftSyntax, blank lines between two tokens (or at the start of a
file) live in the following token's leadingTrivia, which
TokenStreamCreator.visit(_ token:) processes in
extractLeadingTrivia before calling
generateEnableFormattingIfNecessary at
token.positionAfterSkippingLeadingTrivia. When the preceding token
was outside the selection, appendNewlines merged the blank lines
into tokens[lastBreakIndex] while formatting was still disabled,
and .enableFormatting at token.positionAfterSkippingLeadingTrivia
copied all preceding newlines verbatim from the original source.
2. Conversely, when a selection included a token and ended on a
following blank line (--lines 2:3), extractLeadingTrivia never
called generateDisableFormattingIfNecessary after the newline run,
so formatting remained enabled for the first token on the next
unselected line and re-indented it. Similarly, disabling formatting
at endOfFileToken.endPositionBeforeTrailingTrivia before
appendAfterTokensAndTrailingComments(endOfFileToken) dropped the
file's final trailing newline when a selection ended on trailing
blank lines at EOF.
3. In PrettyPrintBuffer.writeNewlines, blank-line indentation checked
number >= 1 rather than number + consecutiveNewlineCount >= 1,
skipping indentation on the first blank line when
writeVerbatimAfterEnablingFormatting had already written the
preceding line's terminating newline (consecutiveNewlineCount == 1).
4. In WhitespaceLinter.compareWhitespace, excess line breaks were only
diagnosed on the first excessUserLines runs (starting at the
terminating newline of the unselected preceding line, which
WhitespaceLinter.diagnose filtered out as outside the selection),
and formattedRunsIterator.next() was advanced even on deleted user
lines, causing a spurious [TrailingWhitespace] finding when excess
blank lines preceded an indented line.

Fix all four places:

  • Track contiguous newline/blank-line runs in extractLeadingTrivia

  • (and trailing blank lines at endOfFileToken). When a run contains
    at least one blank line whose range overlaps or touches selection,
    enable formatting at the start of the first blank line before the
    break token, and call generateDisableFormattingIfNecessary at the
    end of the newline run.
  • Do not disable formatting before after(node.endOfFileToken) on

  • endOfFileToken so the final newline at EOF is preserved.
  • Check number + consecutiveNewlineCount >= 1 when indenting blank

  • lines in PrettyPrintBuffer.writeNewlines.
  • In WhitespaceLinter.compareWhitespace, attribute

  • remainingExcessLines to newline runs within context.selection and
    only advance formattedRunsIterator on non-deleted runs

    Diff: 447 lines (+335 / -112) across 5 files Reviewer: dylansturg Review duration: 1115m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 447 lines diff across 5 files
    0.45
    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.365
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1338"
    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: 45.4% (MEDIUM) · Higher = larger blast radius
    45.4%
    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.9% · Safe to merge
    17.9%