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

Comments

@DanTup
Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented May 5, 2017

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

@alexandrudima

This comment has been minimized.

Copy link
Member

commented May 10, 2017

@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 ?

@alexandrudima

This comment has been minimized.

Copy link
Member

commented Apr 18, 2018

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

@jdneo

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

commented Sep 13, 2018

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

@TylerLeonhardt

This comment has been minimized.

Copy link
Member

commented May 18, 2019

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 ]]
@TylerLeonhardt

This comment has been minimized.

Copy link
Member

commented Aug 27, 2019

@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

This comment has been minimized.

Copy link
Member

commented Aug 28, 2019

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

demo

@TylerLeonhardt

This comment has been minimized.

Copy link
Member

commented Aug 28, 2019

The Python example listed above does not work still.

@jrieken

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

commented Aug 28, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.