Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #22133: Expose getOutliningSpans on the server protocol #22400

Merged
merged 2 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5350,13 +5350,13 @@ namespace ts.projectSystem {
});

// send outlining spans request (normal priority)
session.executeCommandSeq(<protocol.OutliningSpansRequest>{
session.executeCommandSeq(<protocol.OutliningSpansRequestFull>{
command: "outliningSpans",
arguments: { file: f1.path }
});

// ensure the outlining spans request can be canceled
verifyExecuteCommandSeqIsCancellable(<protocol.OutliningSpansRequest>{
verifyExecuteCommandSeqIsCancellable(<protocol.OutliningSpansRequestFull>{
command: "outliningSpans",
arguments: { file: f1.path }
});
Expand Down
12 changes: 10 additions & 2 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,16 @@ namespace ts.server {
}));
}

getOutliningSpans(_fileName: string): OutliningSpan[] {
return notImplemented();
getOutliningSpans(file: string): OutliningSpan[] {
const request = this.processRequest<protocol.OutliningSpansRequest>(CommandNames.GetOutliningSpans, { file });
const response = this.processResponse<protocol.OutliningSpansResponse>(request);

return response.body.map<OutliningSpan>(item => ({
textSpan: this.decodeSpan(item.textSpan, file),
hintSpan: this.decodeSpan(item.hintSpan, file),
bannerText: item.bannerText,
autoCollapse: item.autoCollapse
}));
}

getTodoComments(_fileName: string, _descriptors: TodoCommentDescriptor[]): TodoComment[] {
Expand Down
40 changes: 36 additions & 4 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ namespace ts.server.protocol {
EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full",
/* @internal */
Cleanup = "cleanup",
GetOutliningSpans = "getOutliningSpans",
/* @internal */
OutliningSpans = "outliningSpans",
GetOutliningSpansFull = "outliningSpans", // Full command name is different for backward compatibility purposes
TodoComments = "todoComments",
Indentation = "indentation",
DocCommentTemplate = "docCommentTemplate",
Expand Down Expand Up @@ -303,19 +304,50 @@ namespace ts.server.protocol {
/**
* Request to obtain outlining spans in file.
*/
/* @internal */
export interface OutliningSpansRequest extends FileRequest {
command: CommandTypes.OutliningSpans;
command: CommandTypes.GetOutliningSpans;
}

export interface OutliningSpan {
/** The span of the document to actually collapse. */
textSpan: TextSpan;

/** The span of the document to display when the user hovers over the collapsed span. */
hintSpan: TextSpan;

/** The text to display in the editor for the collapsed region. */
bannerText: string;

/**
* Whether or not this region should be automatically collapsed when
* the 'Collapse to Definitions' command is invoked.
*/
autoCollapse: boolean;
}

/**
* Response to OutliningSpansRequest request.
*/
/* @internal */
export interface OutliningSpansResponse extends Response {
body?: OutliningSpan[];
}

/**
* Request to obtain outlining spans in file.
*/
/* @internal */
export interface OutliningSpansRequestFull extends FileRequest {
command: CommandTypes.GetOutliningSpansFull;
}

/**
* Response to OutliningSpansRequest request.
*/
/* @internal */
export interface OutliningSpansResponseFull extends Response {
body?: ts.OutliningSpan[];
}

/**
* A request to get indentation for a location in file
*/
Expand Down
23 changes: 19 additions & 4 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,21 @@ namespace ts.server {
return { file, project };
}

private getOutliningSpans(args: protocol.FileRequestArgs) {
private getOutliningSpans(args: protocol.FileRequestArgs, simplifiedResult: boolean): protocol.OutliningSpan[] | OutliningSpan[] {
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
return languageService.getOutliningSpans(file);
const spans = languageService.getOutliningSpans(file);
if (simplifiedResult) {
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);
return spans.map(s => ({
textSpan: this.toLocationTextSpan(s.textSpan, scriptInfo),
hintSpan: this.toLocationTextSpan(s.hintSpan, scriptInfo),
bannerText: s.bannerText,
autoCollapse: s.autoCollapse
}));
}
else {
return spans;
}
}

private getTodoComments(args: protocol.TodoCommentRequestArgs) {
Expand Down Expand Up @@ -1893,8 +1905,11 @@ namespace ts.server {
[CommandNames.QuickinfoFull]: (request: protocol.QuickInfoRequest) => {
return this.requiredResponse(this.getQuickInfoWorker(request.arguments, /*simplifiedResult*/ false));
},
[CommandNames.OutliningSpans]: (request: protocol.FileRequest) => {
return this.requiredResponse(this.getOutliningSpans(request.arguments));
[CommandNames.GetOutliningSpans]: (request: protocol.FileRequest) => {
return this.requiredResponse(this.getOutliningSpans(request.arguments, /*simplifiedResult*/ true));
},
[CommandNames.GetOutliningSpansFull]: (request: protocol.FileRequest) => {
return this.requiredResponse(this.getOutliningSpans(request.arguments, /*simplifiedResult*/ false));
},
[CommandNames.TodoComments]: (request: protocol.TodoCommentRequest) => {
return this.requiredResponse(this.getTodoComments(request.arguments));
Expand Down
28 changes: 27 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5067,6 +5067,7 @@ declare namespace ts.server.protocol {
OpenExternalProject = "openExternalProject",
OpenExternalProjects = "openExternalProjects",
CloseExternalProject = "closeExternalProject",
GetOutliningSpans = "getOutliningSpans",
TodoComments = "todoComments",
Indentation = "indentation",
DocCommentTemplate = "docCommentTemplate",
Expand Down Expand Up @@ -5225,6 +5226,31 @@ declare namespace ts.server.protocol {
*/
onlyMultiLine: boolean;
}
/**
* Request to obtain outlining spans in file.
*/
interface OutliningSpansRequest extends FileRequest {
command: CommandTypes.GetOutliningSpans;
}
interface OutliningSpan {
/** The span of the document to actually collapse. */
textSpan: TextSpan;
/** The span of the document to display when the user hovers over the collapsed span. */
hintSpan: TextSpan;
/** The text to display in the editor for the collapsed region. */
bannerText: string;
/**
* Whether or not this region should be automatically collapsed when
* the 'Collapse to Definitions' command is invoked.
*/
autoCollapse: boolean;
}
/**
* Response to OutliningSpansRequest request.
*/
interface OutliningSpansResponse extends Response {
body?: OutliningSpan[];
}
/**
* A request to get indentation for a location in file
*/
Expand Down Expand Up @@ -7276,7 +7302,7 @@ declare namespace ts.server {
private getFileAndProject(args);
private getFileAndLanguageServiceForSyntacticOperation(args);
private getFileAndProjectWorker(uncheckedFileName, projectFileName);
private getOutliningSpans(args);
private getOutliningSpans(args, simplifiedResult);
private getTodoComments(args);
private getDocCommentTemplate(args);
private getSpanOfEnclosingComment(args);
Expand Down
53 changes: 53 additions & 0 deletions tests/cases/fourslash/server/getOutliningSpansForRegions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/// <reference path="../fourslash.ts"/>

////// region without label
////[|// #region
////
////// #endregion|]
////
////// region without label with trailing spaces
////[|// #region
////
////// #endregion|]
////
////// region with label
////[|// #region label1
////
////// #endregion|]
////
////// region with extra whitespace in all valid locations
//// [|// #region label2 label3
////
//// // #endregion|]
////
////// No space before directive
////[|//#region label4
////
//////#endregion|]
////
////// Nested regions
////[|// #region outer
////
////[|// #region inner
////
////// #endregion inner|]
////
////// #endregion outer|]
////
////// region delimiters not valid when there is preceding text on line
//// test // #region invalid1
////
////test // #endregion
////
////// region delimiters not valid when in multiline comment
/////*
////// #region invalid2
////*/
////
/////*
////// #endregion
////*/

debugger;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be removed?


verify.outliningSpansInCurrentFile(test.ranges());