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

Add support for `remap=true` mapping

Open powerman opened this issue 1 year ago • 0 comments

The reason for this feature request is known conflict between cabbrev and cnoremap <CR>.

Showcase:

$ nvim --clean
:cabbrev info version
:info
:cnoremap <CR> <CR>
:info

First :info works while second result in "E492: Not an editor command: info". Using :cmap instead of :cnoremap fixes this issue.

Because of the conflict this code breaks all cabbrev:

cmp.setup {
    mapping = {
        ['<CR>'] = cmp.mapping(function(fallback)
            -- ANYTHING --
        end, { 'c' }),
    }
}

Example of similar issue/fix in another project: https://github.com/inside/vim-search-pulse/issues/9.

At the moment I've worked around this issue in really ugly way: I replaced this trivial handler (which conflicts with cabbrev)

cmp.setup {
    mapping = {
        ['<CR>'] = cmp.mapping(function(fallback)
            if not cmp.confirm { select = false } then
                fallback()
            end
        end, { 'i', 'c' }),
    }
}

with these 3 handlers plus extra "fake" key <S-CR> (fake here means I do not actually press this key, it's used only to run handler function from <CR> mapping, see below):

cmp.setup {
    mapping = {
        ['<CR>'] = cmp.mapping(function(fallback)
            if not cmp.confirm { select = false } then
                fallback()
            end
        end, { 'i' }),
        ['<S-CR>'] = cmp.mapping(function(fallback)
            if not cmp.confirm { select = false } then
                fallback()
            end
        end, { 'c' }),
    }
}

vim.keymap.set('c', '<CR>', function()
    if cmp.visible() then
        return '<S-CR>'
    else
        return '<CR>'
    end
end, { remap = true, expr = true })

The "fake" <S-CR> key/handler is needed because this does not work (result in "E5108: Error executing lua: ....local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/window.lua:201: E565: Not allowed to change text or change window") for unknown (to me) reason:

vim.keymap.set('c', '<CR>', function()
    if not cmp.confirm { select = false } then
        return '<CR>'
    end
end, { remap = true, expr = true })

powerman avatar Oct 25 '24 19:10 powerman