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

`SelectBehavior.insert` merges completion with a following text

Open vanaigr opened this issue 1 year ago • 4 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 = {
    ['<Tab>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.insert }),
    ['<CR>'] = cmp.mapping.confirm({ select = true, behavior = cmp.ConfirmBehavior.insert })
  },

  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

When selecting an item with cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.insert }) and there is another word after the cursor position, some number of characters at the end of the selected item will not be inserted if the word after the cursor starts with these characters.

This is a problem for me, since I typically select a completion item without confirming it, and this 'merging' behavior creates a problem when e.g. adding a new argument to a call, and the completion item name ends in the same word as the next argument.

Steps to reproduce

nvim -u ~/cmp-repro.vim
iaaab<cr>
bbbb<cr>
bb<esc>0
ia<tab>

Expected behavior

The third line in the file should be 'aaab|bb' where | is the cursor position.

Actual behavior

The third line in the file is 'aaa|bb'.

Additional context

SelectBehavior.insert differs from ConfirmBehavior.insert in that when the completion is confirmed, the text after the cursor is not modified. So this would produce the expected behavior:

nvim -u ~/cmp-repro.vim
iaaab<cr>
bbbb<cr>
bb<esc>0
ia<cr>

The code responsible for removing the trailing characters: https://github.com/hrsh7th/nvim-cmp/blob/8f3c541407e691af6163e2447f3af1bd6e17f9a3/lua/cmp/entry.lua#L274-L282

And it was added in ef27b62.

vanaigr avatar Apr 27 '24 05:04 vanaigr

I've actually noticed the same behavior when confirming with: cmp.mapping.confirm({ select = false, behavior = cmp.ConfirmBehavior.insert })

Any solutions or workarounds for this?

svanharmelen avatar Oct 10 '24 14:10 svanharmelen

Any solutions or workarounds for this?

You can try manually removing this code from you installed nvim-cmp: https://github.com/hrsh7th/nvim-cmp/blob/8f3c541407e691af6163e2447f3af1bd6e17f9a3/lua/cmp/entry.lua#L274-L282

vanaigr avatar Oct 10 '24 16:10 vanaigr

Doesn't seem to fix my issue... Guess I'll need to make a self contained example to troubleshoot it.

svanharmelen avatar Oct 10 '24 19:10 svanharmelen

I think my issue was due to: https://github.com/hrsh7th/nvim-cmp/issues/706

svanharmelen avatar Dec 09 '24 08:12 svanharmelen