cmp-cmdline icon indicating copy to clipboard operation
cmp-cmdline copied to clipboard

Error when calling `vim.fn.getcompletion()` for cmdline has unfinished regex patterns

Open Bekaboo opened this issue 1 year ago • 0 comments

Currently cmp-cmdline does not check if the current cmdline content is valid for completion before calling vim.fn.getcompletion(), which can leads to errors, especially when there are unfinished regex patterns in the cmdline.

Consider the following use case where users are using :grep or :vimg to search for the pattern \(foo\)\@<!bar:

:grep /\(f

after typing f the following error occurs:

Error executing vim.schedule lua callback: Vim:E54: Unmatched \(
stack traceback:
        [C]: in function 'getcompletion'
        ...e/pack/packages/opt/cmp-cmdline/lua/cmp_cmdline/init.lua:130: in function 'exec'
        ...e/pack/packages/opt/cmp-cmdline/lua/cmp_cmdline/init.lua:181: in function 'complete'
        .../nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/source.lua:330: in function 'complete'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/core.lua:302: in function 'complete'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/core.lua:170: in function 'autoindent'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/core.lua:162: in function 'on_change'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/init.lua:313: in function 'callback'
        .../site/pack/packages/opt/nvim-cmp/lua/cmp/utils/async.lua:165: in function <.../site/pack/packages/opt/nvim-cmp/lua/cmp/utils/async.lua:163>

this can be reproduced by calling vim.fn.getcompletion('grep /\\(f', 'cmdline') directly:

E5108: Error executing lua Vim:E54: Unmatched \(
stack traceback:
        [C]: in function 'getcompletion'
        [string ":lua"]:1: in main chunk

Similar errors for \@:

:grep /\(foo\)\@<

after typing <, the following error occurs:

Error executing vim.schedule lua callback: Vim:E869: (NFA) Unknown operator '\@
stack traceback:
        [C]: in function 'getcompletion'
        ...e/pack/packages/opt/cmp-cmdline/lua/cmp_cmdline/init.lua:130: in function 'exec'
        ...e/pack/packages/opt/cmp-cmdline/lua/cmp_cmdline/init.lua:181: in function 'complete'
        .../nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/source.lua:330: in function 'complete'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/core.lua:302: in function 'complete'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/core.lua:170: in function 'autoindent'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/core.lua:162: in function 'on_change'
        ...re/nvim/site/pack/packages/opt/nvim-cmp/lua/cmp/init.lua:313: in function 'callback'
        .../site/pack/packages/opt/nvim-cmp/lua/cmp/utils/async.lua:165: in function <.../site/pack/packages/opt/nvim-cmp/lua/cmp/utils/async.lua:163>

Since there's no convenient way to detect if a pattern can be completed without errors, I think the best we can do is to use pcall() to try to get the completion silently and continue only if no errors are triggered.

Bekaboo avatar Jun 15 '23 05:06 Bekaboo