hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
8k stars 398 forks source link

Feature Request: Dedup items #511

Open tzachar opened 2 years ago

tzachar commented 2 years ago

When several sources return the same items, cmp will display duplicate entries. Is there a way of deduping the suggestions list?

Minimal config based on this


if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !filereadable(s:plug_dir .. '/autoload/plug.vim')
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
call plug#end()
PlugInstall | quit

" Setup global configuration. More on configuration below.
lua << EOF
local cmp = require "cmp"
local buffer = require "cmp_buffer"
cmp.register_source('extra', buffer.new())
cmp.setup {
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },

  mapping = {
    ['<CR>'] = cmp.mapping.confirm({ select = true })
  },

  sources = {
    { name = "buffer" },
    { name = "extra" },
    { name = "buffer" },
    { name = "extra" },
  },
}
EOF

To Reproduce

  1. nvim init.vim -u init.vim
  2. Start typing buf anywhere
  3. You get cmp-buffer suggested several times.

Expected behavior

cmp-buffer will be suggested only once.

hrsh7th commented 2 years ago

Why was the buffer source specified twice? (Is it just example?)

tzachar commented 2 years ago

Yes, to illustrate the issue

hrsh7th commented 2 years ago
{
  { name = 'buffer', NEW_KEY_NAME = true or false }
}

What the name do you think better?

tzachar commented 2 years ago

I probably did not wxplain myself... The problem is when two different sources have the same suggestion.

The use of cmp-buffer twice here is just to illustrate.

On Fri, 12 Nov 2021, 14:20 hrsh7th, @.***> wrote:

{ { name = 'buffer', NEW_KEY_NAME = true or false } }

What the name do you think better?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hrsh7th/nvim-cmp/issues/511#issuecomment-967070225, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFXXC4XHZGPULQXUYXOA7LULUBCRANCNFSM5H2NE7IQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

tzachar commented 2 years ago

Or so you propose to filter all items from the same KEY together?

On Fri, 12 Nov 2021, 14:34 Nir Tzachar, @.***> wrote:

I probably did not wxplain myself... The problem is when two different sources have the same suggestion.

The use of cmp-buffer twice here is just to illustrate.

On Fri, 12 Nov 2021, 14:20 hrsh7th, @.***> wrote:

{ { name = 'buffer', NEW_KEY_NAME = true or false } }

What the name do you think better?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hrsh7th/nvim-cmp/issues/511#issuecomment-967070225, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFXXC4XHZGPULQXUYXOA7LULUBCRANCNFSM5H2NE7IQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

tzachar commented 2 years ago

I think the issue is solved when I set dup = 0 in my source.

tzachar commented 2 years ago

Is there a better solution?

hrsh7th commented 2 years ago

I'm planning to remove CompletionItem.dup field and introduce the SourceConfig.allow_duplicates = false for source configuration.

But it requires the source default configuration API...

hrsh7th commented 2 years ago

Anyway, you can use dup field for the time being.

tzachar commented 2 years ago

Cool, I already am.

Seblerr commented 2 years ago

What's the status on this now? I'm getting the same entries from nvim_lsp and buffer using clangd. Setting dup in sources doesn't seem to do anything, and I can't find anything in the docs regarding "dup". image

omulmicsinegru commented 2 years ago

@Seblerr i've had the same difficulties with duplicates in my Vsnip items. This might help image

the vim_item.dup part is the one that interesets you.

I got it from this bug : https://github.com/hrsh7th/nvim-cmp/issues/32

askfiy commented 2 years ago

Keep an eye out for this issue...

barreiroleo commented 2 years ago

@Seblerr i've had the same difficulties with duplicates in my Vsnip items. This might help image

the vim_item.dup part is the one that interesets you.

I got it from this bug : #32

Hello there, any update on this situation? I had to use this config to deduplicate entries between LSP and Luasnip sources.

--- Edit: I say nothing. it was a null-ls issue with a source.

msva commented 2 years ago

btw, @hrsh7th and is it possible to somehow set up, for example, unloading "old" copies of duplicated sources?

For example, when I doing PackerSync, it reloads plugins, including sources, and after every sync (keeping same nvim session), I'm getting additional copy of each source, so it goes to look like like this: image

zenoli commented 2 years ago

@msva this! Just wanted to open an issue for this. I have the exact same isssue. I do PackerSync during my "Reload" function where I reload my config without quitting neovim. When I'm editing my config I do this very frequently obviously and then I get like 10+ duplicates of every entry which makes cmp quite unusable at that point. (If I'm not editing my vimconfig nvim-cmp is awesome of course :-))

Shougo commented 2 years ago

I think nvim-cmp can remove duplicated sources....

megalithic commented 1 year ago

also having this same issue .

ivan-volnov commented 1 year ago

I also have this issue with sorting between lsp and buffer.

1044

yifan0414 commented 8 months ago

How dedup from two different source image

        {
          name = "nvim_lsp",
          max_item_count = 7,
        },
        {
          name = "buffer",
          option = {
            get_bufnrs = function()
              local bufs = {}
              for _, win in ipairs(vim.api.nvim_list_wins()) do
                bufs[vim.api.nvim_win_get_buf(win)] = true
              end
              return vim.tbl_keys(bufs)
            end,
          },
        },

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