hrsh7th / nvim-cmp

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

The wrong suggestion gets confirmed #1875

Open jonasbadstuebner opened 2 months ago

jonasbadstuebner commented 2 months ago

FAQ

Announcement

Minimal reproducible full config

" this is unmodified from the link
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 })
  },

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

lua << EOF
local capabilities = require('cmp_nvim_lsp').default_capabilities()

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

Description

Sometimes, when I press C-n, until I reach the suggestion I want to insert, then press C-y, it works fine. But if I press C-p until I reach that suggestion, then C-y, it inserts the next suggestion from the one I wanted. (I think it is the next one, but it is for sure a different one than the one I selected)

Steps to reproduce

  1. Create a file with the following content:
    
    TEST_1="a"
    TEST_2="b"
    TEST_3="b"

TEST_1


2. Put your cursor to the very end of the last line (`TEST_1|<here`) in insert mode
3. Press C-p once (or more times, but once is enough) -> will not show any result, since it cannot complete with anything
4. Press backspace once, effectively deleting the 1 on the last line
5. Press C-p to select any completion
6. Press C-y to confirm your selection

### Expected behavior

The selected completion gets applied.

### Actual behavior

A completion above or below the selected one gets applied.

### Additional context

_No response_
jonasbadstuebner commented 2 months ago

I observed that if I simply type TEST_ and press C-p, this below are my options: grafik (after it automatically previews the first option now, thats where the 1 is from, I didn't type this; just for clarification)

But if I follow my instructions from above, another option appears: TEST_ grafik

Maybe this has something to do with it? I'm unsure if this is really a problem with cmp, my understanding of LSPs is very limited, so please forgive me if I waste your time here.

Again, this does not happen with C-n. In step 3, it does not matter if you press C-n or C-p, but in step 5 it does.