Repository navigation
Adjust folding range's end line only if closing bracket followed by non-whitespace - #65319
Conversation
There was a problem hiding this comment.
🟡 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
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
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:
Any such exclusion introduces information loss and makes the output ambiguous: clients can no longer be sure whether the An example of the problem
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 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 // 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 // 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. |
Jake Bailey (jakebailey)
left a comment
There was a problem hiding this comment.
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
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
TypeScript Bot (@typescript-bot) pack this |
|
Starting jobs; this comment will be updated as builds start and complete.
|
Updated the PR to not hide the comments anymore. So we'll only hide whitespace. |
|
TypeScript Bot (@typescript-bot) pack this |
|
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 |


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: falseany 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: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).