fix(cmp): remove mapping <CR> on autopairs use nvim-cmp setup

https://github.com/hrsh7th/nvim-cmp/pull/418
This commit is contained in:
zztrieuzz 2021-10-28 11:59:55 +07:00
parent dffcd00ef4
commit 599f7a15eb
3 changed files with 55 additions and 80 deletions

View File

@ -1,4 +1,5 @@
## nvim-autopairs
end
A super powerful autopair for Neovim.
It supports multiple characters.
@ -47,22 +48,17 @@ Before Input After
<details>
<summary><b>nvim-cmp</b></summary>
<h3>
You need to add mapping `CR` on nvim-cmp setup.
check readme.md on nvim-cmp repo.
</h3>
``` lua
-- you need setup cmp first put this after cmp.setup()
require("nvim-autopairs.completion.cmp").setup({
map_cr = true, -- map <CR> on insert mode
map_complete = true, -- it will auto insert `(` (map_char) after select function or method item
auto_select = true, -- automatically select the first item
insert = false, -- use insert confirm behavior instead of replace
map_char = { -- modifies the function or method delimiter by filetypes
all = '(',
tex = '{'
}
})
-- If you want insert `(` after select function or method item
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
local cmp = require('cmp')
cmp.event:on( 'confirm_done', cmp_autopairs.on_confirm_done())
```
Make sure to remove mapping insert mode `<CR>` binding if you have it.
</details>
<details>
<summary><b>coq_nvim</b></summary>
@ -71,7 +67,7 @@ Make sure to remove mapping insert mode `<CR>` binding if you have it.
local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')
npairs.setup({ map_bs = false })
npairs.setup({ map_bs = false, map_cr = false })
vim.g.coq_settings = { keymap = { recommended = false } }

View File

@ -14,7 +14,7 @@ M.state = {
local default = {
map_bs = true,
map_c_w = false,
map_cr = false,
map_cr = true,
disable_filetype = { 'TelescopePrompt', 'spectre_panel' },
disable_in_macro = false,
ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]], '%s+', ''),

View File

@ -3,71 +3,50 @@ local utils = require('nvim-autopairs.utils')
local cmp = require('cmp')
local M = {}
M.setup = function(opt)
opt = opt or {}
opt = vim.tbl_deep_extend('force', {
map_cr = true,
map_complete = true,
auto_select = true,
insert = false,
map_char = { all = '(', tex = '' },
}, opt)
local map_cr = opt.map_cr
local map_complete = opt.map_complete
local map_char = opt.map_char
local behavior = opt.insert
and cmp.ConfirmBehavior.Insert
or cmp.ConfirmBehavior.Replace
local cmp_setup = {}
if map_cr then
cmp_setup.mapping = {
['<CR>'] = cmp.mapping.confirm({
behavior = behavior,
select = opt.auto_select,
}),
}
vim.api.nvim_set_keymap(
'i',
'<CR>',
'v:lua.MPairs.autopairs_cr()',
{ expr = true, noremap = true }
)
end
if map_complete then
local method_kind = cmp.lsp.CompletionItemKind.Method
local function_kind = cmp.lsp.CompletionItemKind.Function
cmp_setup.event = {
on_confirm_done = function(entry)
local line = utils.text_get_current_line(0)
local _, col = utils.get_cursor()
local prev_char, next_char = utils.text_cusor_line(line, col, 1, 1, false)
local item = entry:get_completion_item()
local char = map_char[vim.bo.filetype] or map_char['all'] or '('
if char == '' then
return
end
if prev_char ~= char and next_char ~= char then
if item.kind == method_kind or item.kind == function_kind then
-- check insert text have ( from snippet
if
(
item.textEdit
and item.textEdit.newText
and item.textEdit.newText:match('[%(%[]')
)
or (item.insertText and item.insertText:match('[%(%[]'))
then
return
end
vim.api.nvim_feedkeys(char, 'i', true)
end
end
end,
}
end
cmp.setup(cmp_setup)
M.setup = function()
vim.notify(
'[nvim-autopairs] function nvim-autopairs.completion.cmp setup is deprecated.nvim-cmp update https://github.com/hrsh7th/nvim-cmp/pull/418'
)
vim.notify('[nvim-autopairs]you only need to add <cr> mapping on nvim-cmp.')
end
M.on_confirm_done = function(opt)
opt = opt or {}
opt = vim.tbl_deep_extend('force', {
map_char = { tex = '' },
kind = {
cmp.lsp.CompletionItemKind.Method,
cmp.lsp.CompletionItemKind.Function,
},
}, opt)
local map_char = opt.map_char
return function(entry)
local line = utils.text_get_current_line(0)
local _, col = utils.get_cursor()
local prev_char, next_char = utils.text_cusor_line(line, col, 1, 1, false)
local item = entry:get_completion_item()
local char = map_char[vim.bo.filetype] or '('
if char == '' then
return
end
if prev_char ~= char and next_char ~= char then
if utils.is_in_table(opt.kind, item.kind) then
-- check insert text have ( from snippet
if
(
item.textEdit
and item.textEdit.newText
and item.textEdit.newText:match('[%(%[]')
)
or (item.insertText and item.insertText:match('[%(%[]'))
then
return
end
vim.api.nvim_feedkeys(char, 'i', true)
end
end
end
end
return M