autopairs_cr() producing gibberish when called manually
simonomi opened this issue · comments
Description
In order to use nvim-autopairs with coc, I disabled map_cr
and added the following line to my .nvimrc
(very slightly modified from the one here)
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() :
\ "\<C-g>u\<C-r>=v:lua.require('nvim-autopairs').autopairs_cr()\<CR>"
This results in the command :verbose imap <cr>
returning:
i <CR> * coc#pum#visible() ? coc#pum#confirm() : "\<C-G>u\<C-R>=v:lua.require('nvim-autopairs').autopa
irs_cr()\<CR>"
However, now, whenever I press return, the following occurs:
struct Example {|}
*presses <CR>*
struct Example {
<t_ý>hnormal! ====
}
I tried looking through the code myself, and as far as I can tell, the bug originates within utils.esc
, which is simply a wrapper for vim.api.nvim_replace_termcodes
. For some reason, it seems to be replacing <CMD>
with <t_ý>h
, but only when called from within vim—it worked fine with map_cr
enabled!
Note that even with map_cr
enabled, manually executing <C-r>=v:lua.require('nvim-autopairs').autopairs_cr()
still caused this issue.
Because the bug seems to occur in vim.api.nvim_replace_termcodes
, I don't think this is a bug in nvim-autopairs, but I couldn't get the logging to work, so I don't feel like I have enough information to file a bug with nvim myself. I would appreciate if someone with more experience could look into this, and if necessary, file a bug report with more information where needed.
I've worked around this by replacing the string in rule:get_map_cr
with '<CR><Esc>==O'
, but this isn't an ideal solution.
Mapping bug
1.If you report a bug about indent. Please remember that plugin doesn't do anything about indent.
It just trigger the indent of your vim config so if you have wrong indent config then it will do wrong indent.
You can check by select a block of code and press ==
2. provide result of command :verbose imap <cr>
.
Steps to reproduce
No response
Minimal config
vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvim/site]]
local package_root = '/tmp/nvim/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'
local function load_plugins()
require('packer').startup {
{
'wbthomason/packer.nvim',
{
'windwp/nvim-autopairs',
},
-- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
},
config = {
package_root = package_root,
compile_path = install_path .. '/plugin/packer_compiled.lua',
display = { non_interactive = true },
},
}
end
_G.load_config = function()
require('nvim-autopairs').setup()
end
if vim.fn.isdirectory(install_path) == 0 then
print("Installing nvim-autopairs and dependencies.")
vim.fn.system { 'git', 'clone', '--depth=1', 'https://github.com/wbthomason/packer.nvim', install_path }
end
load_plugins()
require('packer').sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]]
help:v.lua-call
you can't use v:lua.require('nvim-autopairs').autopairs_cr() it should be v:lua.require'nvim-autopairs'.autopairs_cr()
https://github.com/windwp/nvim-autopairs/wiki/Completion-plugin
That doesn't change anything unfortunately. Omitting the parenthesis is just syntactic sugar for calling the function with one argument.
Oh! That's really interesting, I didn't realize that! I'll switch to using that version instead, but it still results in the bug occurring.
I tried looking through the code myself, and as far as I can tell, the bug originates within utils.esc, which is simply a wrapper for vim.api.nvim_replace_termcodes. For some reason, it seems to be replacing <CMD> with <t_ý>h
It's not a bug that's how neovim processes <CMD>, <C-r>= doesn't process escaped keys, use :h feedkeys
instead.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() :
\ "\<C-g>u\<C-r>=feedkeys(v:lua.require('nvim-autopairs').autopairs_cr(),"tin")\<CR>"
With a few minor modifications, it works!! I've updated the documentation accordingly.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() :
\ "\<C-g>u\<C-r>=execute(feedkeys(v:lua.require('nvim-autopairs').autopairs_cr(),"in"))\<CR>"
- added
execute
because the old version would insert an extraneous0
—the return value offeedkeys
- removed the
"t"
flag tofeedkeys
so that undo treats it as 'one step'