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

[REQUEST] Ability to preview insert completion without writing to buffer, like vim's completeopt=noinsert #5068

Open
Esgariot opened this issue Jan 2, 2024 · 4 comments

Comments

@Esgariot
Copy link

Esgariot commented Jan 2, 2024

Feature

Basically, I'd like vim's completeopt = noinsert (or helix's preview-completion-insert)

I see that <C-n> <C-p> are hardcoded and always write to buffer

kakoune/src/input_handler.cc

Lines 1292 to 1303 in 83fb65a

else if (key == ctrl('n'))
{
last_insert().keys.pop_back();
m_completer.select(1, true, last_insert().keys);
update_completions = false;
}
else if (key == ctrl('p'))
{
last_insert().keys.pop_back();
m_completer.select(-1, true, last_insert().keys);
update_completions = false;
}

Usecase

No response

@krobelus
Copy link
Contributor

krobelus commented Jan 3, 2024

It looks like the point of vim's completeopt = noinsert is to make the first <c-x><c-n> only show the menu but not insert the first candidate.
Kakoune provides autocompletion, so the menu is already shown without an explicit key, so I'm not sure if there is a scenario where this is needed?

If you turn off autocomplete, you can use <c-o> to show the menu.
If you want to use the same key for showing the menu and cycling through completions, that can be done by creating mappings in InsertCompletionShow and InsertCompletionHide hooks

@Esgariot
Copy link
Author

Esgariot commented Jan 3, 2024

  1. <c-x> (or let completion menu pop up by itself)
  2. scroll through available completions, some editors show additional "preview" with doc/signature of selected item in completion menu. Scrolling using <tab> / <s-tab> or <up> / <down>
  3. either exit using <esc> or select and apply/insert using <ret>

this way I don't have to remember to "commit undo" before selecting completion and I can decide when to insert selected completion, or abort. Currently I usually fumble it one way or the other and then need to bd and try again, just not used to it.

I have never really used vim that much, so not used to that model either.

I suppose that I could maybe hook InsertCompletionShow such that the word under cursor is somehow stored, then use the autocompletion menu as is, map <esc> to some kind of local undo that restores that word and unmaps itself (like in "completion using tab" example) but I'm not sure how to do it reliably, if even possible

@krobelus
Copy link
Contributor

krobelus commented Jan 3, 2024

I think it's possible we could improve the default UI here (change behavior of <esc> and <ret> to undo and accept).
But I'm not sure if the case for <esc> is strong because it's easy and obvious to use u afterwards.

These mappings try to match your expectations:

remove-hooks global my-completion
declare-option str on_esc_pressed
hook -group my-completion global InsertCompletionShow .* %{
    echo -debug InsertCompletionShow %val{hook_param}
    map window insert <esc> %{<a-semicolon>:set-option window on_esc_pressed <lt>a-semicolon>u; exec -with-hooks <lt>esc><ret>}
}
# Non-empty hook parameter means some completion was accepted.
hook -group my-completion global InsertCompletionHide .+ %{ execute-keys %opt{on_esc_pressed} }
hook -group my-completion global InsertCompletionHide .* %{ unset-option window on_esc_pressed }

with two problems

  1. it does not commit an undo point, so the u might undo too much. This can be worked around by first pressing <c-u>.
  2. it only does one u but a completion may create multiple undo points (for example LSP completions auto-insert imports when accepted)

Both are fixable with changes to the C++ core.

@Esgariot
Copy link
Author

Esgariot commented Jan 3, 2024

Thank you so much for these suggestions and support!

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

No branches or pull requests

2 participants