Skip to content

Commit e52efb0

Browse files
committed
Fix microsoft#22419: Add kind field to OutliningSpan
1 parent 23b9a23 commit e52efb0

15 files changed

Lines changed: 73 additions & 24 deletions

src/harness/fourslash.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,13 @@ Actual: ${stringify(fullActual)}`);
24612461
this.verifyClassifications(expected, actual, this.activeFile.content);
24622462
}
24632463

2464-
public verifyOutliningSpans(spans: Range[]) {
2464+
public printOutliningSpans() {
2465+
const spans = this.languageService.getOutliningSpans(this.activeFile.fileName);
2466+
Harness.IO.log(`Outlining spans (${spans.length} items)`);
2467+
Harness.IO.log(stringify(spans));
2468+
}
2469+
2470+
public verifyOutliningSpans(spans: Range[], kind?: "comment" | "region" | "code") {
24652471
const actual = this.languageService.getOutliningSpans(this.activeFile.fileName);
24662472

24672473
if (actual.length !== spans.length) {
@@ -2470,7 +2476,10 @@ Actual: ${stringify(fullActual)}`);
24702476

24712477
ts.zipWith(spans, actual, (expectedSpan, actualSpan, i) => {
24722478
if (expectedSpan.pos !== actualSpan.textSpan.start || expectedSpan.end !== ts.textSpanEnd(actualSpan.textSpan)) {
2473-
this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.textSpan.start},${ts.textSpanEnd(actualSpan.textSpan)})`);
2479+
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.textSpan.start},${ts.textSpanEnd(actualSpan.textSpan)})`);
2480+
}
2481+
if (kind !== undefined && actualSpan.kind !== kind) {
2482+
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected kind: ('${kind}'), actual: ('${actualSpan.kind}')`);
24742483
}
24752484
});
24762485
}
@@ -4290,8 +4299,8 @@ namespace FourSlashInterface {
42904299
this.state.verifyCurrentNameOrDottedNameSpanText(text);
42914300
}
42924301

4293-
public outliningSpansInCurrentFile(spans: FourSlash.Range[]) {
4294-
this.state.verifyOutliningSpans(spans);
4302+
public outliningSpansInCurrentFile(spans: FourSlash.Range[], kind?: "comment" | "region" | "code") {
4303+
this.state.verifyOutliningSpans(spans, kind);
42954304
}
42964305

42974306
public todoCommentsInCurrentFile(descriptors: string[]) {
@@ -4585,6 +4594,10 @@ namespace FourSlashInterface {
45854594
public printContext() {
45864595
this.state.printContext();
45874596
}
4597+
4598+
public printOutliningSpans() {
4599+
this.state.printOutliningSpans();
4600+
}
45884601
}
45894602

45904603
export class Format {

src/server/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ namespace ts.server {
530530
textSpan: this.decodeSpan(item.textSpan, file),
531531
hintSpan: this.decodeSpan(item.hintSpan, file),
532532
bannerText: item.bannerText,
533-
autoCollapse: item.autoCollapse
533+
autoCollapse: item.autoCollapse,
534+
kind: item.kind
534535
}));
535536
}
536537

src/server/protocol.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ namespace ts.server.protocol {
326326
* the 'Collapse to Definitions' command is invoked.
327327
*/
328328
autoCollapse: boolean;
329+
330+
/**
331+
* Classification of the contents of the span
332+
*/
333+
kind: OutliningSpanKind;
329334
}
330335

331336
/**

src/server/session.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,8 @@ namespace ts.server {
11121112
textSpan: this.toLocationTextSpan(s.textSpan, scriptInfo),
11131113
hintSpan: this.toLocationTextSpan(s.hintSpan, scriptInfo),
11141114
bannerText: s.bannerText,
1115-
autoCollapse: s.autoCollapse
1115+
autoCollapse: s.autoCollapse,
1116+
kind: s.kind
11161117
}));
11171118
}
11181119
else {

src/services/outliningElementsCollector.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace ts.OutliningElementsCollector {
5050

5151
if (!result[1]) {
5252
const span = createTextSpanFromBounds(sourceFile.text.indexOf("//", currentLineStart), lineEnd);
53-
regions.push(createOutliningSpan(span, span, /*autoCollapse*/ false, result[2] || "#region"));
53+
regions.push(createOutliningSpan(span, OutliningSpanKind.Region, span, /*autoCollapse*/ false, result[2] || "#region"));
5454
}
5555
else {
5656
const region = regions.pop();
@@ -83,7 +83,7 @@ namespace ts.OutliningElementsCollector {
8383
break;
8484
case SyntaxKind.MultiLineCommentTrivia:
8585
combineAndAddMultipleSingleLineComments();
86-
out.push(createOutliningSpanFromBounds(pos, end));
86+
out.push(createOutliningSpanFromBounds(pos, end, OutliningSpanKind.Comment));
8787
singleLineCommentCount = 0;
8888
break;
8989
default:
@@ -95,13 +95,13 @@ namespace ts.OutliningElementsCollector {
9595
function combineAndAddMultipleSingleLineComments(): void {
9696
// Only outline spans of two or more consecutive single line comments
9797
if (singleLineCommentCount > 1) {
98-
out.push(createOutliningSpanFromBounds(firstSingleLineCommentStart, lastSingleLineCommentEnd));
98+
out.push(createOutliningSpanFromBounds(firstSingleLineCommentStart, lastSingleLineCommentEnd, OutliningSpanKind.Comment));
9999
}
100100
}
101101
}
102102

103-
function createOutliningSpanFromBounds(pos: number, end: number): OutliningSpan {
104-
return createOutliningSpan(createTextSpanFromBounds(pos, end));
103+
function createOutliningSpanFromBounds(pos: number, end: number, kind: OutliningSpanKind): OutliningSpan {
104+
return createOutliningSpan(createTextSpanFromBounds(pos, end), kind);
105105
}
106106

107107
function getOutliningSpanForNode(n: Node, sourceFile: SourceFile): OutliningSpan | undefined {
@@ -136,7 +136,7 @@ namespace ts.OutliningElementsCollector {
136136
default:
137137
// Block was a standalone block. In this case we want to only collapse
138138
// the span of the block, independent of any parent span.
139-
return createOutliningSpan(createTextSpanFromNode(n, sourceFile));
139+
return createOutliningSpan(createTextSpanFromNode(n, sourceFile), OutliningSpanKind.Code);
140140
}
141141
case SyntaxKind.ModuleBlock:
142142
return spanForNode(n.parent);
@@ -166,11 +166,11 @@ namespace ts.OutliningElementsCollector {
166166
return undefined;
167167
}
168168
const textSpan = createTextSpanFromBounds(useFullStart ? openToken.getFullStart() : openToken.getStart(sourceFile), closeToken.getEnd());
169-
return createOutliningSpan(textSpan, createTextSpanFromNode(hintSpanNode, sourceFile), autoCollapse);
169+
return createOutliningSpan(textSpan, OutliningSpanKind.Code, createTextSpanFromNode(hintSpanNode, sourceFile), autoCollapse);
170170
}
171171
}
172172

173-
function createOutliningSpan(textSpan: TextSpan, hintSpan: TextSpan = textSpan, autoCollapse = false, bannerText = "..."): OutliningSpan {
174-
return { textSpan, hintSpan, bannerText, autoCollapse };
173+
function createOutliningSpan(textSpan: TextSpan, kind: OutliningSpanKind, hintSpan: TextSpan = textSpan, autoCollapse = false, bannerText = "..."): OutliningSpan {
174+
return { textSpan, kind, hintSpan, bannerText, autoCollapse };
175175
}
176-
}
176+
}

src/services/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,17 @@ namespace ts {
816816
* the 'Collapse to Definitions' command is invoked.
817817
*/
818818
autoCollapse: boolean;
819+
820+
/**
821+
* Classification of the contents of the span
822+
*/
823+
kind: OutliningSpanKind;
824+
}
825+
826+
export const enum OutliningSpanKind {
827+
Comment = "comment",
828+
Region = "region",
829+
Code = "code"
819830
}
820831

821832
export const enum OutputFileType {

tests/cases/fourslash/getOutliningForObjectsInArray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@
5353
//// ]|]
5454
//// ]|];
5555

56-
verify.outliningSpansInCurrentFile(test.ranges());
56+
verify.outliningSpansInCurrentFile(test.ranges(), "code");

tests/cases/fourslash/getOutliningSpans.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@
9797
////class AfterNestedNodes[| {
9898
////}|]
9999

100-
verify.outliningSpansInCurrentFile(test.ranges());
100+
verify.outliningSpansInCurrentFile(test.ranges(), "code");

tests/cases/fourslash/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());
51+
verify.outliningSpansInCurrentFile(test.ranges(), "region");

tests/cases/fourslash/getOutliningSpansForUnbalancedEndRegion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
////
88
////// #endregion unmatched
99

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

0 commit comments

Comments
 (0)