-
Notifications
You must be signed in to change notification settings - Fork 408
Advanced techniques
Will Hopkins edited this page Apr 1, 2023
·
15 revisions
This page contains advanced techniques.
nvim-cmp has a programmatic API. So you can manage the completion behavior by yourself.
See https://github.com/hrsh7th/nvim-cmp/issues/519#issuecomment-1091109258
See https://github.com/hrsh7th/nvim-cmp/pull/676#issuecomment-1002532096
cmp.setup({
enabled = function()
-- disable completion in comments
local context = require 'cmp.config.context'
-- keep command mode completion enabled when cursor is in a comment
if vim.api.nvim_get_mode().mode == 'c' then
return true
else
return not context.in_treesitter_capture("comment")
and not context.in_syntax_group("Comment")
end
end
})
cmp.setup.cmdline(':', {
sources = { --[[ your sources ]] },
enabled = function()
-- Set of commands where cmp will be disabled
local disabled = {
IncRename = true
}
-- Get first word of cmdline
local cmd = vim.fn.getcmdline():match("%S+")
-- Return true if cmd isn't disabled
-- else call/return cmp.close(), which returns false
return not disabled[cmd] or cmp.close()
end
})
-- If you want insert `(` after select function or method item
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
local cmp = require('cmp')
cmp.event:on(
'confirm_done',
cmp_autopairs.on_confirm_done()
)