Skip to content

Commit c335c38

Browse files
authored
Fix #22133: Expose getOutliningSpans on the server protocol (#22400)
* Fix #22133: Expose getOutliningSpans on the server protocol * Remove debugger statement from test
1 parent 1fc3aeb commit c335c38

File tree

6 files changed

+145
-13
lines changed

6 files changed

+145
-13
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5350,13 +5350,13 @@ namespace ts.projectSystem {
53505350
});
53515351

53525352
// send outlining spans request (normal priority)
5353-
session.executeCommandSeq(<protocol.OutliningSpansRequest>{
5353+
session.executeCommandSeq(<protocol.OutliningSpansRequestFull>{
53545354
command: "outliningSpans",
53555355
arguments: { file: f1.path }
53565356
});
53575357

53585358
// ensure the outlining spans request can be canceled
5359-
verifyExecuteCommandSeqIsCancellable(<protocol.OutliningSpansRequest>{
5359+
verifyExecuteCommandSeqIsCancellable(<protocol.OutliningSpansRequestFull>{
53605360
command: "outliningSpans",
53615361
arguments: { file: f1.path }
53625362
});

src/server/client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,16 @@ namespace ts.server {
522522
}));
523523
}
524524

525-
getOutliningSpans(_fileName: string): OutliningSpan[] {
526-
return notImplemented();
525+
getOutliningSpans(file: string): OutliningSpan[] {
526+
const request = this.processRequest<protocol.OutliningSpansRequest>(CommandNames.GetOutliningSpans, { file });
527+
const response = this.processResponse<protocol.OutliningSpansResponse>(request);
528+
529+
return response.body.map<OutliningSpan>(item => ({
530+
textSpan: this.decodeSpan(item.textSpan, file),
531+
hintSpan: this.decodeSpan(item.hintSpan, file),
532+
bannerText: item.bannerText,
533+
autoCollapse: item.autoCollapse
534+
}));
527535
}
528536

529537
getTodoComments(_fileName: string, _descriptors: TodoCommentDescriptor[]): TodoComment[] {

src/server/protocol.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ namespace ts.server.protocol {
9191
EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full",
9292
/* @internal */
9393
Cleanup = "cleanup",
94+
GetOutliningSpans = "getOutliningSpans",
9495
/* @internal */
95-
OutliningSpans = "outliningSpans",
96+
GetOutliningSpansFull = "outliningSpans", // Full command name is different for backward compatibility purposes
9697
TodoComments = "todoComments",
9798
Indentation = "indentation",
9899
DocCommentTemplate = "docCommentTemplate",
@@ -303,19 +304,50 @@ namespace ts.server.protocol {
303304
/**
304305
* Request to obtain outlining spans in file.
305306
*/
306-
/* @internal */
307307
export interface OutliningSpansRequest extends FileRequest {
308-
command: CommandTypes.OutliningSpans;
308+
command: CommandTypes.GetOutliningSpans;
309+
}
310+
311+
export interface OutliningSpan {
312+
/** The span of the document to actually collapse. */
313+
textSpan: TextSpan;
314+
315+
/** The span of the document to display when the user hovers over the collapsed span. */
316+
hintSpan: TextSpan;
317+
318+
/** The text to display in the editor for the collapsed region. */
319+
bannerText: string;
320+
321+
/**
322+
* Whether or not this region should be automatically collapsed when
323+
* the 'Collapse to Definitions' command is invoked.
324+
*/
325+
autoCollapse: boolean;
309326
}
310327

311328
/**
312329
* Response to OutliningSpansRequest request.
313330
*/
314-
/* @internal */
315331
export interface OutliningSpansResponse extends Response {
316332
body?: OutliningSpan[];
317333
}
318334

335+
/**
336+
* Request to obtain outlining spans in file.
337+
*/
338+
/* @internal */
339+
export interface OutliningSpansRequestFull extends FileRequest {
340+
command: CommandTypes.GetOutliningSpansFull;
341+
}
342+
343+
/**
344+
* Response to OutliningSpansRequest request.
345+
*/
346+
/* @internal */
347+
export interface OutliningSpansResponseFull extends Response {
348+
body?: ts.OutliningSpan[];
349+
}
350+
319351
/**
320352
* A request to get indentation for a location in file
321353
*/

src/server/session.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,9 +1087,21 @@ namespace ts.server {
10871087
return { file, project };
10881088
}
10891089

1090-
private getOutliningSpans(args: protocol.FileRequestArgs) {
1090+
private getOutliningSpans(args: protocol.FileRequestArgs, simplifiedResult: boolean): protocol.OutliningSpan[] | OutliningSpan[] {
10911091
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
1092-
return languageService.getOutliningSpans(file);
1092+
const spans = languageService.getOutliningSpans(file);
1093+
if (simplifiedResult) {
1094+
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);
1095+
return spans.map(s => ({
1096+
textSpan: this.toLocationTextSpan(s.textSpan, scriptInfo),
1097+
hintSpan: this.toLocationTextSpan(s.hintSpan, scriptInfo),
1098+
bannerText: s.bannerText,
1099+
autoCollapse: s.autoCollapse
1100+
}));
1101+
}
1102+
else {
1103+
return spans;
1104+
}
10931105
}
10941106

10951107
private getTodoComments(args: protocol.TodoCommentRequestArgs) {
@@ -1893,8 +1905,11 @@ namespace ts.server {
18931905
[CommandNames.QuickinfoFull]: (request: protocol.QuickInfoRequest) => {
18941906
return this.requiredResponse(this.getQuickInfoWorker(request.arguments, /*simplifiedResult*/ false));
18951907
},
1896-
[CommandNames.OutliningSpans]: (request: protocol.FileRequest) => {
1897-
return this.requiredResponse(this.getOutliningSpans(request.arguments));
1908+
[CommandNames.GetOutliningSpans]: (request: protocol.FileRequest) => {
1909+
return this.requiredResponse(this.getOutliningSpans(request.arguments, /*simplifiedResult*/ true));
1910+
},
1911+
[CommandNames.GetOutliningSpansFull]: (request: protocol.FileRequest) => {
1912+
return this.requiredResponse(this.getOutliningSpans(request.arguments, /*simplifiedResult*/ false));
18981913
},
18991914
[CommandNames.TodoComments]: (request: protocol.TodoCommentRequest) => {
19001915
return this.requiredResponse(this.getTodoComments(request.arguments));

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5063,6 +5063,7 @@ declare namespace ts.server.protocol {
50635063
OpenExternalProject = "openExternalProject",
50645064
OpenExternalProjects = "openExternalProjects",
50655065
CloseExternalProject = "closeExternalProject",
5066+
GetOutliningSpans = "getOutliningSpans",
50665067
TodoComments = "todoComments",
50675068
Indentation = "indentation",
50685069
DocCommentTemplate = "docCommentTemplate",
@@ -5221,6 +5222,31 @@ declare namespace ts.server.protocol {
52215222
*/
52225223
onlyMultiLine: boolean;
52235224
}
5225+
/**
5226+
* Request to obtain outlining spans in file.
5227+
*/
5228+
interface OutliningSpansRequest extends FileRequest {
5229+
command: CommandTypes.GetOutliningSpans;
5230+
}
5231+
interface OutliningSpan {
5232+
/** The span of the document to actually collapse. */
5233+
textSpan: TextSpan;
5234+
/** The span of the document to display when the user hovers over the collapsed span. */
5235+
hintSpan: TextSpan;
5236+
/** The text to display in the editor for the collapsed region. */
5237+
bannerText: string;
5238+
/**
5239+
* Whether or not this region should be automatically collapsed when
5240+
* the 'Collapse to Definitions' command is invoked.
5241+
*/
5242+
autoCollapse: boolean;
5243+
}
5244+
/**
5245+
* Response to OutliningSpansRequest request.
5246+
*/
5247+
interface OutliningSpansResponse extends Response {
5248+
body?: OutliningSpan[];
5249+
}
52245250
/**
52255251
* A request to get indentation for a location in file
52265252
*/
@@ -7272,7 +7298,7 @@ declare namespace ts.server {
72727298
private getFileAndProject(args);
72737299
private getFileAndLanguageServiceForSyntacticOperation(args);
72747300
private getFileAndProjectWorker(uncheckedFileName, projectFileName);
7275-
private getOutliningSpans(args);
7301+
private getOutliningSpans(args, simplifiedResult);
72767302
private getTodoComments(args);
72777303
private getDocCommentTemplate(args);
72787304
private getSpanOfEnclosingComment(args);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
////// region without label
4+
////[|// #region
5+
////
6+
////// #endregion|]
7+
////
8+
////// region without label with trailing spaces
9+
////[|// #region
10+
////
11+
////// #endregion|]
12+
////
13+
////// region with label
14+
////[|// #region label1
15+
////
16+
////// #endregion|]
17+
////
18+
////// region with extra whitespace in all valid locations
19+
//// [|// #region label2 label3
20+
////
21+
//// // #endregion|]
22+
////
23+
////// No space before directive
24+
////[|//#region label4
25+
////
26+
//////#endregion|]
27+
////
28+
////// Nested regions
29+
////[|// #region outer
30+
////
31+
////[|// #region inner
32+
////
33+
////// #endregion inner|]
34+
////
35+
////// #endregion outer|]
36+
////
37+
////// region delimiters not valid when there is preceding text on line
38+
//// test // #region invalid1
39+
////
40+
////test // #endregion
41+
////
42+
////// region delimiters not valid when in multiline comment
43+
/////*
44+
////// #region invalid2
45+
////*/
46+
////
47+
/////*
48+
////// #endregion
49+
////*/
50+
51+
verify.outliningSpansInCurrentFile(test.ranges());

0 commit comments

Comments
 (0)