diff --git a/lua/lazyvim/plugins/coding.lua b/lua/lazyvim/plugins/coding.lua index 4acd5b3b..b754eaf6 100644 --- a/lua/lazyvim/plugins/coding.lua +++ b/lua/lazyvim/plugins/coding.lua @@ -36,19 +36,8 @@ return { [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete(), [""] = cmp.mapping.abort(), - [""] = function(fallback) - if cmp.core.view:visible() or vim.fn.pumvisible() == 1 then - LazyVim.create_undo() - if cmp.confirm({ select = true }) then - return - end - end - return fallback() - end, - [""] = cmp.mapping.confirm({ - behavior = cmp.ConfirmBehavior.Replace, - select = true, - }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [""] = LazyVim.cmp.confirm(), + [""] = LazyVim.cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. [""] = function(fallback) cmp.abort() fallback() diff --git a/lua/lazyvim/util/cmp.lua b/lua/lazyvim/util/cmp.lua index 037f48e6..3343245a 100644 --- a/lua/lazyvim/util/cmp.lua +++ b/lua/lazyvim/util/cmp.lua @@ -36,4 +36,25 @@ function M.add_missing_snippet_docs(window) 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