fix(cmp): add wrap with vim.defer

This commit is contained in:
windwp 2021-09-14 06:52:23 +07:00
parent c8a42b03e8
commit 9d76310855

View File

@ -4,8 +4,12 @@ local cmp = require('cmp')
local M = {}
M.setup = function(opt)
opt = opt or { map_cr = true, map_complete = true, auto_select = true, map_char = {all = '('} }
if not opt.map_char then opt.map_char = {} end
opt = vim.tbl_deep_extend('force', {
map_cr = true,
map_complete = true,
auto_select = true,
map_char = { all = '(', tex = '' },
}, opt)
local map_cr = opt.map_cr
local map_complete = opt.map_complete
local map_char = opt.map_char
@ -29,31 +33,34 @@ M.setup = function(opt)
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()
vim.defer_fn(function()
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 filetype = vim.bo.filetype
local char = map_char[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, '', true)
local char = map_char[vim.bo.filetype] or map_char['all'] or '('
if char == '' then
return
end
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, 1)
end,
}
end