Skip to content

Commit 798db1e

Browse files
committed
Suppress brace completion of Quotes in Comments
1 parent 09321b3 commit 798db1e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/services/formatting/formatting.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,25 @@ namespace ts.formatting {
11171117
}
11181118
}
11191119

1120+
export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined {
1121+
const precedingToken = findPrecedingToken(position, sourceFile);
1122+
const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end);
1123+
const leadingCommentRangesOfNextToken = getLeadingCommentRangesOfNode(getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false), sourceFile);
1124+
const commentRanges = trailingRangesOfPreviousToken && leadingCommentRangesOfNextToken ?
1125+
trailingRangesOfPreviousToken.concat(leadingCommentRangesOfNextToken) :
1126+
trailingRangesOfPreviousToken || leadingCommentRangesOfNextToken;
1127+
if (commentRanges) {
1128+
for (const range of commentRanges) {
1129+
// We need to extend the range when in an unclosed multi-line comment.
1130+
if (range.pos < position && position < range.end ||
1131+
position === range.end && (range.kind === SyntaxKind.SingleLineCommentTrivia || position === sourceFile.getFullWidth())) {
1132+
return onlyMultiLine && range.kind !== SyntaxKind.MultiLineCommentTrivia ? undefined : range;
1133+
}
1134+
}
1135+
}
1136+
return undefined;
1137+
}
1138+
11201139
function getOpenTokenForList(node: Node, list: Node[]) {
11211140
switch (node.kind) {
11221141
case SyntaxKind.Constructor:

src/services/services.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,13 @@ namespace ts {
18221822
return false;
18231823
}
18241824

1825+
switch (openingBrace) {
1826+
case CharacterCodes.singleQuote:
1827+
case CharacterCodes.doubleQuote:
1828+
case CharacterCodes.backtick:
1829+
return !ts.formatting.getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ false);
1830+
}
1831+
18251832
return true;
18261833
}
18271834

tests/cases/fourslash/commentBraceCompletionPosition.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515

1616
goTo.marker('1');
1717
verify.isValidBraceCompletionAtPosition('(');
18+
verify.not.isValidBraceCompletionAtPosition('"');
19+
verify.not.isValidBraceCompletionAtPosition(`'`);
20+
verify.not.isValidBraceCompletionAtPosition('`');
1821

1922
goTo.marker('2');
2023
verify.isValidBraceCompletionAtPosition('(');
24+
verify.not.isValidBraceCompletionAtPosition('"');
2125

2226
goTo.marker('3');
23-
verify.isValidBraceCompletionAtPosition('(');
27+
verify.isValidBraceCompletionAtPosition('(');
28+
verify.not.isValidBraceCompletionAtPosition('"');

0 commit comments

Comments
 (0)