Skip to content

Adjust folding range's end line only if closing bracket followed by non-whitespace - #65319

Merged
Gabriela Araujo Britto (gabritto) merged 11 commits into
mainfrom
gabritto/issue64235
Nov 10, 2026
Merged

Gabriela Araujo Britto (gabritto) merged 11 commits into
mainfrom
gabritto/issue64235

Conversation

@gabritto

@gabritto Gabriela Araujo Britto (gabritto) commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Fixes #64235.

This PR roughly implements the algorithm described in #64235.

Basically before the PR, if the client had lineOnly: true, and the folding range ended with some kind of closing bracket, we'd recede the end line by one, so that the closing bracket is visible once the range is collapsed.
However, this behavior is not the one desired by many users (see microsoft/vscode#3352), who'd rather not see a single line with only a closing bracket. Since vscode is not planning on supporting lineOnly: false any time soon, this PR allows us to adopt the better behavior for TS: now we only recede the end line if there's some non-whitespace character in the same line as the closing bracket (and therefore it's likely relevant for the user to see that line).

I'm not sure however if we should indeed show a }; in the case of:

if (blah) {
    ...
};

Also not sure if we should omit the closing bracket if there's a comment on that line, though maybe that's not very common.

I also took the liberty of changing the algorithm in the case of a content-mapped file.
If the TS file we're computing ranges for is generated by a content mapper, we can't be sure that absence of non-whitespace in the same line as a closing bracket in the TS file means there isn't any relevant information to show in the corresponding original line. So in that case, we recede the end line if there's any non-whitespace character in the original line (see test TestFoldingRangeLineFoldingOnlyWithUnmappedTrailingMarkup).

Copilot AI balanced review requested due to automatic review settings November 4, 2026 16:35
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 4, 2026
@typescript-automation typescript-automation Bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Nov 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The folding suffix check can hide visible sibling JSX text and statements on the closing brace’s LSP line because it mishandles JSX context and uses ECMAScript line boundaries.

2 open issues
What changed in this PR

This PR updates language-server folding ranges for line-only clients to address #64235, including closing-bracket lines unless trailing content needs to remain visible.

Changes:

  • Preserve closing lines when only trivia follows the closing bracket.
  • Check original source text for trailing content in content-mapped files.
  • Update folding expectations and add terminating-line and mapped-markup regression tests.
File Description
tsc/​internal/​ls/​folding.go Adjusts folding endpoints based on trailing source content.
tsc/​internal/​fourslash/​tests/​foldingRangeLineFoldingOnly_test.go Updates expectations and tests trailing tokens, comments, EOF, and mapped markup.

Review effort: Balanced

Comment thread tsc/internal/ls/folding.go Resolved
Comment thread tsc/internal/ls/folding.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the folding-range changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@n-gist

n-gist (n-gist) commented Nov 5, 2026 •

Copy link
Copy Markdown

In my comment in the linked discussion (#64235 (comment)), I deliberately ignored the possible significance of non-trivia following the closing bracket. My reasoning was that when I see a folded region whose closing-bracket line is hidden, I can understand that there may be non-trivia hidden along with it.

But that's just my perspective. After thinking about this further, I admit that the concern about non-trivia following the closing bracket is valid and may deserve special consideration when presenting code to be read.

However, adjusting the end line by excluding the closing-bracket line shifts the purpose of LS output from providing technical information toward presentation-related data, based on opinionated assumptions that may be questionable. Whether this is a problem depends on what LS output is expected to represent:

a) The natural syntactic boundary

b) The useful folding boundary

Any such exclusion introduces information loss and makes the output ambiguous: clients can no longer be sure whether the END marker points to the line containing the closing delimiter or was adjusted because the LS output represents b).

An example of the problem

  1. The LS recedes the end line when there is non-trivia following the closing bracket.

  2. The client prefers to show the closing-bracket line when a region is folded, even when there is no non-trivia on that line.

