hrsh7th / nvim-cmp

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

`SelectBehavior.insert` merges completion with a following text #1903

Open vanaigr opened 2 months ago

vanaigr commented 2 months ago

FAQ

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.