fix(cmp): use better cmp.confirm

This commit is contained in:
Folke Lemaitre 2024-05-19 22:46:09 +02:00
parent 24a2a9fb0b
commit 2961162eba
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 23 additions and 13 deletions

View file

@ -36,19 +36,8 @@ return {
["<C-f>"] = cmp.mapping.scroll_docs(4), ["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(), ["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(), ["<C-e>"] = cmp.mapping.abort(),
["<CR>"] = function(fallback) ["<CR>"] = LazyVim.cmp.confirm(),
if cmp.core.view:visible() or vim.fn.pumvisible() == 1 then ["<S-CR>"] = LazyVim.cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
LazyVim.create_undo()
if cmp.confirm({ select = true }) then
return
end
end
return fallback()
end,
["<S-CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
["<C-CR>"] = function(fallback) ["<C-CR>"] = function(fallback)
cmp.abort() cmp.abort()
fallback() fallback()

View file

@ -36,4 +36,25 @@ function M.add_missing_snippet_docs(window)
end end
end end
-- This is a better implementation of `cmp.confirm`:
-- * check if the completion menu is visible without waiting for running sources
-- * create an undo point before confirming
-- This function is both faster and more reliable.
---@param opts? {select: boolean, behavior: cmp.ConfirmBehavior}
function M.confirm(opts)
local cmp = require("cmp")
opts = vim.tbl_extend("force", {
select = true,
behavior = cmp.ConfirmBehavior.Insert,
}, opts or {})
return function(fallback)
if cmp.core.view:visible() or vim.fn.pumvisible() == 1 then
LazyVim.create_undo()
if cmp.confirm(opts) then
return
end
end
return fallback()
end
end
return M return M