Client expects to adjust output mathematically as:

// client
if (alwaysShowClosingBracketLine) { // true for VS Code, by its current default folding style
  regions.forEach(r => {
    r.end--
  })
} else {
  // noop
}
// Consider

if (foo) {
  first()
  second()
  third()
} else bar()

// When folded, results in

if (foo) { /*...*/
  third()
} else bar()

Now third() remains visible because client receded all regions, including those whose end lines had already been receded by the LS due to the non-trivia exclusion.

To fix this, client would need to parse and analyze the regions to restore that information.

Possible solutions

To preserve the possibility of supporting both a) and b), it would be useful to provide information about end-line adjustments explicitly, by adding it to the calculated output. Clients could then skip regions that had already been reduced:

// LS
interface Region {
  reduced?: true // Set when the region was receded by one line due to non-trivia following the closing bracket.
}

// client
if (alwaysShowClosingBracketLine) {
  regions.forEach(r => {
    if (!r.reduced) r.end--
  })
} else {
  // noop
}

Alternatively, a solution that preserves both a) and b) more naturally, at the cost of making the exclusion effects explicit rather than implicit, would be to provide the relevant information without adjusting END, keeping it as a strict technical pointer, and letting clients decide what to do with the additional information. This also leaves room for any future expansions of additional information, without collapsing all effects into resulting value:

// LS
interface Region {
  hasNonTriviaAfterEnd?: true
}

// client
if (alwaysShowClosingBracketLine) {
  regions.forEach(r => {
    r.end--
  })
} else {
  regions.forEach(r => {
    if (r.hasNonTriviaAfterEnd) r.end--
  })
}

Or as an extendable bitmask if this looks beneficial, to reduce traffic:

// LS
const enum RegionFlags {
  NonTriviaAfterEnd = 1 << 0,
  CommentAfterEnd = 1 << 1,
  SemicolonAfterEnd = 1 << 2
}
interface Region {
  flags?: RegionFlags
}

// client
if (alwaysShowClosingBracketLine) {
  regions.forEach(r => {
    r.end--
  })
} else {
  regions.forEach(r => {
    if (r.flags & RegionFlags.NonTriviaAfterEnd) r.end--
  })
}

@gabritto

Copy link
Copy Markdown
Member Author

In my comment in the linked discussion (#64235 (comment)), I deliberately ignored the possible significance of non-trivia following the closing bracket. My reasoning was that when I see a folded region whose closing-bracket line is hidden, I can understand that there may be non-trivia hidden along with it.

But that's just my perspective. After thinking about this further, I admit that the concern about non-trivia following the closing bracket is valid and may deserve special consideration when presenting code to be read.

However, adjusting the end line by excluding the closing-bracket line shifts the purpose of LS output from providing technical information toward presentation-related data, based on opinionated assumptions that may be questionable. Whether this is a problem depends on what LS output is expected to represent:

a) The natural syntactic boundary

b) The useful folding boundary

Any such exclusion introduces information loss and makes the output ambiguous: clients can no longer be sure whether the END marker points to the line containing the closing delimiter or was adjusted because the LS output represents b).

An example of the problem

  1. The LS recedes the end line when there is non-trivia following the closing bracket.
  2. The client prefers to show the closing-bracket line when a region is folded, even when there is no non-trivia on that line.

Client expects to adjust output mathematically as:

// client
if (alwaysShowClosingBracketLine) { // true for VS Code, by its current default folding style
  regions.forEach(r => {
    r.end--
  })
} else {
  // noop
}
// Consider

if (foo) {
  first()
  second()
  third()
} else bar()

// When folded, results in

if (foo) { /*...*/
  third()
} else bar()

Now third() remains visible because client receded all regions, including those whose end lines had already been receded by the LS due to the non-trivia exclusion.

To fix this, client would need to parse and analyze the regions to restore that information.

Possible solutions

