diff --git a/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go b/tsc/internal/fourslash/tests/foldingRangeLineFoldingOnly_test.go index 2a8fa6a7c13be..e8b907573bf0c 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,179 @@ 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: 28}, // 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 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 +
+ World +
+ // Other text +
` + 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: 8}, + {StartLine: 1, EndLine: 2}, + {StartLine: 4, EndLine: 6}, + }) +} + +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 053a988bbf8f8..db151f466bd3f 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,54 @@ 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-- + 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} + offset := l.converters.FromLSPRangeToOriginal( + sourceFile, + lsproto.Range{Start: end, End: end}, + ).End() + nextPos := offset + 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 + } + } else { // Decide based on the TS content + tokenAfterFold := astnav.GetTokenAtPosition(sourceFile, int(endOffset)) + isJSXText := ast.IsJsxText(tokenAfterFold) + jsxStart := astnav.GetStartOfNode(tokenAfterFold, sourceFile, false /*includeJSDoc*/) + isJSXTextSameLine := isJSXText && jsxStart >= int(endOffset) && !strings.ContainsAny(sourceText[int(endOffset):jsxStart], "\r\n") + if isJSXTextSameLine { + // Keep JSX text visible on same line + 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-- + } } } }