Suppress brace completion of Quotes in Comments#16433
Conversation
amcasey
left a comment
There was a problem hiding this comment.
Looks correct - comments are just about consistency and simplification.
| 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); |
There was a problem hiding this comment.
Consider pulling out "followingToken" for consistency with "precedingToken".
There was a problem hiding this comment.
Might also want to make specification of /includeJsDocComment/ consistent (or explicitly inconsistent).
|
|
||
| export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined { | ||
| const precedingToken = findPrecedingToken(position, sourceFile); | ||
| const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end); |
There was a problem hiding this comment.
If this were set to empty, rather than undefined, in the absence of precedingToken, the checks below could be simplified.
There was a problem hiding this comment.
findPrecedingToken looks like it might be quite expensive. We should verify any impact on typing before checking this in - especially as some brace matching operations happen as a blocking operation on the UI thread.
| 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 || |
There was a problem hiding this comment.
range.pos <= position? It should be impossible, but it seems strange to ignore the first character.
| // 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; |
There was a problem hiding this comment.
Seems strange to accept and then reject single line comments. Another way to check would be to change range.kind === SyntaxKind.SingleLineCommentTrivia to (!onlyMultiLine && range.kind === SyntaxKind.SingleLineCommentTrivia).
| } | ||
|
|
||
| switch (openingBrace) { | ||
| case CharacterCodes.singleQuote: |
There was a problem hiding this comment.
Which brace completions do we still provide and why?
There was a problem hiding this comment.
Indeed. I've never liked any completions when typing inside a comment (including braces). Is there a rationale for what we do provide and what we don't?
There was a problem hiding this comment.
This behavior mirrors that of CSharp editing in VS. But we can just as easily suppress all completions in comments.
| case CharacterCodes.singleQuote: | ||
| case CharacterCodes.doubleQuote: | ||
| case CharacterCodes.backtick: | ||
| return !ts.formatting.getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ false); |
There was a problem hiding this comment.
The only caller passes literal false for the last argument and ignores the return value?
|
|
||
| goTo.marker('1'); | ||
| verify.isValidBraceCompletionAtPosition('('); | ||
| verify.not.isValidBraceCompletionAtPosition('"'); |
There was a problem hiding this comment.
Would it be worthwhile to test a position that isn't at EOL (e.g. a contraction, as in the bug report)?
| } | ||
| } | ||
|
|
||
| export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined { |
There was a problem hiding this comment.
why not use isInComment in services\utilities?
| } | ||
| } | ||
|
|
||
| export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined { |
There was a problem hiding this comment.
Why do we need this function? There's already an isInComment function exposed, and you don't use the range returned but just test if one was returned. Seems all we should care about is if the cursor is in a comment, right?
|
Re-ran Jenkins test and it succeeded. |
|
please port this to release-2.4 |
|
Double-checked the perf in VS via integrations tests which spammed brace opening characters, in and around comment blocks, in a 1000-line source file. For both completions tested, (parens and double quotes), performance remained virtually unchanged between the master branch with and without this PR applied. With the patch, results appeared marginally faster, but it was within a gut-check margin of error (difference of 1 additional keystroke out of 800 being above 30 ms). |
Fixes https://developercommunity.visualstudio.com/content/problem/16657/javascript-comment-typing-a-single-quotation-mark.html