Skip to content

Commit

Permalink
feat(cmp): added option auto_brackets that adds brackets to functio…
Browse files Browse the repository at this point in the history
…ns/methods in configured filetypes
  • Loading branch information
folke committed Mar 28, 2024
1 parent cfbd358 commit bf8ce80
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lua/lazyvim/plugins/coding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down Expand Up @@ -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("()<left>", false, false, true)
vim.api.nvim_feedkeys(keys, "i", true)
end
end)
end,
},

Expand Down

0 comments on commit bf8ce80

Please sign in to comment.