Skip to content

Commit 143d0d5

Browse files
committed
Only use one route for grabbing outline nodes, which now includes special casing the EOF token
1 parent 55be7b4 commit 143d0d5

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

src/harness/fourslashImpl.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,18 +2469,35 @@ namespace FourSlash {
24692469

24702470
public printOutliningSpans() {
24712471
const spans = this.languageService.getOutliningSpans(this.activeFile.fileName);
2472-
Harness.IO.log(`Outlining spans (${spans.length} items)`);
2472+
Harness.IO.log(`Outlining spans (${spans.length} items)\nResults:`);
24732473
Harness.IO.log(stringify(spans));
2474+
this.printOutliningSpansInline(spans);
2475+
}
2476+
2477+
private printOutliningSpansInline(spans: ts.OutliningSpan[]) {
2478+
const allSpanInsets = [] as { text: string, pos: number }[];
2479+
let annotated = this.activeFile.content;
2480+
ts.forEach(spans, span => {
2481+
allSpanInsets.push({ text: "[|", pos: span.textSpan.start });
2482+
allSpanInsets.push({ text: "|]", pos: span.textSpan.start + span.textSpan.length });
2483+
});
2484+
2485+
const reverseSpans = allSpanInsets.sort((l, r) => r.pos - l.pos);
2486+
ts.forEach(reverseSpans, span => {
2487+
annotated = annotated.slice(0, span.pos) + span.text + annotated.slice(span.pos);
2488+
});
2489+
Harness.IO.log(`\nMockup:\n${annotated}`);
24742490
}
24752491

24762492
public verifyOutliningSpans(spans: Range[], kind?: "comment" | "region" | "code" | "imports") {
24772493
const actual = this.languageService.getOutliningSpans(this.activeFile.fileName);
24782494

2479-
if (actual.length !== spans.length) {
2480-
this.raiseError(`verifyOutliningSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}`);
2495+
const filterActual = ts.filter(actual, f => kind === undefined ? true : f.kind === kind);
2496+
if (filterActual.length !== spans.length) {
2497+
this.raiseError(`verifyOutliningSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}\n\nFound Spans:\n\n${this.printOutliningSpansInline(actual)}`);
24812498
}
24822499

2483-
ts.zipWith(spans, actual, (expectedSpan, actualSpan, i) => {
2500+
ts.zipWith(spans, filterActual, (expectedSpan, actualSpan, i) => {
24842501
if (expectedSpan.pos !== actualSpan.textSpan.start || expectedSpan.end !== ts.textSpanEnd(actualSpan.textSpan)) {
24852502
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.textSpan.start},${ts.textSpanEnd(actualSpan.textSpan)})`);
24862503
}

src/services/outliningElementsCollector.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ namespace ts.OutliningElementsCollector {
44
const res: OutliningSpan[] = [];
55
addNodeOutliningSpans(sourceFile, cancellationToken, res);
66
addRegionOutliningSpans(sourceFile, res);
7-
addTopLevelUnattachedCommentSpans(sourceFile, res);
87
return res.sort((span1, span2) => span1.textSpan.start - span2.textSpan.start);
98
}
109

1110
function addNodeOutliningSpans(sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
1211
let depthRemaining = 40;
1312
let current = 0;
14-
const statements = sourceFile.statements;
13+
// Includes the EOF Token so that comments which aren't attached to statements are included
14+
const statements = [...sourceFile.statements, sourceFile.endOfFileToken];
1515
const n = statements.length;
1616
while (current < n) {
1717
while (current < n && !isAnyImportSyntax(statements[current])) {
@@ -34,7 +34,7 @@ namespace ts.OutliningElementsCollector {
3434
if (depthRemaining === 0) return;
3535
cancellationToken.throwIfCancellationRequested();
3636

37-
if (isDeclaration(n)) {
37+
if (isDeclaration(n) || n.kind === SyntaxKind.EndOfFileToken) {
3838
addOutliningForLeadingCommentsForNode(n, sourceFile, cancellationToken, out);
3939
}
4040

@@ -107,23 +107,6 @@ namespace ts.OutliningElementsCollector {
107107
return regionDelimiterRegExp.exec(lineText);
108108
}
109109

110-
function addTopLevelUnattachedCommentSpans(sourceFile: SourceFile, out: Push<OutliningSpan>): void {
111-
// Comments which are attached to statements would be included in addNodeOutliningSpans
112-
if (sourceFile.statements.length > 0) return;
113-
114-
// This will instead set up spans for the missing ones
115-
forEach(getLeadingCommentRangesOfNode(sourceFile, sourceFile), (range) => {
116-
// To not mess with // #region support
117-
const isMultiline = sourceFile.text.substring(range.pos, 2) === "/*";
118-
if (!isMultiline) return;
119-
120-
const span = createTextSpanFromBounds(range.pos, range.end);
121-
const comment = sourceFile.text.substring(range.pos, range.end);
122-
const outline = createOutliningSpan(span, OutliningSpanKind.Comment, span, /*autoCollapse*/ false, comment);
123-
out.push(outline);
124-
});
125-
}
126-
127110
function addOutliningForLeadingCommentsForNode(n: Node, sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
128111
const comments = getLeadingCommentRangesOfNode(n, sourceFile);
129112
if (!comments) return;
@@ -289,6 +272,9 @@ namespace ts.OutliningElementsCollector {
289272
}
290273

291274
function createOutliningSpan(textSpan: TextSpan, kind: OutliningSpanKind, hintSpan: TextSpan = textSpan, autoCollapse = false, bannerText = "..."): OutliningSpan {
275+
if(kind === OutliningSpanKind.Region) {
276+
debugger;
277+
}
292278
return { textSpan, kind, hintSpan, bannerText, autoCollapse };
293279
}
294280
}

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ declare namespace FourSlashInterface {
317317
baselineQuickInfo(): void;
318318
baselineSmartSelection(): void;
319319
nameOrDottedNameSpanTextIs(text: string): void;
320-
outliningSpansInCurrentFile(spans: Range[]): void;
320+
outliningSpansInCurrentFile(spans: Range[], kind?: "comment" | "region" | "code" | "imports"): void;
321321
outliningHintSpansInCurrentFile(spans: Range[]): void;
322322
todoCommentsInCurrentFile(descriptors: string[]): void;
323323
matchingBracePositionInCurrentFile(bracePosition: number, expectedMatchPosition: number): void;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
// #22732
4+
5+
////console.log(0);
6+
////[|/*
7+
///// * Some text
8+
//// */|]
9+
10+
verify.outliningHintSpansInCurrentFile(test.ranges());

tests/cases/fourslash/server/getOutliningSpansForRegions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
////// #endregion
4949
////*/
5050

51-
verify.outliningSpansInCurrentFile(test.ranges(), "region");
51+
verify.outliningSpansInCurrentFile(test.ranges(), "region");

0 commit comments

Comments
 (0)