vim-matchup icon indicating copy to clipboard operation
vim-matchup copied to clipboard

Incompatibility with nvim-cmp snippet completion

Open mikehaertl opened this issue 2 years ago • 10 comments

Explain the issue

When this plugin is enabled I experience an issue with certain vsnippet based snippets in nvim-cmp:

If I move over one of the affected snippets in the nvim-cmp popup menu, the menu immediately closes (without confirmation!). It then leaves me with an incompletely expanded snippet in insert mode.

How to reproduce

Use the config below and start Neovim with nvim -u demo.vim.

  1. Enter insert mode
  2. Type x - it should bring up the completion menu image
  3. Move the selection down with <c-j>

The completion menu will close and leave you in this condition: image

Now comment out the vim-matchup plugin and try the same. This time the menu doesn't close and you can select any item: image

Additional information

So far I found out that the snippet must meet certain conditions:

  • It must include a textEdit
  • It probably has to include brackets like [, {, etc.

Reported here before: https://github.com/hrsh7th/cmp-vsnip/issues/9

Minimal vimrc file

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/vim-vsnip'
Plug 'andymass/vim-matchup'
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-j>'] = cmp.mapping(cmp.mapping.select_next_item(), {'i', 'c'}),
    ['<C-k>'] = cmp.mapping(cmp.mapping.select_prev_item(), {'i', 'c'}),
  },

  sources = cmp.config.sources({
    { name = "demo" },
  }),
}


local source = {}
function source:is_available()
  return true
end
function source:complete(params, callback)
  callback({
    { label = "x1" },
    {
      label = "x2",
      filterText = "x2",
      insertTextFormat = cmp.lsp.InsertTextFormat.Snippet,
      kind = cmp.lsp.CompletionItemKind.Snippet,
      textEdit = {
        newText = "[$1]",
        range = {
          start = {
            line = params.context.cursor.line,
            -- Hacky way to get correct range - but works for demo
            character = params.context.cursor.col - 2,
          },
          ['end'] = {
            line = params.context.cursor.line,
            character = params.context.cursor.col - 1,
          },
        },
      }
    },
    { label = "x3" },
  })
end
cmp.register_source('demo', source)
EOF

mikehaertl avatar Nov 12 '23 09:11 mikehaertl

Does cmp offer any mechanism to tell when it is "busy?" Like a function or a variable I can inspect

andymass avatar Nov 12 '23 12:11 andymass

Hmm, you can check if the menu is visible, if that helps:

require('cmp').visible()

See https://github.com/hrsh7th/nvim-cmp/blob/main/doc/cmp.txt#L155

mikehaertl avatar Nov 12 '23 15:11 mikehaertl

Unfortunately, I can't reproduce the problem with the config given.

One thing you can try is let g:matchup_matchparen_pumvisible = 0.

https://github.com/andymass/vim-matchup/blob/d30b72d20f01478a8486f15a57c89fe3177373db/doc/matchup.txt#L780

andymass avatar Nov 12 '23 16:11 andymass

It seems to only happen on the nightly build of neovim: Right now I'm on latest v0.10.0-dev-1535+g9ecb43b63 but also had the problem on an older build based on commit c4f4c7a35 from September.

I've tried with v0.9.3 and don't have the issue there, either.

So not sure if it's worth to investigate. On the other hand if the problem is not adressed it may still be in Neovim v0.10.0 when it's released.

mikehaertl avatar Nov 12 '23 18:11 mikehaertl

One thing you can try is let g:matchup_matchparen_pumvisible = 0

Thanks but it does not change the behavior.

If I find time I can bisect the commit that introduced the problem in Neovim.

mikehaertl avatar Nov 12 '23 18:11 mikehaertl

:+1: Been hitting this issue for a few months, just got around to tracking the problem to vim-matchup.

3rd avatar Nov 16 '23 00:11 3rd

cmp does not use vim's pum instead a custom window. I just pushed a commit that considers cmp.visible as recommended by @mikehaertl

andymass avatar Dec 19 '23 13:12 andymass

+1 happens to me as well, thanks for the plugins btw, hopefully this issue will be resolved soon

Altho let g:matchup_matchparen_pumvisible = 0 works when I don't set window configuration to my cmp (ie. use default pumwin)

wustho avatar Mar 07 '24 23:03 wustho