Skip to content

Don't complete quotes if they complete a valid string#60227

Merged
davidwengier merged 2 commits intodotnet:mainfrom
davidwengier:DoubleQuoteCompletion
Mar 28, 2022
Merged

Don't complete quotes if they complete a valid string#60227
davidwengier merged 2 commits intodotnet:mainfrom
davidwengier:DoubleQuoteCompletion

Conversation

@davidwengier
Copy link
Copy Markdown
Member

Fixes #59178

I like this, but I don't normally have automatic brace completion turned on anyway, so I'm probably biased.

@davidwengier davidwengier requested a review from a team as a code owner March 17, 2022 11:05
@ghost ghost added the Area-IDE label Mar 17, 2022
// the second quote won't either.
if (token.IsKind(SyntaxKind.StringLiteralToken) &&
token.Span.Length > 1 &&
!token.ContainsDiagnostics)
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.

I'm wondering if token.Span.Length <= 1 would always mean token.ContainsDiagnostics

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.

a concern i have here is typing before something like $$ + " + bar"

We'll put in the " and see that " + " is legal, and will then not inject "" right? I'm not sure how this system works, but do we have access to the before/after documents to actually validate that we're in a situation where we should not inject multiple braces?

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.

hmm, you are right, in your case, we probably need to check if the closing quote was the opening quote from another string literal before typing

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm wondering if token.Span.Length <= 1 would always mean token.ContainsDiagnostics

From memory something simple like var s = $$test results in a string literal token for "test (ie, length > 1) but it has a diagnostics (unterminated string literal)

a concern i have here is typing before something like $$ + " + bar"

Good idea, will test (and yes I expect it would be broken)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

but do we have access to the before/after documents

Not easily that I can see. The platform calls us after the open quote has been inserted. Maybe we could check for any diagnostics on the line? Or check if there are a balanced number of quotes on the line or similar.

Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi Mar 18, 2022

Choose a reason for hiding this comment

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

I guess we can create a new tree from the text by removing the ", but not sure if we want to do that (potential perf impact maybe?

It would hopefully not be terrible, given that we can do incremental parsing. HOwever string-delimiter changes can be one of the worst cases here as it can dramatically change the meaning of the code (esp. with multi-line @" strings). So it's very possible this would be a problem. :(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

but do we have access to the before/after documents

Looks like, yes, when the session is started we can grab the before state. It's not clear to me how this would help though. Was there a particular algorithm you had in mind?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated this to not skip the completion if there are syntax errors on the line, and it seems to work okay. Had to deny it from verbatim string too, but that seems a reasonable compromise to me.

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.

Yes. If we have the before state, we can see what the shape of the tree was then. We can also get the diagnostics on the line before/after and have a heuristic that if there were several before, and much less afterwards (in the span of the string you create), then you should just inject a single quote.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's pretty much what I'm doing now, but I'm being way more conservative and not allowing any syntax errors after. My thinking is essentially that they have the feature turned on, so they mostly want two quotes, so I want to be pretty certain about it if we're only going to give them one.

@davidwengier
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 4 pipeline(s).

@davidwengier
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 4 pipeline(s).

@davidwengier
Copy link
Copy Markdown
Member Author

/azp run roslyn-integration-lsp-CI

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@davidwengier
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 4 pipeline(s).

@davidwengier davidwengier merged commit a838dcf into dotnet:main Mar 28, 2022
@davidwengier davidwengier deleted the DoubleQuoteCompletion branch March 28, 2022 22:05
@ghost ghost added this to the Next milestone Mar 28, 2022
@allisonchou allisonchou modified the milestones: Next, 17.2.P3 Mar 28, 2022
CyrusNajmabadi added a commit to CyrusNajmabadi/roslyn that referenced this pull request Apr 20, 2022
CyrusNajmabadi added a commit to CyrusNajmabadi/roslyn that referenced this pull request Apr 20, 2022
CyrusNajmabadi added a commit that referenced this pull request Apr 20, 2022
Revert "Don't complete quotes if they complete a valid string (#60227)"
CyrusNajmabadi added a commit that referenced this pull request Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate if adding a quote should not trigger quote-completion in the case that it would terminate an existing string.

4 participants