Skip to content

Adjust folding range end lines for lineFoldingOnly clients#2772

Merged
DanielRosenwasser merged 4 commits intomainfrom
copilot/fix-code-folding-ranges
Feb 12, 2026
Merged

Adjust folding range end lines for lineFoldingOnly clients#2772
DanielRosenwasser merged 4 commits intomainfrom
copilot/fix-code-folding-ranges

Conversation

Copy link
Contributor

Copilot AI commented Feb 12, 2026

  • Investigate the issue: VS Code folding ranges are different/worse with tsgo
  • Root cause: The VS Code TypeScript extension applies adjustFoldingEnd when converting outlining spans to FoldingRange - it subtracts 1 from endLine when the range ends with a closing pair character (}, ], ), `, >). The Go LSP server returns FoldingRanges directly without this adjustment, causing VS Code (which hides endLine) to also hide the closing bracket line.
  • Implement adjustFoldingEnd in internal/ls/folding.go that mirrors the VS Code TypeScript extension's behavior
  • Apply the adjustment when client signals lineFoldingOnly: true
  • Add VerifyFoldingRangeLines method to fourslash test framework
  • Add integration test verifying the lineFoldingOnly adjustment
  • Add integration test verifying #region folding works correctly with lineFoldingOnly
  • Run existing tests to verify no regressions
  • Run full test suite - all tests pass
  • Run linter and formatter - no issues
  • Code review and security check - no issues
Original prompt

This section details on the original issue you should resolve

<issue_title>VSCode: code folding ranges</issue_title>
<issue_description>The collapsible ranges in typescript-native w/ TypeScript (Native Preview) extension are different (and far worse) than the current stable versions

Steps to reproduce

if (EMPTY_TAGs.has(tag)) {
  output += "/>";
} else {
  output += ">";

  if (!html && kidcount > 0) {
    //
  }
}

export function use<T>(ctx: Value<T>): T | undefined {
  // 
}

Behavior with typescript@6.0

Image Image

Behavior with tsgo

Image Image

Versions

typescriptteam.native-preview @ 0.20260212.1
@typescript/native-preview @ 7.0.0-dev.20260212.1
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

When VS Code sends lineFoldingOnly: true, it hides lines from startLine+1
to endLine. Without adjustment, closing brackets (}, ], ), etc.) would be
hidden when folding. This mirrors the VS Code TypeScript extension's
adjustFoldingEnd behavior (workaround for vscode#47240).

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix code folding ranges in TypeScript Native Preview Adjust folding range end lines for lineFoldingOnly clients Feb 12, 2026
Copilot AI requested a review from jakebailey February 12, 2026 16:40
@jakebailey
Copy link
Member

jakebailey commented Feb 12, 2026

LGTM:

Image

@jakebailey jakebailey marked this pull request as ready for review February 12, 2026 17:29
Copilot AI review requested due to automatic review settings February 12, 2026 17:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aligns the Go language server’s folding range behavior with VS Code’s lineFoldingOnly semantics, preventing closing braces/brackets/tags from being hidden when a fold is applied (matching the stable TypeScript extension behavior referenced in vscode#47240).

Changes:

  • Adjust folding range endLine for lineFoldingOnly clients when the fold ends on a closing pair character (}, ], ), `, >).
  • Add fourslash helper VerifyFoldingRangeLines to assert folding behavior using only start/end lines.
  • Add an integration test covering the reported reproduction case with lineFoldingOnly: true.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
internal/ls/folding.go Applies lineFoldingOnly end-line adjustment to keep closing delimiters visible when folding.
internal/fourslash/fourslash.go Adds a line-only folding range verifier for lineFoldingOnly scenarios.
internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go New fourslash integration test validating adjusted end lines for nested blocks/functions.

@DanielRosenwasser
Copy link
Member

I guess I'll link to microsoft/vscode#281660 as well

Comment on lines 36 to 37
// When lineFoldingOnly is true, VS Code hides lines from startLine+1 to endLine. To keep closing
// brackets/braces visible, we subtract 1 from endLine when the range ends with a closing pair character.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// When lineFoldingOnly is true, VS Code hides lines from startLine+1 to endLine. To keep closing
// brackets/braces visible, we subtract 1 from endLine when the range ends with a closing pair character.
// When lineFoldingOnly is true, we hide lines from startLine+1 to endLine. And to keep closing
// brackets/braces visible, we subtract 1 from endLine when the range ends with a closing pair character.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied in 0959ea3.

@DanielRosenwasser
Copy link
Member

Did you test this out on #region comments?

@jakebailey
Copy link
Member

I didn't, no. @copilot Address the above comments, including adding a test which uses regions too.

Update comment wording per DanielRosenwasser's suggestion.
Add TestFoldingRangeLineFoldingOnlyWithRegions to verify that
#region/#endregion spans are NOT adjusted (they don't end with
closing pair characters), while code blocks within regions are.

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 12, 2026

I didn't, no. @copilot Address the above comments, including adding a test which uses regions too.

Added TestFoldingRangeLineFoldingOnlyWithRegions in 0959ea3. The #region/#endregion spans end with n (not a closing pair character), so adjustFoldingEnd correctly leaves them untouched. The test verifies that region spans keep their original end lines while code blocks within regions (like function foo() {}) still get adjusted.

@jakebailey
Copy link
Member

Seems fine?

folding2

But, maybe you wanted it to leave the end?

@jakebailey
Copy link
Member

@DanielRosenwasser
Copy link
Member

I guess they can't intersect anyway, but it was probably good to have something here.

@DanielRosenwasser
Copy link
Member

Needs a format I guess

@DanielRosenwasser DanielRosenwasser added this pull request to the merge queue Feb 12, 2026
Merged via the queue into main with commit 0cb9de2 Feb 12, 2026
20 checks passed
@DanielRosenwasser DanielRosenwasser deleted the copilot/fix-code-folding-ranges branch February 12, 2026 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VSCode: code folding ranges

3 participants