Skip to content

Commit a209db7

Browse files
committed
dont compute preceding token twice
1 parent f3e0cbb commit a209db7

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/services/formatting/formatting.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,9 +1155,11 @@ namespace ts.formatting {
11551155
/**
11561156
* Gets the indentation level of the multi-line comment enclosing position,
11571157
* and a negative value if the position is not in a multi-line comment.
1158+
*
1159+
* @param precedingToken Must be the result of `findPrecedingToken(position, sourceFile)`.
11581160
*/
1159-
export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number, options: EditorSettings): number {
1160-
const range = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true);
1161+
export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number, precedingToken: Node | undefined, options: EditorSettings): number {
1162+
const range = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword
11611163
if (range) {
11621164
const commentStart = range.pos;
11631165
const commentLineStart = getLineStartPositionForPosition(commentStart, sourceFile);
@@ -1167,10 +1169,14 @@ namespace ts.formatting {
11671169
return -1;
11681170
}
11691171

1172+
/**
1173+
* @param precedingToken pass `null` if preceding token was already computed and result was `undefined`.
1174+
*/
11701175
export function getRangeOfEnclosingComment(
11711176
sourceFile: SourceFile,
11721177
position: number,
11731178
onlyMultiLine: boolean,
1179+
precedingToken: Node | null | undefined = findPrecedingToken(position, sourceFile), // tslint:disable-line:no-null-keyword
11741180
tokenAtPosition = getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false),
11751181
predicate?: (c: CommentRange) => boolean): CommentRange | undefined {
11761182
// Considering a fixed position,
@@ -1181,7 +1187,6 @@ namespace ts.formatting {
11811187
// Note, `node.start` is the start-position of the first comment following the previous
11821188
// token that is not a trailing comment, so the leading and trailing comments of all
11831189
// tokens contain all comments in a sourcefile disjointly.
1184-
const precedingToken = findPrecedingToken(position, sourceFile);
11851190
const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end);
11861191
const leadingCommentRangesOfNextToken = getLeadingCommentRangesOfNode(tokenAtPosition, sourceFile);
11871192
const commentRanges = trailingRangesOfPreviousToken && leadingCommentRangesOfNextToken ?

src/services/formatting/smartIndenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ namespace ts.formatting {
3131
return 0;
3232
}
3333

34-
const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position, options);
34+
const precedingToken = findPrecedingToken(position, sourceFile);
35+
const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position, precedingToken, options);
3536
if (indentationOfEnclosingMultiLineComment >= 0) {
3637
return indentationOfEnclosingMultiLineComment;
3738
}
3839

39-
const precedingToken = findPrecedingToken(position, sourceFile);
4040
if (!precedingToken) {
4141
return getBaseIndentation(options);
4242
}

0 commit comments

Comments
 (0)