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

Fix replace_keycodes has expr traceback #1264

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ubitux
Copy link

@ubitux ubitux commented Oct 25, 2022

Fixes the following traceback:
E5108: Error executing lua: ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:250: "replace_keycodes" requires "expr"
stack traceback:
[C]: in function 'nvim_set_keymap'
...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:250: in function 'set_map'
...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:170: in function 'solve'
...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:133: in function <...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:132>

Happens by simple pressing tab in insert mode on an empty file supported by a LSP.

Regression since 53bd574

Fixes the following traceback:
    E5108: Error executing lua: ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:250: "replace_keycodes" requires "expr"
    stack traceback:
            [C]: in function 'nvim_set_keymap'
            ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:250: in function 'set_map'
            ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:170: in function 'solve'
            ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:133: in function <...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:132>

Happens by simple pressing tab in insert mode on an empty file supported
by a LSP.

Regression since 53bd574
@hrsh7th
Copy link
Owner

hrsh7th commented Oct 29, 2022

Sorry. I think it's not a valid fix. Could you create minimal reproduction step for your problem?

@ubitux
Copy link
Author

ubitux commented Oct 29, 2022

Sorry. I think it's not a valid fix.

Yeah that's very likely I did something wrong

Could you create minimal reproduction step for your problem?

So I took https://raw.githubusercontent.com/hrsh7th/nvim-cmp/main/utils/vimrc.vim, added the following in the mapping (as recommended in nvim-cmp vimdoc)

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

Then according to https://github.com/hrsh7th/vim-vsnip#2-setting I wanted to have:

imap <expr> <Tab>   vsnip#jumpable(1)   ? '<Plug>(vsnip-jump-next)'      : '<Tab>'
smap <expr> <Tab>   vsnip#jumpable(1)   ? '<Plug>(vsnip-jump-next)'  

Which I translated into lua as such:

vim.keymap.set({'i','s'}, '<Tab>', [[vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>']], {expr=true, remap=true})

Now for some reason the vim version doesn't cause the issue, but the lua one does, and it seems related to remap=true. Since the vimscript wasn't using inoremap/snoremap I thought that was the correct thing to do.

Anyway, here is the minimal configuration:

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
vim.keymap.set({'i','s'}, '<Tab>', [[vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>']], {expr=true, remap=true})

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' }),
  },

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

lua << EOF
local capabilities = require('cmp_nvim_lsp').default_capabilities()

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

Repro:

  1. nvim -u /tmp/vimrc.vim /tmp/test.c
  2. go in insert mode
  3. press <Tab>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants