diff --git a/lua/lazyvim/plugins/coding.lua b/lua/lazyvim/plugins/coding.lua index 9a00a8c9..0823c12e 100644 --- a/lua/lazyvim/plugins/coding.lua +++ b/lua/lazyvim/plugins/coding.lua @@ -10,11 +10,22 @@ return { "hrsh7th/cmp-buffer", "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() vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true }) local cmp = require("cmp") local defaults = require("cmp.config.default")() return { + auto_brackets = {}, -- configure any filetype to auto add brackets completion = { completeopt = "menu,menuone,noinsert", }, @@ -58,12 +69,25 @@ return { sorting = defaults.sorting, } end, - ---@param opts cmp.ConfigSchema + ---@param opts cmp.ConfigSchema | {auto_brackets?: string[]} config = function(_, opts) for _, source in ipairs(opts.sources) do source.group_index = source.group_index or 1 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("()", false, false, true) + vim.api.nvim_feedkeys(keys, "i", true) + end + end) end, },