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

Suggestions sometimes are missing keywords

Open mikehaertl opened this issue 2 years ago • 3 comments

It often happens that a keyword is not suggested even though it definitely appears in some buffer. For example here, where the keyword is literally 2 lines above:

image

I don't have a procedure to reproduce this. If there's something I can try to debug when it happens, let me know.

Relevant part of my config:

    sources = cmp.config.sources({
      { name = 'vsnip' },
      { name = 'nvim_lsp' },
      {
          name = 'buffer',
          option = {
            -- complete from all buffers
            get_bufnrs = function()
              return vim.api.nvim_list_bufs()
            end,
            -- Use iskeyword instead of default regex to match words
            keyword_pattern = [[\k\+]],
          },
      },
      { name = 'nvim_lua' },
      { name = 'nvim_lsp_signature_help' },
      { name = 'path' },
    }),

mikehaertl avatar Apr 28 '23 16:04 mikehaertl

Same here. It seems to mostly work correctly for files opened from within Vim, but not for files passed on the command line, except for the first one, and sometimes (but not always) the second one :grinning:

My config is at https://github.com/toupeira/dotfiles/blob/main/vim/plugins/cmp.lua, I can also reproduce it with just the buffer source active.

toupeira avatar Jun 05 '24 13:06 toupeira

I found a partial workaround for my problem: Files that are passed on the command-line aren't loaded directly on startup (bufloaded() returns 0), only when they're switched to. But using vim.api.nvim_list_bufs() includes them anyway, so cmp-buffer will try to index them but can't get the buffer contents.

Filtering the list to only loaded buffers (and also listed buffers, since I don't care about keywords from hidden buffers) works around this, completions from unloaded buffers won't be available immediately, but once they're loaded cmp-buffer can now properly index them.

Here's my new configuration:

        option = {
          get_bufnrs = function()
            return vim.tbl_filter(
              function(buf)
                return vim.fn.buflisted(buf) == 1 and
                       vim.fn.bufloaded(buf) == 1
              end,
              vim.api.nvim_list_bufs()
            )
          end
        }

(I'd submit a PR for this, but there's already a few similar ones and @hrsh7th seems to be on vacation or something :wink:)

toupeira avatar Jun 13 '24 20:06 toupeira

ohhh this works for me very well, as I'd run into the same problem @toupeira

randywallace avatar Jul 27 '24 06:07 randywallace