hrsh7th / nvim-cmp

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

Input of parentheses missing after selecting completion #1132

Open sQVe opened 2 years ago

sQVe commented 2 years 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 = {
    ['<CR>'] = cmp.mapping.confirm({ select = true }),

    ["<Tab>"] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      else
        fallback()
      end
    end, { "i", "s" }),

    ["<S-Tab>"] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_prev_item()
      else
        fallback()
      end
    end, { "i", "s" })
  },

  sources = cmp.config.sources({
    { name = "nvim_lsp" },
    { name = "buffer" },
  }),
}
EOF

lua << EOF
local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())

require'lspconfig'.tsserver.setup {
  capabilities = capabilities,
}
EOF

Description

When completing with cmp with the tsserver LSP it sometimes fails to register ( or ) strokes if you type them fast enough. This is fairly consistent for me and a real annoyance.

Steps to reproduce

I've only been able to reproduce this on the tsserver LSP. Completion via the buffer or sumneko_lua works fine.

Adding a screenrecording for clearity. The sound gives an indication of the speed of the keypresses. https://user-images.githubusercontent.com/2284724/184642619-e6989b57-8295-4a94-911f-18ddb333e66e.mp4

Expected behavior

All characters should be inputted.

Actual behavior

Inconsistent input of characters depending of speed of input.

Additional context

No response

hrsh7th commented 2 years ago

Your screencast is clear to understand to me but couldn't reproduce it.

https://user-images.githubusercontent.com/629908/184651314-6f2f9e17-a7c5-4266-81ad-5095813ccecc.mp4

sQVe commented 2 years ago

@hrsh7th Are you selecting the first completion item in your screencast? I need to have a completion item selected to be able to reproduce this.

gimerstedt commented 2 years ago

i think i had the same issue quite a while ago and i didn't want to confirm completions all the time so i added this, seems to be working ok:

    ['()'] = cmp.mapping(function()
      cmp.mapping.complete()
      vim.api.nvim_put({ '()' }, 'c', false, true)
    end, { 'i' }),
sQVe commented 2 years ago

i think i had the same issue quite a while ago and i didn't want to confirm completions all the time so i added this, seems to be working ok:

    ['()'] = cmp.mapping(function()
      cmp.mapping.complete()
      vim.api.nvim_put({ '()' }, 'c', false, true)
    end, { 'i' }),

Thank you for sharing. I'll try that first thing once I've won the battle over a certain stomach bug. Looks really promising though 🙏

sQVe commented 2 years ago

@gimerstedt This does indeed seem to fix the issue! 🙏🏼

I've now mapped the following function to ():

  local parentheses = function(fallback)
    if cmp.visible() then
      cmp.complete()
      vim.api.nvim_put({ '()' }, 'c', false, true)
    else
      fallback()
    end
  end
hrsh7th commented 2 years ago

I still can't reproduce this...

https://user-images.githubusercontent.com/629908/186608595-af803ff7-96b0-4770-94ab-40cf8d90cfa4.mov

dwoznicki commented 1 year ago

I'll just note that I'm experiencing the same issue, but can't seem to figure out where it's originating from. I've tried:

And now I'm not sure where to go from here. @hrsh7th If you have any advice on where to look, I'd be happy to try and continue debugging this issue.

In the meantime, @gimerstedt 's fix works for me too.

rodhash commented 8 months ago

vim.api.nvim_put({ '()' }, 'c', false, true)

This works but the cursor ends up after () which is a bit annoying, would be interesting to have the cursor inside brackets.