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

Missing items in autocomplete for clangd

Open kakig opened this issue 2 years ago • 10 comments

FAQ

  • [X] I have checked the FAQ and it didn't resolve my problem.

Announcement

Minimal reproducible full config

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 .. '/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'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/vim-vsnip'
Plug 'neovim/nvim-lspconfig'
call plug#end()
PlugInstall | quit

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

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

  },

  sources = cmp.config.sources({
    { name = "nvim_lsp" },
    { name = "buffer" },
  }),
}
EOF

lua << EOF
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())

require'lspconfig'.clangd.setup {
  capabilities = capabilities,
}
EOF

Description

Not all items that should appear in autocompletion actually appear.

Steps to reproduce

I noticed this when using the GLEW library in C++, so I'm going to include steps for that, but the problem may happen on other circumstances or with other language servers as well.

  1. Paste this into a test cpp file:
#include <GL/glew.h>

int main() {
}
  1. Inside the main function, start typing glGetShader. You should get the following result: image
  2. Press <C-Space> to manually trigger completions. Now the results are correct: image

Expected behavior

Completion items should be updated while typing so that it becomes unnecessary to press <C-Space> to manually update.

Actual behavior

Completion items don't seem to be updated while typing, only filtered after completion is triggered for the first time. As a result, some items are missing in autocomplete.

Additional context

I tried tinkering with nvim-cmp's performance options, with no success, it didn't change anything in the behavior. I also tried changing the trigger settings, but couldn't figure out exactly what I was doing.

Likewise, I also tested the example in VSCode, and it behaves as expected. image

edit: formatting, add screenshots, context

kakig avatar Sep 10 '22 12:09 kakig

It is the problem of nvim-cmp? I think it is clangd's feature. I think to gather candidates is very slow. And it does not provide all candidates quickly.

Shougo avatar Sep 11 '22 01:09 Shougo

In my experience, clangd is very fast, especially when compared to python and typescript servers. I'm almost certain this issue also happens on those servers as well.

I found a workaround which is to put

trigger_characters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_'}

in the source config, so that it updates on every keystroke, but I'd rather have a better solution if possible

kakig avatar Sep 11 '22 10:09 kakig

OK. The result seems cached.

Shougo avatar Sep 11 '22 11:09 Shougo

Does increasing fetching_timeout help?

lvimuser avatar Sep 11 '22 14:09 lvimuser

The clangd returns full items when user requested manual completion.

hrsh7th avatar Sep 11 '22 14:09 hrsh7th

Oh. Sorry. I miss the VSCode example. I'll check it.

hrsh7th avatar Sep 11 '22 14:09 hrsh7th

Hm... @kakig Could you check your clangd's version? (both of VSCode's one and nvim's one)

hrsh7th avatar Sep 11 '22 15:09 hrsh7th

After some of the investiagation, I think the vscode-clangd and clangd does not have the client specific implementation.

So it may be a bug in nvim-cmp

hrsh7th avatar Sep 11 '22 15:09 hrsh7th

Hm... @kakig Could you check your clangd's version? (both of VSCode's one and nvim's one)

They are using the same clangd binary.

clangd --version
clangd version 14.0.6
Features: linux
Platform: x86_64-pc-linux-gnu

kakig avatar Sep 15 '22 14:09 kakig

Does increasing fetching_timeout help?

I tried it, didn't seem to have any effect.

kakig avatar Sep 15 '22 14:09 kakig

@kakig Could you provide the reproduction steps that does not needed to use GL lib?

hrsh7th avatar Oct 15 '22 06:10 hrsh7th

I'll disable the workaround and see if I can find any issues that are more easy to reproduce

kakig avatar Oct 15 '22 23:10 kakig

I've got some updates. It seems that the problem is really related to clangd, as it started crashing a lot while I was using it, and switching to ccls fixed my problems. You can close the issue if you think that this isn't a bug in nvim-cmp.

In case this information is useful, I was able to reproduce the issue with just the C++ standard library. It seems to happen more often when using backspace after the completion popup appears. Start with the following code:

#include <memory>

int main() {
  return 0;
}

and insert std::pointer. image note that the penultimate item in the completion is is_pointer. Now, if you backspace pointer, but leave std::, and start typing is_pointer, it should appear in the completion items, but instead, nothing appears. image

kakig avatar Oct 31 '22 15:10 kakig