hrsh7th / nvim-cmp

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

Garbage lines getting dumped into document when using <C-o>gj and <C-o>gk #1620

Open drazil100 opened 1 year ago

drazil100 commented 1 year ago

FAQ

Announcement

Minimal reproducible full config

inoremap <Down> <C-o>gj
inoremap <Up> <C-o>gk

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 }),
    ['<Up>'] = cmp.mapping.select_prev_item({behavior = 'select'}),
    ['<Down>'] = cmp.mapping.select_next_item({behavior = 'select'}),

  },

  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 using <C-o>gj with cmp.mapping.select_next_item() or <C-o>gk with cmp.mapping.select_prev_item() in insert mode it will generate a bunch of garbage lines at either the top or bottom of the file.

Before: Screenshot_2023-06-13-08-00-04

After: Screenshot_2023-06-13-08-01-14

Steps to reproduce

  1. Enter insert mode
  2. Either hold up at the top of the file or down at the bottom of the file

Expected behavior

Nothing happens.

Actual behavior

Garbage lines are being generated.

Additional context

It may be difficult to reproduce, especially with the minimal reproducible full config as it seems to be more likely to trigger the worse neovim is performing. I am on a cheepo $300 Celeron powered laptop and it was a bit of a struggle to get it to happen with the minimal config (only after I opened a stream in the background did it trigger easily). With my regular 1000 line neovim config though it happens at the slightest touch.

vurentjie commented 1 year ago

I noticed this bug today as well. It looks like some things happening on InsertEnter autocmd cause it, because <c-o> triggers InsertEnter. Mainly originating in this part here: https://github.com/hrsh7th/nvim-cmp/blob/2743dd989e9b932e1b4813a4927d7b84272a14e2/lua/cmp/init.lua#L299-L308

My current workaround is something like this, which is not ideal, but fixes the problem:

local ctrl_o_key = function(key)
  local ei = vim.opt.eventignore
  vim.opt.eventignore = 'InsertEnter'
  vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<c-o>' .. key, true, true, true))
  vim.schedule(function()
    vim.opt.eventignore = ei
  end)
end

vim.keymap.set('i', '<down>', function() ctrl_o_key('gj') end) 
vim.keymap.set('i', '<up>', function() ctrl_o_key('gk') end) 

Also just to add, I repro the same issue, 1) entering insert mode at the top of the file 2) hit <cr> about 15-20 times to add a few lines, 3) hold <up> key so that it triggers fast

drazil100 commented 1 month ago

Whoops! Seems I had already posted an issue on this earlier and completely forgot. Not sure which issue should be closed but here is the new issue. #2033