Skip to content

Allow auto-inserting completion items #34

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

Closed
noomly opened this issue Oct 8, 2024 · 4 comments · Fixed by #65
Closed

Allow auto-inserting completion items #34

noomly opened this issue Oct 8, 2024 · 4 comments · Fixed by #65

Comments

@noomly
Copy link
Contributor

noomly commented Oct 8, 2024

Currently the only way to actually insert text from the selected completion item is to "accept" it. It requires first using keymaps to select the right item, and then use another keymap to accept it. I propose adding a new auto_insert boolean to the config, that if true will have this behavior:

  • When selecting an item via select_next and select_prev, its text will be automatically inserted, if the item is not a snippet
  • When reaching the end of the completion menu, the inserted text will be removed before cycling back to the top of the list

It would need to be coupled with #24 so that we can avoid inserting the first item every time the completion menu appears, and instead only insert it when deliberately starting the selection.

This allows the following workflow:

  • Write text
  • Completion menu shows up
  • Start cycling through the items
  • Once the right item was selected, no need to type another special keymap, we can simply resume our typing and the completion menu disappears once we type a "non-completable" character, like space or a ( for example.

Of course if we intend to use a snippet, use an argument-filling snippet-like helper (not sure if that's implemented yet?), or benefit from auto-importing, we can optionally use the accept keymap.

I've tried to implement this myself, however it is a little bit more complicated than I expected. I initially thought it'd be just a matter of calling:

text_edits_lib.apply_text_edits(item.client_id, { item.textEdit })

without the rest of the accept function, but it messes up with the auto commands set up by:
function trigger.activate_autocmds()

so we'd probably need to add some sort of state to identify whether the change was the result of a selection or an "accept".

@BrianNormant
Copy link
Contributor

BrianNormant commented Oct 8, 2024

If I understand correctly, The idea is that
when the completion menu is opened and a item is selected, the next key not bound to select_next or select_prev should accept the suggestion followed by its regular action.

Example, If I select require('blink' and press ) it should insert it followed by a parentheses. Such that the final text is: require('blink')

@Rentib
Copy link

Rentib commented Oct 8, 2024

If I understand correctly, The idea is that when the completion menu is opened and a item is selected, the next key not bound to select_next or select_prev should accept the suggestion followed by its regular action.

Example, If I select require('blink' and press ) it should insert it followed by a parentheses. Such that the final text is: require('blink')

I don't think so. I believie it is about replicating this behaviour from nvim-cmp

cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),

@noomly
Copy link
Contributor Author

noomly commented Oct 8, 2024

I believie it is about replicating this behaviour from nvim-cmp

Correct. I think it could be a bit better though.

In my original post I mentioned selecting an item (inserting the suggestion automatically) and closing the completion menu (by typing a ( for example) as not invoking the accept behavior, simply leaving the automatically inserted text. But now that I think about it, why not invoke the accept behavior automatically when the menu would be closed by typing a (, so that auto-import would work without requiring the extra accept keymap.

It would still be a special case for snippets though. Even if we insert the whole snippets on selection, which I'm not sure would be desirable at all, it probably wouldn't be desired to trigger the snippet behavior (cycling through the placeholders of the template) after inserting a non completable character.

I've been working on implementing this today and I think got something that almost works. I'll post a draft PR after #24 is merged.

@BrianNormant
Copy link
Contributor

BrianNormant commented Oct 9, 2024

Ok, I was thinking of a feature from coq_nvim.
I feels more natural to just continue typing when I already selected the suggestion with tab/S-tab rather than pressing then continue typing.

I've implemented it by binding every non special key in insert mode but it's kinda hacky, + It might impact performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants