Skip to content

Commit 963c1f0

Browse files
author
Orta
authored
Cherry picks 36880 into 3.8 (#37082)
* Adds floating block comments to the outlining spans response * Only use one route for grabbing outline nodes, which now includes special casing the EOF token
1 parent 80a84c3 commit 963c1f0

File tree

6 files changed

+45
-8
lines changed

6 files changed

+45
-8
lines changed

src/harness/fourslashImpl.ts

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

25012501
public printOutliningSpans() {
25022502
const spans = this.languageService.getOutliningSpans(this.activeFile.fileName);
2503-
Harness.IO.log(`Outlining spans (${spans.length} items)`);
2503+
Harness.IO.log(`Outlining spans (${spans.length} items)\nResults:`);
25042504
Harness.IO.log(stringify(spans));
2505+
this.printOutliningSpansInline(spans);
2506+
}
2507+
2508+
private printOutliningSpansInline(spans: ts.OutliningSpan[]) {
2509+
const allSpanInsets = [] as { text: string, pos: number }[];
2510+
let annotated = this.activeFile.content;
2511+
ts.forEach(spans, span => {
2512+
allSpanInsets.push({ text: "[|", pos: span.textSpan.start });
2513+
allSpanInsets.push({ text: "|]", pos: span.textSpan.start + span.textSpan.length });
2514+
});
2515+
2516+
const reverseSpans = allSpanInsets.sort((l, r) => r.pos - l.pos);
2517+
ts.forEach(reverseSpans, span => {
2518+
annotated = annotated.slice(0, span.pos) + span.text + annotated.slice(span.pos);
2519+
});
2520+
Harness.IO.log(`\nMockup:\n${annotated}`);
25052521
}
25062522

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

2510-
if (actual.length !== spans.length) {
2511-
this.raiseError(`verifyOutliningSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}`);
2526+
const filterActual = ts.filter(actual, f => kind === undefined ? true : f.kind === kind);
2527+
if (filterActual.length !== spans.length) {
2528+
this.raiseError(`verifyOutliningSpans failed - expected total spans to be ${spans.length}, but was ${actual.length}\n\nFound Spans:\n\n${this.printOutliningSpansInline(actual)}`);
25122529
}
25132530

2514-
ts.zipWith(spans, actual, (expectedSpan, actualSpan, i) => {
2531+
ts.zipWith(spans, filterActual, (expectedSpan, actualSpan, i) => {
25152532
if (expectedSpan.pos !== actualSpan.textSpan.start || expectedSpan.end !== ts.textSpanEnd(actualSpan.textSpan)) {
25162533
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.textSpan.start},${ts.textSpanEnd(actualSpan.textSpan)})`);
25172534
}

src/services/outliningElementsCollector.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace ts.OutliningElementsCollector {
1010
function addNodeOutliningSpans(sourceFile: SourceFile, cancellationToken: CancellationToken, out: Push<OutliningSpan>): void {
1111
let depthRemaining = 40;
1212
let current = 0;
13-
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];
1415
const n = statements.length;
1516
while (current < n) {
1617
while (current < n && !isAnyImportSyntax(statements[current])) {
@@ -33,7 +34,7 @@ namespace ts.OutliningElementsCollector {
3334
if (depthRemaining === 0) return;
3435
cancellationToken.throwIfCancellationRequested();
3536

36-
if (isDeclaration(n)) {
37+
if (isDeclaration(n) || n.kind === SyntaxKind.EndOfFileToken) {
3738
addOutliningForLeadingCommentsForNode(n, sourceFile, cancellationToken, out);
3839
}
3940

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
// #22732
4+
5+
////[|/*
6+
///// * Some text
7+
//// */|]
8+
9+
verify.outliningHintSpansInCurrentFile(test.ranges());
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)