Don't complete quotes if they complete a valid string#60227
Don't complete quotes if they complete a valid string#60227davidwengier merged 2 commits intodotnet:mainfrom
Conversation
| // the second quote won't either. | ||
| if (token.IsKind(SyntaxKind.StringLiteralToken) && | ||
| token.Span.Length > 1 && | ||
| !token.ContainsDiagnostics) |
There was a problem hiding this comment.
I'm wondering if token.Span.Length <= 1 would always mean token.ContainsDiagnostics
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :(
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
|
/azp run roslyn-integration-lsp-CI |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
Revert "Don't complete quotes if they complete a valid string (#60227)"
Fix infinite loop introduced with #60227
Fixes #59178
I like this, but I don't normally have automatic brace completion turned on anyway, so I'm probably biased.