nvim-cmp
nvim-cmp copied to clipboard
In some cases, the default is not to select the first completion option
I find some weird problems when I use it on a daily basis. For example, in the rust code below, the first completion option is not selected by default.
This is part of my config file.
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
cmp.setup {
view = {
entries = { name = "custom", selection_order = "top_down" }
},
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert {
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete {},
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
['<Tab>'] = function(fallback)
if not cmp.select_next_item() then
if vim.bo.buftype ~= 'prompt' and has_words_before() then
cmp.complete()
else
fallback()
end
end
end,
['<S-Tab>'] = function(fallback)
if not cmp.select_prev_item() then
if vim.bo.buftype ~= 'prompt' and has_words_before() then
cmp.complete()
else
fallback()
end
end
end,
},
sources = {
{ name = "copilot" },
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
},
}
What is your &completeopt
?
It might be related to LSP's pre-seelct preference, see cmp' faq: https://github.com/hrsh7th/nvim-cmp/blob/0b751f6beef40fd47375eaf53d3057e0bfa317e4/doc/cmp.txt#L886-L897
@wookayin like this.
vim.o.completeopt = 'menu,menuone'
It might be related to LSP's pre-seelct preference, see cmp' faq:
https://github.com/hrsh7th/nvim-cmp/blob/0b751f6beef40fd47375eaf53d3057e0bfa317e4/doc/cmp.txt#L886-L897
According to this setting method, the first Item is not selected by default.
According to this setting method, the first Item is not selected by default.
Yes, the default config will prioritize a item to pre-select if the LSP / a source gives one (not necessarily the first item). You can see the default config here: https://github.com/hrsh7th/nvim-cmp/blob/0b751f6beef40fd47375eaf53d3057e0bfa317e4/lua/cmp/config/default.lua#L27