diff --git a/src/server/protocol.ts b/src/server/protocol.ts index cc027f8e950..597c69d474f 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -136,12 +136,16 @@ namespace ts.server.protocol { SelectionRange = "selectionRange", /* @internal */ SelectionRangeFull = "selectionRange-full", + ToggleLineComment = "toggleLineComment", /* @internal */ ToggleLineCommentFull = "toggleLineComment-full", + ToggleMultilineComment = "toggleMultilineComment", /* @internal */ ToggleMultilineCommentFull = "toggleMultilineComment-full", + CommentSelection = "commentSelection", /* @internal */ CommentSelectionFull = "commentSelection-full", + UncommentSelection = "uncommentSelection", /* @internal */ UncommentSelectionFull = "uncommentSelection-full", PrepareCallHierarchy = "prepareCallHierarchy", @@ -1540,8 +1544,23 @@ namespace ts.server.protocol { parent?: SelectionRange; } - export interface CommentSelectionRequest extends FileRequest { + export interface ToggleLineCommentRequest extends FileRequest { + command: CommandTypes.ToggleLineComment; + arguments: FileRangeRequestArgs; + } + export interface ToggleMultilineCommentRequest extends FileRequest { + command: CommandTypes.ToggleMultilineComment; + arguments: FileRangeRequestArgs; + } + + export interface CommentSelectionRequest extends FileRequest { + command: CommandTypes.CommentSelection; + arguments: FileRangeRequestArgs; + } + + export interface UncommentSelectionRequest extends FileRequest { + command: CommandTypes.UncommentSelection; arguments: FileRangeRequestArgs; } diff --git a/src/server/session.ts b/src/server/session.ts index 900564f934d..c6cc9084e89 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -2201,36 +2201,68 @@ namespace ts.server { }); } - private toggleLineComment(args: protocol.FileRangeRequestArgs): TextChange[] { + private toggleLineComment(args: protocol.FileRangeRequestArgs, simplifiedResult: boolean): TextChange[] | protocol.CodeEdit[] { const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args); const scriptInfo = this.projectService.getScriptInfo(file)!; const textRange = this.getRange(args, scriptInfo); - return languageService.toggleLineComment(file, textRange); + const textChanges = languageService.toggleLineComment(file, textRange); + + if (simplifiedResult) { + const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; + + return textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo)); + } + + return textChanges; } - private toggleMultilineComment(args: protocol.FileRangeRequestArgs): TextChange[] { + private toggleMultilineComment(args: protocol.FileRangeRequestArgs, simplifiedResult: boolean): TextChange[] | protocol.CodeEdit[] { const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args); const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; const textRange = this.getRange(args, scriptInfo); - return languageService.toggleMultilineComment(file, textRange); + const textChanges = languageService.toggleMultilineComment(file, textRange); + + if (simplifiedResult) { + const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; + + return textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo)); + } + + return textChanges; } - private commentSelection(args: protocol.FileRangeRequestArgs): TextChange[] { + private commentSelection(args: protocol.FileRangeRequestArgs, simplifiedResult: boolean): TextChange[] | protocol.CodeEdit[] { const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args); const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; const textRange = this.getRange(args, scriptInfo); - return languageService.commentSelection(file, textRange); + const textChanges = languageService.commentSelection(file, textRange); + + if (simplifiedResult) { + const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; + + return textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo)); + } + + return textChanges; } - private uncommentSelection(args: protocol.FileRangeRequestArgs): TextChange[] { + private uncommentSelection(args: protocol.FileRangeRequestArgs, simplifiedResult: boolean): TextChange[] | protocol.CodeEdit[] { const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args); const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; const textRange = this.getRange(args, scriptInfo); - return languageService.uncommentSelection(file, textRange); + const textChanges = languageService.uncommentSelection(file, textRange); + + if (simplifiedResult) { + const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!; + + return textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo)); + } + + return textChanges; } private mapSelectionRange(selectionRange: SelectionRange, scriptInfo: ScriptInfo): protocol.SelectionRange { @@ -2678,17 +2710,29 @@ namespace ts.server { [CommandNames.ProvideCallHierarchyOutgoingCalls]: (request: protocol.ProvideCallHierarchyOutgoingCallsRequest) => { return this.requiredResponse(this.provideCallHierarchyOutgoingCalls(request.arguments)); }, - [CommandNames.ToggleLineCommentFull]: (request: protocol.CommentSelectionRequest) => { - return this.requiredResponse(this.toggleLineComment(request.arguments)); + [CommandNames.ToggleLineComment]: (request: protocol.ToggleLineCommentRequest) => { + return this.requiredResponse(this.toggleLineComment(request.arguments, /*simplifiedResult*/ true)); }, - [CommandNames.ToggleMultilineCommentFull]: (request: protocol.CommentSelectionRequest) => { - return this.requiredResponse(this.toggleMultilineComment(request.arguments)); + [CommandNames.ToggleLineCommentFull]: (request: protocol.ToggleLineCommentRequest) => { + return this.requiredResponse(this.toggleLineComment(request.arguments, /*simplifiedResult*/ false)); + }, + [CommandNames.ToggleMultilineComment]: (request: protocol.ToggleMultilineCommentRequest) => { + return this.requiredResponse(this.toggleMultilineComment(request.arguments, /*simplifiedResult*/ true)); + }, + [CommandNames.ToggleMultilineCommentFull]: (request: protocol.ToggleMultilineCommentRequest) => { + return this.requiredResponse(this.toggleMultilineComment(request.arguments, /*simplifiedResult*/ false)); + }, + [CommandNames.CommentSelection]: (request: protocol.CommentSelectionRequest) => { + return this.requiredResponse(this.commentSelection(request.arguments, /*simplifiedResult*/ true)); }, [CommandNames.CommentSelectionFull]: (request: protocol.CommentSelectionRequest) => { - return this.requiredResponse(this.commentSelection(request.arguments)); + return this.requiredResponse(this.commentSelection(request.arguments, /*simplifiedResult*/ false)); }, - [CommandNames.UncommentSelectionFull]: (request: protocol.CommentSelectionRequest) => { - return this.requiredResponse(this.uncommentSelection(request.arguments)); + [CommandNames.UncommentSelection]: (request: protocol.UncommentSelectionRequest) => { + return this.requiredResponse(this.uncommentSelection(request.arguments, /*simplifiedResult*/ true)); + }, + [CommandNames.UncommentSelectionFull]: (request: protocol.UncommentSelectionRequest) => { + return this.requiredResponse(this.uncommentSelection(request.arguments, /*simplifiedResult*/ false)); }, }); diff --git a/src/testRunner/unittests/tsserver/session.ts b/src/testRunner/unittests/tsserver/session.ts index 8203f5187ae..5ca88f4adb9 100644 --- a/src/testRunner/unittests/tsserver/session.ts +++ b/src/testRunner/unittests/tsserver/session.ts @@ -272,10 +272,10 @@ namespace ts.server { CommandNames.PrepareCallHierarchy, CommandNames.ProvideCallHierarchyIncomingCalls, CommandNames.ProvideCallHierarchyOutgoingCalls, - CommandNames.ToggleLineCommentFull, - CommandNames.ToggleMultilineCommentFull, - CommandNames.CommentSelectionFull, - CommandNames.UncommentSelectionFull, + CommandNames.ToggleLineComment, + CommandNames.ToggleMultilineComment, + CommandNames.CommentSelection, + CommandNames.UncommentSelection, ]; it("should not throw when commands are executed with invalid arguments", () => { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index d8ee834cfa7..8971b73149e 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -6304,6 +6304,10 @@ declare namespace ts.server.protocol { GetEditsForFileRename = "getEditsForFileRename", ConfigurePlugin = "configurePlugin", SelectionRange = "selectionRange", + ToggleLineComment = "toggleLineComment", + ToggleMultilineComment = "toggleMultilineComment", + CommentSelection = "commentSelection", + UncommentSelection = "uncommentSelection", PrepareCallHierarchy = "prepareCallHierarchy", ProvideCallHierarchyIncomingCalls = "provideCallHierarchyIncomingCalls", ProvideCallHierarchyOutgoingCalls = "provideCallHierarchyOutgoingCalls" @@ -7328,7 +7332,20 @@ declare namespace ts.server.protocol { textSpan: TextSpan; parent?: SelectionRange; } + interface ToggleLineCommentRequest extends FileRequest { + command: CommandTypes.ToggleLineComment; + arguments: FileRangeRequestArgs; + } + interface ToggleMultilineCommentRequest extends FileRequest { + command: CommandTypes.ToggleMultilineComment; + arguments: FileRangeRequestArgs; + } interface CommentSelectionRequest extends FileRequest { + command: CommandTypes.CommentSelection; + arguments: FileRangeRequestArgs; + } + interface UncommentSelectionRequest extends FileRequest { + command: CommandTypes.UncommentSelection; arguments: FileRangeRequestArgs; } /**