61 lines
2.0 KiB
Lua
61 lines
2.0 KiB
Lua
---@class lazyvim.util.cmp
|
|
local M = {}
|
|
|
|
---@param entry cmp.Entry
|
|
function M.auto_brackets(entry)
|
|
local cmp = require("cmp")
|
|
local Kind = cmp.lsp.CompletionItemKind
|
|
local item = entry:get_completion_item()
|
|
if vim.tbl_contains({ Kind.Function, Kind.Method }, item.kind) then
|
|
local cursor = vim.api.nvim_win_get_cursor(0)
|
|
local prev_char = vim.api.nvim_buf_get_text(0, cursor[1] - 1, cursor[2], cursor[1] - 1, cursor[2] + 1, {})[1]
|
|
if prev_char ~= "(" and prev_char ~= ")" then
|
|
local keys = vim.api.nvim_replace_termcodes("()<left>", false, false, true)
|
|
vim.api.nvim_feedkeys(keys, "i", true)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- This function adds missing documentation to snippets.
|
|
-- The documentation is a preview of the snippet.
|
|
---@param window cmp.CustomEntriesView|cmp.NativeEntriesView
|
|
function M.add_missing_snippet_docs(window)
|
|
local cmp = require("cmp")
|
|
local Kind = cmp.lsp.CompletionItemKind
|
|
local entries = window:get_entries()
|
|
for _, entry in ipairs(entries) do
|
|
if entry:get_kind() == Kind.Snippet then
|
|
local item = entry:get_completion_item()
|
|
if not item.documentation and item.insertText then
|
|
item.documentation = {
|
|
kind = cmp.lsp.MarkupKind.Markdown,
|
|
value = string.format("```%s\n%s\n```", vim.bo.filetype, item.insertText),
|
|
}
|
|
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
|