nvim-cmp icon indicating copy to clipboard operation
nvim-cmp copied to clipboard

In some cases, the default is not to select the first completion option

Open 52funny opened this issue 8 months ago • 5 comments

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.

image

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' },
    },
}

52funny avatar Nov 26 '23 06:11 52funny

What is your &completeopt ?

wookayin avatar Dec 02 '23 03:12 wookayin

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

bew avatar Dec 02 '23 22:12 bew

@wookayin like this.

vim.o.completeopt = 'menu,menuone'

52funny avatar Dec 03 '23 04:12 52funny

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.

52funny avatar Dec 03 '23 04:12 52funny

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

bew avatar Dec 03 '23 04:12 bew