To preserve the possibility of supporting both a) and b), it would be useful to provide information about end-line adjustments explicitly, by adding it to the calculated output. Clients could then skip regions that had already been reduced:

// LS
interface Region {
  reduced?: true // Set when the region was receded by one line due to non-trivia following the closing bracket.
}

// client
if (alwaysShowClosingBracketLine) {
  regions.forEach(r => {
    if (!r.reduced) r.end--
  })
} else {
  // noop
}

Alternatively, a solution that preserves both a) and b) more naturally, at the cost of making the exclusion effects explicit rather than implicit, would be to provide the relevant information without adjusting END, keeping it as a strict technical pointer, and letting clients decide what to do with the additional information. This also leaves room for any future expansions of additional information, without collapsing all effects into resulting value:

// LS
interface Region {
  hasNonTriviaAfterEnd?: true
}

// client
if (alwaysShowClosingBracketLine) {
  regions.forEach(r => {
    r.end--
  })
} else {
  regions.forEach(r => {
    if (r.hasNonTriviaAfterEnd) r.end--
  })
}

Or as an extendable bitmask if this looks beneficial, to reduce traffic:

// LS
const enum RegionFlags {
  NonTriviaAfterEnd = 1 << 0,
  CommentAfterEnd = 1 << 1,
  SemicolonAfterEnd = 1 << 2
}
interface Region {
  flags?: RegionFlags
}

// client
if (alwaysShowClosingBracketLine) {
  regions.forEach(r => {
    r.end--
  })
} else {
  regions.forEach(r => {
    if (r.flags & RegionFlags.NonTriviaAfterEnd) r.end--
  })
}

I don't think existing clients do the line adjustment you mention above, so I don't know that it's worth adding a custom thing in case a client might implement that kind of thing in the future. In any case, if something like this happens, we may adjust the TypeScript behavior, but for now, I think the strategy in this PR is a good way to solve the issue.

@jakebailey Jake Bailey (jakebailey) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omit the closing bracket if there's a comment on that line, though maybe that's not very common.

Probably not? The ; case I can't imagine anyone caring about, besides arrow funcs (Not sure if that is tested or working), but not hiding the comment seems better?

Anyway I'm not super picky if you think this is doing the right thing

@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Nov 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The folding logic incorrectly excludes closing lines with trailing comments and treats whitespace-only or later-line JSX text as visible same-line content.

2 open issues

Review effort: Balanced

Comment thread tsc/internal/ls/folding.go Resolved
Comment thread tsc/internal/ls/folding.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Line-only TSX folding can panic on an invalid slice range, and trailing comments incorrectly cause closing lines to be excluded from folds.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread tsc/internal/ls/folding.go Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@jakebailey

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) pack this

@typescript-automation

typescript-automation Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started

@gabritto

Copy link
Copy Markdown
Member Author

Probably not? The ; case I can't imagine anyone caring about, besides arrow funcs (Not sure if that is tested or working), but not hiding the comment seems better?

Updated the PR to not hide the comments anymore. So we'll only hide whitespace.

@jakebailey

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) pack this

@typescript-automation

typescript-automation Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started ❌ Results

@typescript-automation

Copy link
Copy Markdown
Contributor

Hey, Jake Bailey (@jakebailey)! I was unable to build and publish this PR.

Check the logs at: https://github.com/microsoft/TypeScript/actions/runs/42716721724

Merged via the queue into main with commit b7917e5 Nov 10, 2026
34 checks passed
@gabritto
Gabriela Araujo Britto (gabritto) deleted the gabritto/issue64235 branch November 10, 2026 01:35
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Nov 10, 2026
@gabritto Gabriela Araujo Britto (gabritto) changed the title Adjust folding range's end line only if closing bracket followed by non-trivia Adjust folding range's end line only if closing bracket followed by non-whitespace Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Include terminating line in folding ranges when last token has no trailing non-trivia

4 participants