From 798db1e884e3fae85a8bdaf87ade760912607b4f Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Fri, 9 Jun 2017 21:10:01 -0700 Subject: [PATCH] Suppress brace completion of Quotes in Comments --- src/services/formatting/formatting.ts | 19 +++++++++++++++++++ src/services/services.ts | 7 +++++++ .../commentBraceCompletionPosition.ts | 7 ++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 531b768f6d8..15ff50fc909 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -1117,6 +1117,25 @@ namespace ts.formatting { } } + export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined { + const precedingToken = findPrecedingToken(position, sourceFile); + const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end); + const leadingCommentRangesOfNextToken = getLeadingCommentRangesOfNode(getTokenAtPosition(sourceFile, position, /*includeJsDocComment*/ false), sourceFile); + const commentRanges = trailingRangesOfPreviousToken && leadingCommentRangesOfNextToken ? + trailingRangesOfPreviousToken.concat(leadingCommentRangesOfNextToken) : + trailingRangesOfPreviousToken || leadingCommentRangesOfNextToken; + if (commentRanges) { + for (const range of commentRanges) { + // We need to extend the range when in an unclosed multi-line comment. + if (range.pos < position && position < range.end || + position === range.end && (range.kind === SyntaxKind.SingleLineCommentTrivia || position === sourceFile.getFullWidth())) { + return onlyMultiLine && range.kind !== SyntaxKind.MultiLineCommentTrivia ? undefined : range; + } + } + } + return undefined; + } + function getOpenTokenForList(node: Node, list: Node[]) { switch (node.kind) { case SyntaxKind.Constructor: diff --git a/src/services/services.ts b/src/services/services.ts index 2f835d7de64..32c3a428f1b 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1822,6 +1822,13 @@ namespace ts { return false; } + switch (openingBrace) { + case CharacterCodes.singleQuote: + case CharacterCodes.doubleQuote: + case CharacterCodes.backtick: + return !ts.formatting.getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ false); + } + return true; } diff --git a/tests/cases/fourslash/commentBraceCompletionPosition.ts b/tests/cases/fourslash/commentBraceCompletionPosition.ts index 3730a3466c9..12a7629c383 100644 --- a/tests/cases/fourslash/commentBraceCompletionPosition.ts +++ b/tests/cases/fourslash/commentBraceCompletionPosition.ts @@ -15,9 +15,14 @@ goTo.marker('1'); verify.isValidBraceCompletionAtPosition('('); +verify.not.isValidBraceCompletionAtPosition('"'); +verify.not.isValidBraceCompletionAtPosition(`'`); +verify.not.isValidBraceCompletionAtPosition('`'); goTo.marker('2'); verify.isValidBraceCompletionAtPosition('('); +verify.not.isValidBraceCompletionAtPosition('"'); goTo.marker('3'); -verify.isValidBraceCompletionAtPosition('('); \ No newline at end of file +verify.isValidBraceCompletionAtPosition('('); +verify.not.isValidBraceCompletionAtPosition('"'); \ No newline at end of file