Open mikehaertl opened 2 years ago
I hit the same issue, and although I know basically nothing about cmp, I managed to cobble this together, also thanks to @mikehaertl's starting point:
['<CR>'] = cmp.mapping(function(fallback)
-- workaround for https://github.com/hrsh7th/cmp-nvim-lsp-signature-help/issues/13
if cmp.get_selected_entry().source.name == 'nvim_lsp_signature_help' then
fallback()
else
cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}(fallback)
end
end),
I'm pretty sure it's not "the right fix", but it seems to work well for me so far :)
@emmanueltouzery Awesome, thanks. (Note to self: I really should study the docs more, get_selected_entry()
is not that hard to find out).
I was confused about the cmp.mapping.confirm {...}(...)
part but just learned that it's syntactic sugar for cmp.mapping.confirm({...})(...)
. But that still makes me wonder what that final (fallback)
is for. Could you explain?
Also final (little) problem left: When you're on the next line the signature help is now gone and can't be brought back.
i really can't help that much :) it could be all wrong.
since we give cmp.mapping. confirm {}
to <CR>
and since we pass a function now instead (although wrapped in cmp.mapping(..)), I assumed that cmp.mapping.confirm{} is a function, so I tried to call it like that, and it worked.
Honestly I couldn't find dev docs to author cmp plugins. If you have a link, I'd be thankful. I discovered get_selected_entry()
using good old print(vim.expand(..))
debugging :)
There's something in the online help (:h cmp-develop
): https://github.com/hrsh7th/nvim-cmp/blob/main/doc/cmp.txt#L649, but it only explains the basics.
Regarding the extra (fallback)
call in cmp.mapping.confirm() (fallback)
: I'm not sure if that really makes sense. There's no return value documented for cmp.mapping.confirm()
either. I've left it away and your solution still works fine.
I still had some error messages of this form
E5108: Error executing lua [string ":lua"]:32: attempt to index a nil value
So we really need to check if the menu is visible and if an item is selcted at all. So my final version now looks like this:
['<CR>'] = function(fallback)
-- Don't block <CR> if signature help is active
-- https://github.com/hrsh7th/cmp-nvim-lsp-signature-help/issues/13
if not cmp.visible() or not cmp.get_selected_entry() or cmp.get_selected_entry().source.name == 'nvim_lsp_signature_help' then
fallback()
else
cmp.confirm({
-- Replace word if completing in the middle of a word
-- https://github.com/hrsh7th/nvim-cmp/issues/664
behavior = cmp.ConfirmBehavior.Replace,
-- Don't select first item on CR if nothing was selected
select = false,
})
end
end
Weird: I had cmp.mapping.confirm()
instead of cmp.confirm()
(according to the docs both are the same) but then CR did not work anymore to confirm a selection.
yes I just changed to:
- if cmp.get_selected_entry().source.name == 'nvim_lsp_signature_help' then
+ if cmp.get_selected_entry() ~= nil and cmp.get_selected_entry().source.name == 'nvim_lsp_signature_help' then
to fix the same issue. but your version is presumably better, i don't know.
You probably also want to check if the cmp menu is visible at all and if not use the normal CR behavior (i.e. fallback()
). That's why I added the not cmp.visible()
.
EDIT: Ah, get_selected_entry()
is probably nil
if the menu is not visible so you don't need that extra check.
Thanks for the fixes. I haven't tested @emmanueltouzery's solution, but @mikehaertl's works perfectly for me. Any chance of a PR for this?
Hi! I am facing the same problem and @mikehaertl solution works perfectly!
Problem
I have a custom mapping of
CR
for nvim-cmp:Unfortunately this blocks the
CR
key when the signature popup is visible. For example in this case I would like to insert a linebreak:If I press enter, nothing happens. I have to press
C-e
to abort the popup before I can insert a linebreak.Possible solution
My workaround in pseudo code would be something like this:
I just don't know how to implement
menu_contains_only_items_from_signature_source()
. Would this work at all?