Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic insertion of parens doesn't interact nicely with completions, results in duplicated characters #26012

Closed
DanTup opened this issue May 5, 2017 · 11 comments · Fixed by microsoft/azuredatastudio#7206
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug suggest IntelliSense, Auto Complete verified Verification succeeded
Milestone

Comments

@DanTup
Copy link
Contributor

DanTup commented May 5, 2017

  • VSCode Version: 1.11.2
  • OS Version: Windows 10

Assume the following code (cursor is where the | is):

foo() {
}


main() {
  fo|
}

Completion is returning foo(). If the user continues to type o( then the o and ( are "subtracted" from the completion client-side by Code (it doesn't send another request to the completion provider). This works great; however since typing the ( auto-inserts a ) if the user then uses the completion, they will end up with:

main() {
  foo()) // <-- two closing parens
}

There's an animation showing a slightly different version or this at Dart-Code/Dart-Code#285

I can't see how the completion provider can fix this, since Code has taken over completion at this point (it's filtering and reducing the completion text client-side). I think the best thing would be for Code to subtract any auto-inserted characters from the end of the completion text in the same way it's removing the typed characters from the start.

Eg, when the user types the (:

  1. Filter the completion list by the (
  2. Remove the ( from the completion text to be inserted, and adjust the range accordingly
  3. Because a character was auto-inserted after the cursor, subtract that character from the end of the insertion text (and adjust the range) if it's the same character

I think 1 & 2 already happen, I think 3 would make sense to fix this.

@jrieken jrieken added bug Issue identified by VS Code Team member as probable bug editor-core Editor basic functionality suggest IntelliSense, Auto Complete labels May 5, 2017
@jrieken
Copy link
Member

jrieken commented May 5, 2017

@alexandrudima Is there a way I can halt electric brackets for a moment?

@alexdima
Copy link
Member

@jrieken I'm not 100% sure what you mean.

The code for handling typing is at typeWithInterceptors -- https://github.com/Microsoft/vscode/blob/master/src/vs/editor/common/controller/cursorTypeOperations.ts#L547

The cursor goes into that if the source of the type command is keyboard.

Would you want in the onWillType to cancel the editor's processing of type ?

@alexdima
Copy link
Member

ping @jrieken What do you want in terms of editor API here ?

@alexdima alexdima removed the editor-core Editor basic functionality label Apr 19, 2018
@jdneo
Copy link
Member

jdneo commented Aug 27, 2018

There is another issue about the same thing: redhat-developer/vscode-java#503.

Since VS Code has taken over completion at this point, there is no chance for language server to handle with such case. VS Code should correct the bracket according to the current content and the snippet given by the language server.

@jrieken
Copy link
Member

jrieken commented Sep 13, 2018

This is related to #10266 but the automatic brackets force you into this situation...

@TylerLeonhardt
Copy link
Member

Pulling a Python example from #70786. The root cause there was that snippets and auto closing pairs don't play nice together.

Snippet:

{
    "listcompPy": {
        "prefix":"[x for x in",
        "body":[
            "[x for x in range(1,20) if x%2==0 ]"
        ]
    }
}

typing [x results in a single extra bracket at the end:

[x for x in range(1,20) if x%2==0 ]]

@jrieken jrieken added this to the August 2019 milestone Aug 26, 2019
@jrieken jrieken removed the editor-autoclosing Editor automatic closing of parens / brackets / etc. label Aug 26, 2019
@TylerLeonhardt
Copy link
Member

@jrieken glad to see this fixed in that commit! What is your recommendation for how the above python snippet should look? As it does there or something different?

@jdneo
Copy link
Member

jdneo commented Aug 28, 2019

Thank you @jrieken for the fix. I tried in the Insider version it works like a charm!

demo

@TylerLeonhardt
Copy link
Member

The Python example listed above does not work still.

@jrieken
Copy link
Member

jrieken commented Aug 28, 2019

The Python example listed above does not work still.

It depends. Trigger IntelliSense, type [x (and get [x]), select snippet, all good. However, when you type [x (makes [x]), trigger suggest now (or auto trigger based on typing x, select snippet and the ] isn't nuked. That's #10266 which is about overriding text (to the right) which was there before (not while) suggestions show.

@TylerLeonhardt
Copy link
Member

Thanks @jrieken, I can place that example in #10266 glad we are making progress here!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug suggest IntelliSense, Auto Complete verified Verification succeeded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants