From 513d2e45f9b8c71a43ef9d067a370563cb270fd0 Mon Sep 17 00:00:00 2001 From: Gabriela Araujo Britto Date: Wed, 4 Nov 2026 16:05:34 +0000 Subject: [PATCH 1/6] adjust end line only if non-trivia --- .../tests/foldingRangeLineFoldingOnly_test.go | 152 +++++++++++++++++- tsc/internal/ls/folding.go | 33 +++- 2 files changed, 178 insertions(+), 7 deletions(-) diff --git a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go index 2a8fa6a7c13be..d607c7e2a4b6b 100644 --- a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go +++ b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go @@ -6,6 +6,7 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/fourslash" "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" "github.com/microsoft/TypeScript/tsc/internal/testutil" + "github.com/microsoft/TypeScript/tsc/internal/testutil/contentmappertest" ) func TestFoldingRangeLineFoldingOnly(t *testing.T) { @@ -39,7 +40,7 @@ export function use(ctx: any): T | undefined { f, done := fourslash.NewFourslash(t, capabilities, content) defer done() - // With lineFoldingOnly, end lines should be adjusted so closing brackets stay visible. + // With lineFoldingOnly, closing lines are included unless followed by non-trivia on the same line. // Line 0: if (EMPTY_TAGs.has(tag)) { // Line 1: output += "/>"; // Line 2: } else { @@ -54,10 +55,10 @@ export function use(ctx: any): T | undefined { // Line 11: // // Line 12: } f.VerifyFoldingRangeLines(t, []fourslash.FoldingRangeLineExpected{ - {StartLine: 0, EndLine: 1}, // if block: end adjusted from line 2 to 1 - {StartLine: 2, EndLine: 7}, // else block: end adjusted from line 8 to 7 - {StartLine: 5, EndLine: 6}, // inner if block: end adjusted from line 7 to 6 - {StartLine: 10, EndLine: 11}, // function: end adjusted from line 12 to 11 + {StartLine: 0, EndLine: 1}, // if block: keep the trailing else visible + {StartLine: 2, EndLine: 8}, // else block: closing line included + {StartLine: 5, EndLine: 7}, // inner if block: closing line included + {StartLine: 10, EndLine: 12}, // function: closing line at EOF included }) } @@ -107,8 +108,147 @@ const z = 3; // Line 12: // #endregion f.VerifyFoldingRangeLines(t, []fourslash.FoldingRangeLineExpected{ {StartLine: 0, EndLine: 5}, // #region MyRegion: NOT adjusted (ends with "n", not a closing pair) - {StartLine: 2, EndLine: 3}, // function foo() block: end adjusted from line 4 to 3 + {StartLine: 2, EndLine: 4}, // function foo() block: closing line included {StartLine: 7, EndLine: 12}, // #region Outer: NOT adjusted {StartLine: 9, EndLine: 11}, // #region Inner: NOT adjusted }) } + +func TestFoldingRangeTerminatingLine(t *testing.T) { + t.Parallel() + + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `if (Math.random()) { + +} +else if (Math.random()) { + +} + +if (Math.random()) { + +} else if (Math.random()) { + +} + +if (Math.random()) { + +} /*strange, but valid*/ console.log("hi there what're you doin' on this line?") + +if (Math.random()) { + +} +/* Normal - this is totally normal code! That is why I am saying so! */ +console.log("Hello world!"); + +if (Math.random()) { + +}; + +if (Math.random()) { + +} // finally out of that ` + "`if`" + ` + +if (Math.random()) { + +console.log("Jumping into the last carriage!") } + +if (Math.random()) { + +}` + for _, test := range []struct { + lineFoldingOnly bool + expected []fourslash.FoldingRangeLineExpected + }{ + { + expected: []fourslash.FoldingRangeLineExpected{ + {StartLine: 0, EndLine: 2}, // else on following line: if block + {StartLine: 3, EndLine: 5}, // else on following line: else-if block + {StartLine: 7, EndLine: 9}, // else on same line: if block + {StartLine: 9, EndLine: 11}, // else on same line: else-if block + {StartLine: 13, EndLine: 15}, // statement on same line + {StartLine: 17, EndLine: 19}, // contents on following lines: if block + {StartLine: 20, EndLine: 20}, // contents on following lines: block comment + {StartLine: 23, EndLine: 25}, // empty statement on same line + {StartLine: 27, EndLine: 29}, // comment on same line + {StartLine: 31, EndLine: 33}, // statement before closing brace + {StartLine: 35, EndLine: 37}, // end-of-file on same line + }, + }, + { + lineFoldingOnly: true, + expected: []fourslash.FoldingRangeLineExpected{ + {StartLine: 0, EndLine: 2}, // else on following line: if block + {StartLine: 3, EndLine: 5}, // else on following line: else-if block + {StartLine: 7, EndLine: 8}, // else on same line: if block + {StartLine: 9, EndLine: 11}, // else on same line: else-if block + {StartLine: 13, EndLine: 14}, // statement on same line + {StartLine: 17, EndLine: 19}, // contents on following lines: if block + {StartLine: 20, EndLine: 20}, // contents on following lines: block comment + {StartLine: 23, EndLine: 24}, // empty statement on same line + {StartLine: 27, EndLine: 29}, // comment on same line + {StartLine: 31, EndLine: 33}, // statement before closing brace + {StartLine: 35, EndLine: 37}, // end-of-file on same line + }, + }, + } { + capabilities := &lsproto.ClientCapabilities{ + TextDocument: &lsproto.TextDocumentClientCapabilities{ + FoldingRange: &lsproto.FoldingRangeClientCapabilities{ + LineFoldingOnly: &test.lineFoldingOnly, + }, + }, + } + f, done := fourslash.NewFourslash(t, capabilities, content) + defer done() + f.VerifyFoldingRangeLines(t, test.expected) + } +} + +func TestFoldingRangeLineFoldingOnlyWithUnmappedTrailingMarkup(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + // The mapper produces this virtual TypeScript: + // + // function f() { + // const value = 1; + // } + // function __render() { + // } + // export default {}; + // + content := `// @Filename: /tsconfig.json +{ + "compilerOptions": { "strict": true }, + "contentMappers": [ + { "package": "mapper", "extensions": [".component"] } + ] +} + +// @Filename: /node_modules/mapper/package.json +` + contentmappertest.PackageJSON(contentmappertest.ComponentMapper) + ` + +// @Filename: /app.component +
Keep me visible
+` + ptrTrue := true + f, done := fourslash.NewFourslashWithOptions(t, content, &fourslash.FourslashOptions{ + Capabilities: &lsproto.ClientCapabilities{ + TextDocument: &lsproto.TextDocumentClientCapabilities{ + FoldingRange: &lsproto.FoldingRangeClientCapabilities{ + LineFoldingOnly: &ptrTrue, + }, + }, + }, + ContentMapperSpawner: contentmappertest.NewSpawner(), + RunExternalCode: true, + }) + defer done() + + f.GoToFile(t, "/app.component") + f.VerifyFoldingRangeLines(t, []fourslash.FoldingRangeLineExpected{ + {StartLine: 0, EndLine: 1}, // keep trailing host markup on line 2 visible + }) +} diff --git a/tsc/internal/ls/folding.go b/tsc/internal/ls/folding.go index 7bb733b65f29a..04b5ff3621ccf 100644 --- a/tsc/internal/ls/folding.go +++ b/tsc/internal/ls/folding.go @@ -6,6 +6,7 @@ import ( "slices" "strings" "unicode" + "unicode/utf8" "github.com/microsoft/TypeScript/tsc/internal/ast" "github.com/microsoft/TypeScript/tsc/internal/astnav" @@ -17,6 +18,7 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/printer" "github.com/microsoft/TypeScript/tsc/internal/scanner" "github.com/microsoft/TypeScript/tsc/internal/spanmap" + "github.com/microsoft/TypeScript/tsc/internal/stringutil" ) func (l *LanguageService) ProvideFoldingRange(ctx context.Context, documentURI lsproto.DocumentUri) (lsproto.FoldingRangeResponse, error) { @@ -104,7 +106,36 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc foldEndChar := sourceText[int(endOffset)-1] if foldEndChar == '}' || foldEndChar == ']' || foldEndChar == ')' || foldEndChar == '`' || foldEndChar == '>' { if r.EndLine > r.StartLine { - r.EndLine-- + if sourceFile.IsContentMapped() { + originalText := sourceFile.OriginalText() + end := lsproto.Position{Line: r.EndLine, Character: *r.EndCharacter} + offset := l.converters.FromLSPRangeToOriginal( + sourceFile, + lsproto.Range{Start: end, End: end}, + ).End() + nextPos := offset + var adjustLine bool + for nextPos < len(originalText) { + char, width := utf8.DecodeRuneInString(originalText[nextPos:]) + if !stringutil.IsWhiteSpaceLike(char) { + adjustLine = true + break + } + if char == '\r' || char == '\n' { + break + } + nextPos += width + } + if adjustLine { + r.EndLine-- + } + } else { + nextNonTriviaPos := scanner.SkipTriviaEx(sourceText, int(endOffset), &scanner.SkipTriviaOptions{StopAfterLineBreak: true}) + // non-trivia after closing bracket on the same line: adjust end line to keep closing bracket visible + if nextNonTriviaPos < len(sourceText) && scanner.GetECMALineOfPosition(sourceFile, nextNonTriviaPos) == scanner.GetECMALineOfPosition(sourceFile, int(endOffset)-1) { + r.EndLine-- + } + } } } } From 4ef3b9325a68c09f7d181f7f8b0a41bdbe43920a Mon Sep 17 00:00:00 2001 From: Gabriela Araujo Britto Date: Wed, 4 Nov 2026 09:54:49 -0700 Subject: [PATCH 2/6] Fix end line adjustment for closing bracket visibility Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tsc/internal/ls/folding.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsc/internal/ls/folding.go b/tsc/internal/ls/folding.go index cafdbd648ee04..013e7589eae6f 100644 --- a/tsc/internal/ls/folding.go +++ b/tsc/internal/ls/folding.go @@ -132,7 +132,7 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc } else { nextNonTriviaPos := scanner.SkipTriviaEx(sourceText, int(endOffset), &scanner.SkipTriviaOptions{StopAfterLineBreak: true}) // non-trivia after closing bracket on the same line: adjust end line to keep closing bracket visible - if nextNonTriviaPos < len(sourceText) && scanner.GetECMALineOfPosition(sourceFile, nextNonTriviaPos) == scanner.GetECMALineOfPosition(sourceFile, int(endOffset)-1) { + if nextNonTriviaPos < len(sourceText) && !strings.ContainsAny(sourceText[int(endOffset):nextNonTriviaPos], "\r\n") { r.EndLine-- } } From 15ffa6d859740edab4fc396e56d589bc602733dc Mon Sep 17 00:00:00 2001 From: Gabriela Araujo Britto Date: Wed, 4 Nov 2026 17:55:43 +0000 Subject: [PATCH 3/6] consider jsx text --- .../tests/foldingRangeLineFoldingOnly_test.go | 27 +++++++++++++++++++ tsc/internal/ls/folding.go | 9 ++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go index d607c7e2a4b6b..89f1e7d1ef54d 100644 --- a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go +++ b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go @@ -205,6 +205,33 @@ if (Math.random()) { } } +func TestFoldingRangeLineFoldingOnlyWithTrailingJSXText(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @jsx: preserve +// @Filename: /a.tsx +
+
+ Hello +
// Keep me visible +
` + ptrTrue := true + capabilities := &lsproto.ClientCapabilities{ + TextDocument: &lsproto.TextDocumentClientCapabilities{ + FoldingRange: &lsproto.FoldingRangeClientCapabilities{ + LineFoldingOnly: &ptrTrue, + }, + }, + } + f, done := fourslash.NewFourslash(t, capabilities, content) + defer done() + + f.VerifyFoldingRangeLines(t, []fourslash.FoldingRangeLineExpected{ + {StartLine: 0, EndLine: 4}, + {StartLine: 1, EndLine: 2}, + }) +} + func TestFoldingRangeLineFoldingOnlyWithUnmappedTrailingMarkup(t *testing.T) { t.Parallel() defer testutil.RecoverAndFail(t, "Panic on fourslash test") diff --git a/tsc/internal/ls/folding.go b/tsc/internal/ls/folding.go index 013e7589eae6f..18a54e97294fb 100644 --- a/tsc/internal/ls/folding.go +++ b/tsc/internal/ls/folding.go @@ -130,7 +130,14 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc r.EndLine-- } } else { - nextNonTriviaPos := scanner.SkipTriviaEx(sourceText, int(endOffset), &scanner.SkipTriviaOptions{StopAfterLineBreak: true}) + tokenAfterFold := astnav.GetTokenAtPosition(sourceFile, int(endOffset)) + nextNonTriviaPos := scanner.SkipTriviaEx( + sourceText, + int(endOffset), + &scanner.SkipTriviaOptions{ + StopAfterLineBreak: true, + StopAtComments: ast.IsJsxText(tokenAfterFold), + }) // non-trivia after closing bracket on the same line: adjust end line to keep closing bracket visible if nextNonTriviaPos < len(sourceText) && !strings.ContainsAny(sourceText[int(endOffset):nextNonTriviaPos], "\r\n") { r.EndLine-- From 015ebf4afbf8b1c51c484690f91571253c313005 Mon Sep 17 00:00:00 2001 From: Gabriela Araujo Britto Date: Mon, 9 Nov 2026 19:42:57 +0000 Subject: [PATCH 4/6] only omit whitespace --- .../tests/foldingRangeLineFoldingOnly_test.go | 2 +- tsc/internal/ls/folding.go | 39 ++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go index 89f1e7d1ef54d..d52fa9ea6198e 100644 --- a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go +++ b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go @@ -186,7 +186,7 @@ if (Math.random()) { {StartLine: 17, EndLine: 19}, // contents on following lines: if block {StartLine: 20, EndLine: 20}, // contents on following lines: block comment {StartLine: 23, EndLine: 24}, // empty statement on same line - {StartLine: 27, EndLine: 29}, // comment on same line + {StartLine: 27, EndLine: 28}, // comment on same line {StartLine: 31, EndLine: 33}, // statement before closing brace {StartLine: 35, EndLine: 37}, // end-of-file on same line }, diff --git a/tsc/internal/ls/folding.go b/tsc/internal/ls/folding.go index 18a54e97294fb..285486a28e85a 100644 --- a/tsc/internal/ls/folding.go +++ b/tsc/internal/ls/folding.go @@ -106,6 +106,8 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc foldEndChar := sourceText[int(endOffset)-1] if foldEndChar == '}' || foldEndChar == ']' || foldEndChar == ')' || foldEndChar == '`' || foldEndChar == '>' { if r.EndLine > r.StartLine { + var adjustLine bool + // Decide whether to adjust the end line based on the original content following the folding range if sourceFile.IsContentMapped() { originalText := sourceFile.OriginalText() end := lsproto.Position{Line: r.EndLine, Character: *r.EndCharacter} @@ -114,7 +116,6 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc lsproto.Range{Start: end, End: end}, ).End() nextPos := offset - var adjustLine bool for nextPos < len(originalText) { char, width := utf8.DecodeRuneInString(originalText[nextPos:]) if !stringutil.IsWhiteSpaceLike(char) { @@ -126,23 +127,31 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc } nextPos += width } - if adjustLine { - r.EndLine-- - } - } else { + } else { // Decide based on the TS content tokenAfterFold := astnav.GetTokenAtPosition(sourceFile, int(endOffset)) - nextNonTriviaPos := scanner.SkipTriviaEx( - sourceText, - int(endOffset), - &scanner.SkipTriviaOptions{ - StopAfterLineBreak: true, - StopAtComments: ast.IsJsxText(tokenAfterFold), - }) - // non-trivia after closing bracket on the same line: adjust end line to keep closing bracket visible - if nextNonTriviaPos < len(sourceText) && !strings.ContainsAny(sourceText[int(endOffset):nextNonTriviaPos], "\r\n") { - r.EndLine-- + isJSXText := ast.IsJsxText(tokenAfterFold) + if isJSXText { + // Keep jsx text visible + adjustLine = true + } else { + nextPos := int(endOffset) + for nextPos < len(sourceText) { + char, width := utf8.DecodeRuneInString(sourceText[nextPos:]) + if !stringutil.IsWhiteSpaceLike(char) { + // Non-whitespace after closing bracket on the same line + adjustLine = true + break + } + if char == '\r' || char == '\n' { + break + } + nextPos += width + } } } + if adjustLine { + r.EndLine-- + } } } } From 5cec8bcfae61bfde0805932e68b788e4a8a3f3a8 Mon Sep 17 00:00:00 2001 From: Gabriela Araujo Britto Date: Mon, 9 Nov 2026 20:32:53 +0000 Subject: [PATCH 5/6] only show jsx text in same line as closing bracket --- .../fourslash/tests/foldingRangeLineFoldingOnly_test.go | 7 ++++++- tsc/internal/ls/folding.go | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go index d52fa9ea6198e..e8b907573bf0c 100644 --- a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go +++ b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go @@ -214,6 +214,10 @@ func TestFoldingRangeLineFoldingOnlyWithTrailingJSXText(t *testing.T) {
Hello
// Keep me visible +
+ World +
+ // Other text ` ptrTrue := true capabilities := &lsproto.ClientCapabilities{ @@ -227,8 +231,9 @@ func TestFoldingRangeLineFoldingOnlyWithTrailingJSXText(t *testing.T) { defer done() f.VerifyFoldingRangeLines(t, []fourslash.FoldingRangeLineExpected{ - {StartLine: 0, EndLine: 4}, + {StartLine: 0, EndLine: 8}, {StartLine: 1, EndLine: 2}, + {StartLine: 4, EndLine: 6}, }) } diff --git a/tsc/internal/ls/folding.go b/tsc/internal/ls/folding.go index 285486a28e85a..994a15baaca60 100644 --- a/tsc/internal/ls/folding.go +++ b/tsc/internal/ls/folding.go @@ -130,8 +130,10 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc } else { // Decide based on the TS content tokenAfterFold := astnav.GetTokenAtPosition(sourceFile, int(endOffset)) isJSXText := ast.IsJsxText(tokenAfterFold) - if isJSXText { - // Keep jsx text visible + jsxStart := astnav.GetStartOfNode(tokenAfterFold, sourceFile, false /*includeJSDoc*/) + isJSXTextSameLine := isJSXText && !strings.ContainsAny(sourceText[int(endOffset):jsxStart], "\r\n") + if isJSXTextSameLine { + // Keep JSX text visible on same line adjustLine = true } else { nextPos := int(endOffset) From f20c3e4a7dcba6cd0d0d3279ceb0891d2d4829ff Mon Sep 17 00:00:00 2001 From: Gabriela Araujo Britto Date: Mon, 9 Nov 2026 14:01:31 -0700 Subject: [PATCH 6/6] Fix JSX text same line condition in folding.go Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tsc/internal/ls/folding.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsc/internal/ls/folding.go b/tsc/internal/ls/folding.go index 994a15baaca60..db151f466bd3f 100644 --- a/tsc/internal/ls/folding.go +++ b/tsc/internal/ls/folding.go @@ -131,7 +131,7 @@ func (l *LanguageService) adjustFoldingEnd(ranges []*lsproto.FoldingRange, sourc tokenAfterFold := astnav.GetTokenAtPosition(sourceFile, int(endOffset)) isJSXText := ast.IsJsxText(tokenAfterFold) jsxStart := astnav.GetStartOfNode(tokenAfterFold, sourceFile, false /*includeJSDoc*/) - isJSXTextSameLine := isJSXText && !strings.ContainsAny(sourceText[int(endOffset):jsxStart], "\r\n") + isJSXTextSameLine := isJSXText && jsxStart >= int(endOffset) && !strings.ContainsAny(sourceText[int(endOffset):jsxStart], "\r\n") if isJSXTextSameLine { // Keep JSX text visible on same line adjustLine = true