Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectBehavior.insert merges completion with a following text #1903

Open
2 tasks done
vanaigr opened this issue Apr 27, 2024 · 0 comments
Open
2 tasks done

SelectBehavior.insert merges completion with a following text #1903

vanaigr opened this issue Apr 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@vanaigr
Copy link

vanaigr commented Apr 27, 2024

FAQ

  • 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:

nvim-cmp/lua/cmp/entry.lua

Lines 274 to 282 in 8f3c541

-- remove duplicated string.
if self:get_offset() ~= self.context.cursor.col then
for i = 1, #word do
if str.has_prefix(self.context.cursor_after_line, string.sub(word, i, #word)) then
word = string.sub(word, 1, i - 1)
break
end
end
end

And it was added in ef27b62.

@vanaigr vanaigr added the bug Something isn't working label Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant