Skip to content

Commit 1a320fc

Browse files
authored
Merge pull request microsoft#23883 from Microsoft/outliningSpanKind
Fix microsoft#22419: Add `kind` field to `OutliningSpan`
2 parents 835e325 + 0b902bf commit 1a320fc

17 files changed

+95
-24
lines changed

src/harness/fourslash.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,13 @@ Actual: ${stringify(fullActual)}`);
24642464
this.verifyClassifications(expected, actual, this.activeFile.content);
24652465
}
24662466

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

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

24742480
ts.zipWith(spans, actual, (expectedSpan, actualSpan, i) => {
24752481
if (expectedSpan.pos !== actualSpan.textSpan.start || expectedSpan.end !== ts.textSpanEnd(actualSpan.textSpan)) {
2476-
this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.textSpan.start},${ts.textSpanEnd(actualSpan.textSpan)})`);
2482+
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected: (${expectedSpan.pos},${expectedSpan.end}), actual: (${actualSpan.textSpan.start},${ts.textSpanEnd(actualSpan.textSpan)})`);
2483+
}
2484+
if (kind !== undefined && actualSpan.kind !== kind) {
2485+
return this.raiseError(`verifyOutliningSpans failed - span ${(i + 1)} expected kind: ('${kind}'), actual: ('${actualSpan.kind}')`);
24772486
}
24782487
});
24792488
}
@@ -4293,8 +4302,8 @@ namespace FourSlashInterface {
42934302
this.state.verifyCurrentNameOrDottedNameSpanText(text);
42944303
}
42954304

4296-
public outliningSpansInCurrentFile(spans: FourSlash.Range[]) {
4297-
this.state.verifyOutliningSpans(spans);
4305+
public outliningSpansInCurrentFile(spans: FourSlash.Range[], kind?: "comment" | "region" | "code") {
4306+
this.state.verifyOutliningSpans(spans, kind);
42984307
}
42994308

43004309
public todoCommentsInCurrentFile(descriptors: string[]) {
@@ -4588,6 +4597,10 @@ namespace FourSlashInterface {
45884597
public printContext() {
45894598
this.state.printContext();
45904599
}
4600+
4601+
public printOutliningSpans() {
4602+
this.state.printOutliningSpans();
4603+
}
45914604
}
45924605

45934606
export class Format {

src/server/client.ts

+2-1
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

+5
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

+2-1
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

+10-10
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

+11
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/baselines/reference/api/tsserverlibrary.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -4872,6 +4872,15 @@ declare namespace ts {
48724872
* the 'Collapse to Definitions' command is invoked.
48734873
*/
48744874
autoCollapse: boolean;
4875+
/**
4876+
* Classification of the contents of the span
4877+
*/
4878+
kind: OutliningSpanKind;
4879+
}
4880+
enum OutliningSpanKind {
4881+
Comment = "comment",
4882+
Region = "region",
4883+
Code = "code"
48754884
}
48764885
enum OutputFileType {
48774886
JavaScript = 0,
@@ -5594,6 +5603,10 @@ declare namespace ts.server.protocol {
55945603
* the 'Collapse to Definitions' command is invoked.
55955604
*/
55965605
autoCollapse: boolean;
5606+
/**
5607+
* Classification of the contents of the span
5608+
*/
5609+
kind: OutliningSpanKind;
55975610
}
55985611
/**
55995612
* Response to OutliningSpansRequest request.

tests/baselines/reference/api/typescript.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -4872,6 +4872,15 @@ declare namespace ts {
48724872
* the 'Collapse to Definitions' command is invoked.
48734873
*/
48744874
autoCollapse: boolean;
4875+
/**
4876+
* Classification of the contents of the span
4877+
*/
4878+
kind: OutliningSpanKind;
4879+
}
4880+
enum OutliningSpanKind {
4881+
Comment = "comment",
4882+
Region = "region",
4883+
Code = "code"
48754884
}
48764885
enum OutputFileType {
48774886
JavaScript = 0,

tests/cases/fourslash/getOutliningForObjectsInArray.ts

+1-1
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

+1-1
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

+1-1
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

+1-1
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");

tests/cases/fourslash/getOutliningSpansForUnbalancedRegion.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
////
88
////// #endregion matched|]
99

10-
verify.outliningSpansInCurrentFile(test.ranges());
10+
verify.outliningSpansInCurrentFile(test.ranges(), "region");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
////[|/*
4+
//// Block comment at the beginning of the file before module:
5+
//// line one of the comment
6+
//// line two of the comment
7+
//// line three
8+
//// line four
9+
//// line five
10+
////*/|]
11+
////declare module "m";
12+
////[|// Single line comments at the start of the file
13+
////// line 2
14+
////// line 3
15+
////// line 4|]
16+
////declare module "n";
17+
18+
verify.outliningSpansInCurrentFile(test.ranges(), "comment");

tests/cases/fourslash/server/getOutliningSpansForRegions.ts

+1-1
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/shims-pp/getOutliningSpans.ts

+1-1
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/shims/getOutliningSpans.ts

+1-1
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");

0 commit comments

Comments
 (0)