Skip to content

Suppress brace completion of Quotes in Comments#16433

Merged
aozgaa merged 2 commits into
masterfrom
suppressBraceCompletionOfQuotesinComments
Jun 13, 2017
Merged

Suppress brace completion of Quotes in Comments#16433
aozgaa merged 2 commits into
masterfrom
suppressBraceCompletionOfQuotesinComments

Conversation

@aozgaa
Copy link
Copy Markdown
Contributor

@aozgaa aozgaa commented Jun 10, 2017

Copy link
Copy Markdown
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct - comments are just about consistency and simplification.

Comment thread src/services/formatting/formatting.ts Outdated
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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider pulling out "followingToken" for consistency with "precedingToken".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also want to make specification of /includeJsDocComment/ consistent (or explicitly inconsistent).

Comment thread src/services/formatting/formatting.ts Outdated

export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined {
const precedingToken = findPrecedingToken(position, sourceFile);
const trailingRangesOfPreviousToken = precedingToken && getTrailingCommentRanges(sourceFile.text, precedingToken.end);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this were set to empty, rather than undefined, in the absence of precedingToken, the checks below could be simplified.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/services/formatting/formatting.ts Outdated
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 ||
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range.pos <= position? It should be impossible, but it seems strange to ignore the first character.

Comment thread src/services/formatting/formatting.ts Outdated
// 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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread src/services/services.ts
}

switch (openingBrace) {
case CharacterCodes.singleQuote:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which brace completions do we still provide and why?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

@aozgaa aozgaa Jun 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior mirrors that of CSharp editing in VS. But we can just as easily suppress all completions in comments.

Comment thread src/services/services.ts Outdated
case CharacterCodes.singleQuote:
case CharacterCodes.doubleQuote:
case CharacterCodes.backtick:
return !ts.formatting.getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ false);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only caller passes literal false for the last argument and ignores the return value?


goTo.marker('1');
verify.isValidBraceCompletionAtPosition('(');
verify.not.isValidBraceCompletionAtPosition('"');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worthwhile to test a position that isn't at EOL (e.g. a contraction, as in the bug report)?

Comment thread src/services/formatting/formatting.ts Outdated
}
}

export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use isInComment in services\utilities?

Comment thread src/services/formatting/formatting.ts Outdated
}
}

export function getRangeOfEnclosingComment(sourceFile: SourceFile, position: number, onlyMultiLine: boolean): CommentRange | undefined {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

@billti billti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks so easy now ;-)

@aozgaa
Copy link
Copy Markdown
Contributor Author

aozgaa commented Jun 13, 2017

Re-ran Jenkins test and it succeeded.

@aozgaa aozgaa merged commit 2a05bb1 into master Jun 13, 2017
@aozgaa aozgaa deleted the suppressBraceCompletionOfQuotesinComments branch June 13, 2017 18:08
@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Jun 13, 2017

please port this to release-2.4

@aozgaa
Copy link
Copy Markdown
Contributor Author

aozgaa commented Jun 14, 2017

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).

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants