Skip to content

Commit c71a3e3

Browse files
author
Andy Hanson
committed
Simplify getRangeOfEnclosingComment
1 parent 22d33d2 commit c71a3e3

File tree

5 files changed

+34
-52
lines changed

5 files changed

+34
-52
lines changed

src/services/formatting/formatting.ts

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,48 +1146,35 @@ namespace ts.formatting {
11461146
export function getRangeOfEnclosingComment(
11471147
sourceFile: SourceFile,
11481148
position: number,
1149-
onlyMultiLine: boolean,
11501149
precedingToken?: Node | null, // tslint:disable-line:no-null-keyword
1151-
tokenAtPosition = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false),
1152-
predicate?: (c: CommentRange) => boolean): CommentRange | undefined {
1150+
tokenAtPosition = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false)): CommentRange | undefined {
11531151
const tokenStart = tokenAtPosition.getStart(sourceFile);
11541152
if (tokenStart <= position && position < tokenAtPosition.getEnd()) {
11551153
return undefined;
11561154
}
11571155

1158-
if (precedingToken === undefined) {
1159-
precedingToken = findPrecedingToken(position, sourceFile);
1160-
}
1156+
precedingToken = precedingToken === null ? undefined : precedingToken === undefined ? findPrecedingToken(position, sourceFile) : precedingToken; //neater
11611157

11621158
// Between two consecutive tokens, all comments are either trailing on the former
11631159
// or leading on the latter (and none are in both lists).
11641160
const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end);
11651161
const leadingCommentRangesOfNextToken = getLeadingCommentRangesOfNode(tokenAtPosition, sourceFile);
1166-
const commentRanges = trailingRangesOfPreviousToken && leadingCommentRangesOfNextToken ?
1167-
trailingRangesOfPreviousToken.concat(leadingCommentRangesOfNextToken) :
1168-
trailingRangesOfPreviousToken || leadingCommentRangesOfNextToken;
1169-
if (commentRanges) {
1170-
for (const range of commentRanges) {
1171-
// The end marker of a single-line comment does not include the newline character.
1172-
// With caret at `^`, in the following case, we are inside a comment (^ denotes the cursor position):
1173-
//
1174-
// // asdf ^\n
1175-
//
1176-
// But for closed multi-line comments, we don't want to be inside the comment in the following case:
1177-
//
1178-
// /* asdf */^
1179-
//
1180-
// However, unterminated multi-line comments *do* contain their end.
1181-
//
1182-
// Internally, we represent the end of the comment at the newline and closing '/', respectively.
1183-
//
1184-
if ((range.pos < position && position < range.end ||
1185-
position === range.end && (range.kind === SyntaxKind.SingleLineCommentTrivia || position === sourceFile.getFullWidth()))) {
1186-
return (range.kind === SyntaxKind.MultiLineCommentTrivia || !onlyMultiLine) && (!predicate || predicate(range)) ? range : undefined;
1187-
}
1188-
}
1189-
}
1190-
return undefined;
1162+
const commentRanges = concatenate(trailingRangesOfPreviousToken, leadingCommentRangesOfNextToken);
1163+
return commentRanges && find(commentRanges, range => rangeContainsPositionExclusive(range, position) ||
1164+
// The end marker of a single-line comment does not include the newline character.
1165+
// With caret at `^`, in the following case, we are inside a comment (^ denotes the cursor position):
1166+
//
1167+
// // asdf ^\n
1168+
//
1169+
// But for closed multi-line comments, we don't want to be inside the comment in the following case:
1170+
//
1171+
// /* asdf */^
1172+
//
1173+
// However, unterminated multi-line comments *do* contain their end.
1174+
//
1175+
// Internally, we represent the end of the comment at the newline and closing '/', respectively.
1176+
//
1177+
position === range.end && (range.kind === SyntaxKind.SingleLineCommentTrivia || position === sourceFile.getFullWidth()));
11911178
}
11921179

