feat(cmp): added option auto_brackets
that adds brackets to functions/methods in configured filetypes
This commit is contained in:
@ -10,11 +10,22 @@ return {
|
|||||||
"hrsh7th/cmp-buffer",
|
"hrsh7th/cmp-buffer",
|
||||||
"hrsh7th/cmp-path",
|
"hrsh7th/cmp-path",
|
||||||
},
|
},
|
||||||
|
-- Not all LSP servers add brackets when completing a function.
|
||||||
|
-- To better deal with this, LazyVim adds a custom option to cmp,
|
||||||
|
-- that you can configure. For example:
|
||||||
|
--
|
||||||
|
-- ```lua
|
||||||
|
-- opts = {
|
||||||
|
-- auto_brackets = { "python" }
|
||||||
|
-- }
|
||||||
|
-- ```
|
||||||
|
|
||||||
opts = function()
|
opts = function()
|
||||||
vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true })
|
vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true })
|
||||||
local cmp = require("cmp")
|
local cmp = require("cmp")
|
||||||
local defaults = require("cmp.config.default")()
|
local defaults = require("cmp.config.default")()
|
||||||
return {
|
return {
|
||||||
|
auto_brackets = {}, -- configure any filetype to auto add brackets
|
||||||
completion = {
|
completion = {
|
||||||
completeopt = "menu,menuone,noinsert",
|
completeopt = "menu,menuone,noinsert",
|
||||||
},
|
},
|
||||||
@ -58,12 +69,25 @@ return {
|
|||||||
sorting = defaults.sorting,
|
sorting = defaults.sorting,
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
---@param opts cmp.ConfigSchema
|
---@param opts cmp.ConfigSchema | {auto_brackets?: string[]}
|
||||||
config = function(_, opts)
|
config = function(_, opts)
|
||||||
for _, source in ipairs(opts.sources) do
|
for _, source in ipairs(opts.sources) do
|
||||||
source.group_index = source.group_index or 1
|
source.group_index = source.group_index or 1
|
||||||
end
|
end
|
||||||
require("cmp").setup(opts)
|
local cmp = require("cmp")
|
||||||
|
local Kind = cmp.lsp.CompletionItemKind
|
||||||
|
cmp.setup(opts)
|
||||||
|
cmp.event:on("confirm_done", function(event)
|
||||||
|
if not vim.tbl_contains(opts.auto_brackets or {}, vim.bo.filetype) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local entry = event.entry
|
||||||
|
local item = entry:get_completion_item()
|
||||||
|
if vim.tbl_contains({ Kind.Function, Kind.Method }, item.kind) then
|
||||||
|
local keys = vim.api.nvim_replace_termcodes("()<left>", false, false, true)
|
||||||
|
vim.api.nvim_feedkeys(keys, "i", true)
|
||||||
|
end
|
||||||
|
end)
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user