Nvim-cmp is adding multiple times the same sources

Viewed 159

I'm using nvim-cmpto have a contextual window to display my LSP suggestions and my snippets but when I open multiple buffers, I have an issue : the same source is added multiple times to nvim-cmp causing the same result to be repeated in the popup.

For example, here is the result of :CmpStatus: after a few minutes of work.

# ready source names
- vsnip
- buffer
- nvim_lsp:pylsp
- vsnip
- nvim_lsp:pylsp
- nvim_lsp:pylsp

Here is my nvim-cmpconfig :

cmp.setup({
    snippet = {
        expand = function(args)
            vim.fn["vsnip#anonymous"](args.body)
        end,
    },
    ...
    sources = {
        { name = 'vsnip' },
        { name = 'nvim_lua' },
        { name = 'nvim_lsp' },
        { name = 'buffer', keyword_length = 3 }
    },
}

Does anyone know how to adress this issue ? Is it a problem with my configuration ?

1 Answers

In your cmp configuration, you can use the dup keyword for vim_item with the formatting option / format function. See help for complete-item for explanations (:help complete-item).

cmp.setup({
  formatting = {
    format = function(entry, vim_item)
      vim_item.menu = ({
        nvim_lsp = '[LSP]',
        vsnip = '[Snippet]',
        nvim_lua = '[Nvim Lua]',
        buffer = '[Buffer]',
      })[entry.source.name]

      vim_item.dup = ({
        vsnip = 0,
        nvim_lsp = 0,
        nvim_lua = 0,
        buffer = 0,
      })[entry.source.name] or 0

      return vim_item
    end
  }
})

You can see details in this feature request for nvim-cmp plugin.

Related