11931180
function getOpenTokenForList(node: Node, list: ReadonlyArray<Node>) {

src/services/formatting/smartIndenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace ts.formatting {
3434

3535
const precedingToken = findPrecedingToken(position, sourceFile);
3636

37-
const enclosingCommentRange = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword
38-
if (enclosingCommentRange) {
37+
const enclosingCommentRange = getRangeOfEnclosingComment(sourceFile, position, precedingToken || null); // tslint:disable-line:no-null-keyword
38+
if (enclosingCommentRange && enclosingCommentRange.kind === SyntaxKind.MultiLineCommentTrivia) {
3939
return getCommentIndent(sourceFile, position, options, enclosingCommentRange);
4040
}
4141

src/services/services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,8 +2033,8 @@ namespace ts {
20332033

20342034
function getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan | undefined {
20352035
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
2036-
const range = formatting.getRangeOfEnclosingComment(sourceFile, position, onlyMultiLine);
2037-
return range && createTextSpanFromRange(range);
2036+
const range = formatting.getRangeOfEnclosingComment(sourceFile, position);
2037+
return range && (!onlyMultiLine || range.kind === SyntaxKind.MultiLineCommentTrivia) ? createTextSpanFromRange(range) : undefined;
20382038
}
20392039

20402040
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {

src/services/utilities.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,8 @@ namespace ts {
10291029
* @param tokenAtPosition Must equal `getTokenAtPosition(sourceFile, position)
10301030
* @param predicate Additional predicate to test on the comment range.
10311031
*/
1032-
export function isInComment(
1033-
sourceFile: SourceFile,
1034-
position: number,
1035-
tokenAtPosition?: Node,
1036-
predicate?: (c: CommentRange) => boolean): boolean {
1037-
return !!formatting.getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ false, /*precedingToken*/ undefined, tokenAtPosition, predicate);
1032+
export function isInComment(sourceFile: SourceFile, position: number, tokenAtPosition?: Node): CommentRange | undefined {
1033+
return formatting.getRangeOfEnclosingComment(sourceFile, position, /*precedingToken*/ undefined, tokenAtPosition);
10381034
}
10391035

10401036
export function hasDocComment(sourceFile: SourceFile, position: number) {
@@ -1154,17 +1150,16 @@ namespace ts {
11541150
}
11551151

11561152
export function isInReferenceComment(sourceFile: SourceFile, position: number): boolean {
1157-
return isInComment(sourceFile, position, /*tokenAtPosition*/ undefined, c => {
1158-
const commentText = sourceFile.text.substring(c.pos, c.end);
1159-
return tripleSlashDirectivePrefixRegex.test(commentText);
1160-
});
1153+
return isInReferenceCommentWorker(sourceFile, position, /*shouldBeReference*/ true);
11611154
}
11621155

11631156
export function isInNonReferenceComment(sourceFile: SourceFile, position: number): boolean {
1164-
return isInComment(sourceFile, position, /*tokenAtPosition*/ undefined, c => {
1165-
const commentText = sourceFile.text.substring(c.pos, c.end);
1166-
return !tripleSlashDirectivePrefixRegex.test(commentText);
1167-
});
1157+
return isInReferenceCommentWorker(sourceFile, position, /*shouldBeReference*/ false);
1158+
}
1159+
1160+
function isInReferenceCommentWorker(sourceFile: SourceFile, position: number, shouldBeReference: boolean): boolean {
1161+
const range = isInComment(sourceFile, position, /*tokenAtPosition*/ undefined);
1162+
return !!range && shouldBeReference === tripleSlashDirectivePrefixRegex.test(sourceFile.text.substring(range.pos, range.end));
11681163
}
11691164

11701165
export function createTextSpanFromNode(node: Node, sourceFile?: SourceFile): TextSpan {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10742,7 +10742,7 @@ declare namespace ts {
1074210742
* @param tokenAtPosition Must equal `getTokenAtPosition(sourceFile, position)
1074310743
* @param predicate Additional predicate to test on the comment range.
1074410744
*/
10745-
function isInComment(sourceFile: SourceFile, position: number, tokenAtPosition?: Node, predicate?: (c: CommentRange) => boolean): boolean;
10745+
function isInComment(sourceFile: SourceFile, position: number, tokenAtPosition?: Node): CommentRange | undefined;
1074610746
function hasDocComment(sourceFile: SourceFile, position: number): boolean | undefined;
1074710747
function getNodeModifiers(node: Node): string;
1074810748
function getTypeArgumentOrTypeParameterList(node: Node): NodeArray<Node> | undefined;
@@ -11350,8 +11350,8 @@ declare namespace ts.formatting {
1135011350
/**
1135111351
* @param precedingToken pass `null` if preceding token was already computed and result was `undefined`.
1135211352
*/
11353-
function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean, precedingToken?: Node | null, // tslint:disable-line:no-null-keyword
11354-
tokenAtPosition?: Node, predicate?: (c: CommentRange) => boolean): CommentRange | undefined;
11353+
function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, precedingToken?: Node | null, // tslint:disable-line:no-null-keyword
11354+
tokenAtPosition?: Node): CommentRange | undefined;
1135511355
function getIndentationString(indentation: number, options: EditorSettings): string;
1135611356
}
1135711357
declare namespace ts.formatting {

0 commit comments

Comments
 (0)