Skip to content

Commit 8fc3fd9

Browse files
committed
request returns span
1 parent b02963b commit 8fc3fd9

9 files changed

Lines changed: 42 additions & 29 deletions

File tree

src/harness/fourslash.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,13 +2500,17 @@ namespace FourSlash {
25002500
}
25012501
}
25022502

2503-
public verifyIsInMultiLineCommentAtPosition(negative: boolean) {
2503+
public verifySpanOfEnclosingComment(negative: boolean, onlyMultiLine: boolean) {
25042504
const expected = !negative;
25052505
const position = this.currentCaretPosition;
25062506
const fileName = this.activeFile.fileName;
2507-
const actual = this.languageService.isInMultiLineCommentAtPosition(fileName, position);
2507+
const actual = !!this.languageService.getSpanOfEnclosingComment(fileName, position, /*onlyMultiLine*/ onlyMultiLine);
25082508
if (expected !== actual) {
2509-
this.raiseError(`verifyIsInMultiLineCommentAtPosition failed: at position '${position}' in '${fileName}', expected '${expected}'.`);
2509+
this.raiseError(`verifySpanOfEnclosingComment failed:
2510+
position: '${position}'
2511+
fileName: '${fileName}'
2512+
onlyMultiLine: '${onlyMultiLine}'
2513+
expected: '${expected}'.`);
25102514
}
25112515
}
25122516

@@ -3583,8 +3587,8 @@ namespace FourSlashInterface {
35833587
this.state.verifyBraceCompletionAtPosition(this.negative, openingBrace);
35843588
}
35853589

3586-
public isInMultiLineCommentAtPosition() {
3587-
this.state.verifyIsInMultiLineCommentAtPosition(this.negative);
3590+
public isInCommentAtPosition(onlyMultiLine: boolean) {
3591+
this.state.verifySpanOfEnclosingComment(this.negative, onlyMultiLine);
35883592
}
35893593

35903594
public codeFixAvailable() {

src/harness/harnessLanguageService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ namespace Harness.LanguageService {
486486
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
487487
return unwrapJSONCallResult(this.shim.isValidBraceCompletionAtPosition(fileName, position, openingBrace));
488488
}
489-
isInMultiLineCommentAtPosition(fileName: string, position: number): boolean {
490-
return unwrapJSONCallResult(this.shim.getisInMultiLineCommentAtPosition(fileName, position));
489+
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): ts.TextSpan {
490+
return unwrapJSONCallResult(this.shim.getSpanOfEnclosingComment(fileName, position, onlyMultiLine));
491491
}
492492
getCodeFixesAtPosition(): ts.CodeAction[] {
493493
throw new Error("Not supported on the shim.");

src/server/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ namespace ts.server {
676676
return notImplemented();
677677
}
678678

679-
isInMultiLineCommentAtPosition(_fileName: string, _position: number): boolean {
679+
getSpanOfEnclosingComment(_fileName: string, _position: number, _onlyMultiLine: boolean): TextSpan {
680680
return notImplemented();
681681
}
682682

src/server/protocol.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ts.server.protocol {
88
/* @internal */
99
BraceFull = "brace-full",
1010
BraceCompletion = "braceCompletion",
11-
isInMultiLineComment = "isInMultiLineComment",
11+
GetSpanOfEnclosingComment = "getSpanOfEnclosingComment",
1212
Change = "change",
1313
Close = "close",
1414
Completions = "completions",
@@ -240,10 +240,18 @@ namespace ts.server.protocol {
240240
}
241241

242242
/**
243-
* A request to determine if the caret is inside a multi-line comment.
243+
* A request to determine if the caret is inside a comment.
244244
*/
245-
export interface IsInMultiLineCommentAtPositionRequest extends FileLocationRequest {
246-
command: CommandTypes.isInMultiLineComment;
245+
export interface SpanOfEnclosingCommentRequest extends FileLocationRequest {
246+
command: CommandTypes.GetSpanOfEnclosingComment;
247+
arguments: SpanOfEnclosingCommentRequestArgs;
248+
}
249+
250+
export interface SpanOfEnclosingCommentRequestArgs extends FileLocationRequestArgs {
251+
/**
252+
* Requires that the enclosing span be a multi-line comment, or else the request returns undefined.
253+
*/
254+
onlyMultiLine: boolean;
247255
}
248256

249257
/**

src/server/session.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,11 +961,12 @@ namespace ts.server {
961961
return project.getLanguageService(/*ensureSynchronized*/ false).getDocCommentTemplateAtPosition(file, position);
962962
}
963963

964-
private getisInMultiLineCommentAtPosition(args: protocol.FileLocationRequestArgs) {
964+
private getSpanOfEnclosingComment(args: protocol.SpanOfEnclosingCommentRequestArgs) {
965965
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
966966
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
967+
const onlyMultiLine = args.onlyMultiLine;
967968
const position = this.getPosition(args, scriptInfo);
968-
return project.getLanguageService(/*ensureSynchronized*/ false).isInMultiLineCommentAtPosition(file, position);
969+
return project.getLanguageService(/*ensureSynchronized*/ false).getSpanOfEnclosingComment(file, position, onlyMultiLine);
969970
}
970971

971972
private getIndentation(args: protocol.IndentationRequestArgs) {
@@ -1701,8 +1702,8 @@ namespace ts.server {
17011702
[CommandNames.DocCommentTemplate]: (request: protocol.DocCommentTemplateRequest) => {
17021703
return this.requiredResponse(this.getDocCommentTemplate(request.arguments));
17031704
},
1704-
[CommandNames.isInMultiLineComment]: (request: protocol.IsInMultiLineCommentAtPositionRequest) => {
1705-
return this.requiredResponse(this.getisInMultiLineCommentAtPosition(request.arguments));
1705+
[CommandNames.GetSpanOfEnclosingComment]: (request: protocol.SpanOfEnclosingCommentRequest) => {
1706+
return this.requiredResponse(this.getSpanOfEnclosingComment(request.arguments));
17061707
},
17071708
[CommandNames.Format]: (request: protocol.FormatRequest) => {
17081709
return this.requiredResponse(this.getFormattingEditsForRange(request.arguments));

src/services/formatting/formatting.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ namespace ts.formatting {
11221122
* and a negative value if the position is not in a multi-line comment.
11231123
*/
11241124
export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number, options: EditorSettings): number {
1125-
const range = getRangeOfEnclosingComment(sourceFile, position, SyntaxKind.MultiLineCommentTrivia);
1125+
const range = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true);
11261126
if (range) {
11271127
const commentStart = range.pos;
11281128
const commentLineStart = getLineStartPositionForPosition(commentStart, sourceFile);
@@ -1132,7 +1132,7 @@ namespace ts.formatting {
11321132
return undefined;
11331133
}
11341134

1135-
export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, kind: CommentKind): CommentRange | undefined {
1135+
export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined {
11361136
const precedingToken = findPrecedingToken(position, sourceFile);
11371137
const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end);
11381138
const leadingCommentRangesOfNextToken = getLeadingCommentRangesOfNode(getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false), sourceFile);
@@ -1143,7 +1143,7 @@ namespace ts.formatting {
11431143
for (const range of commentRanges) {
11441144
// We need to extend the range when in an unclosed multi-line comment.
11451145
if (range.pos < position && (position < range.end || position === range.end && position === sourceFile.getFullWidth())) {
1146-
return range.kind === kind ? range : undefined;
1146+
return onlyMultiLine && range.kind !== SyntaxKind.MultiLineCommentTrivia ? undefined : range;
11471147
}
11481148
}
11491149
}

src/services/services.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,9 +1818,10 @@ namespace ts {
18181818
return true;
18191819
}
18201820

1821-
function isInMultiLineCommentAtPosition(fileName: string, position: number): boolean {
1821+
function getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean) {
18221822
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
1823-
return !!ts.formatting.getRangeOfEnclosingComment(sourceFile, position, SyntaxKind.MultiLineCommentTrivia);
1823+
const range = ts.formatting.getRangeOfEnclosingComment(sourceFile, position, onlyMultiLine);
1824+
return range && createTextSpanFromRange(range);
18241825
}
18251826

18261827
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
@@ -2045,7 +2046,7 @@ namespace ts {
20452046
getFormattingEditsAfterKeystroke,
20462047
getDocCommentTemplateAtPosition,
20472048
isValidBraceCompletionAtPosition,
2048-
isInMultiLineCommentAtPosition,
2049+
getSpanOfEnclosingComment,
20492050
getCodeFixesAtPosition,
20502051
getEmitOutput,
20512052
getNonBoundSourceFile,

src/services/shims.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ namespace ts {
255255
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): string;
256256

257257
/**
258-
* Returns JSON-encoded boolean to indicate whether the caret at the current position is in a multi-line comment.
258+
* Returns a JSON-encoded TextSpan | undefined indicating the range of the enclosing comment, if it exists.
259259
*/
260-
getisInMultiLineCommentAtPosition(fileName: string, position: number): string;
260+
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): string;
261261

262262
getEmitOutput(fileName: string): string;
263263
getEmitOutputObject(fileName: string): EmitOutput;
@@ -840,11 +840,10 @@ namespace ts {
840840
);
841841
}
842842

843-
/// GET IS IN MULTI-LINE COMMENT
844-
public getisInMultiLineCommentAtPosition(fileName: string, position: number): string {
843+
public getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): string {
845844
return this.forwardJSONCall(
846-
`getisInMultiLineCommentAtPosition('${fileName}', ${position})`,
847-
() => this.languageService.isInMultiLineCommentAtPosition(fileName, position)
845+
`getSpanOfEnclosingComment('${fileName}', ${position})`,
846+
() => this.languageService.getSpanOfEnclosingComment(fileName, position, onlyMultiLine)
848847
);
849848
}
850849

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ namespace ts {
260260

261261
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
262262

263-
isInMultiLineCommentAtPosition(fileName: string, position: number): boolean;
263+
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
264264

265265
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
266266
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];

0 commit comments

Comments
 (0)