Improve keymap handling

This commit is contained in:
hrsh7th
2021-08-10 00:03:34 +09:00
parent b41afe2f83
commit 35d04be45e
2 changed files with 83 additions and 81 deletions

View File

@@ -200,91 +200,83 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
debug.log('entry.confirm', e) debug.log('entry.confirm', e)
local ctx = context.new() local ctx = context.new()
keymap.feedkeys( keymap.feedkeys('<C-g>U' .. string.rep('<BS>', ctx.cursor.col - e.context.cursor.col), 'n', function()
'<C-g>U' .. string.rep('<BS>', ctx.cursor.col - e.context.cursor.col), --@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
'n', if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
vim.schedule_wrap(function() local pre = context.new()
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334 e:resolve(function()
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then local new = context.new()
local pre = context.new() local text_edits = misc.safe(e:get_completion_item().additionalTextEdits) or {}
e:resolve(function() if #text_edits == 0 then
local new = context.new() return
local text_edits = misc.safe(e:get_completion_item().additionalTextEdits) or {} end
if #text_edits == 0 then
return
end
local has_cursor_line_text_edit = (function() local has_cursor_line_text_edit = (function()
local minrow = math.min(pre.cursor.row, new.cursor.row) local minrow = math.min(pre.cursor.row, new.cursor.row)
local maxrow = math.max(pre.cursor.row, new.cursor.row) local maxrow = math.max(pre.cursor.row, new.cursor.row)
for _, te in ipairs(text_edits) do for _, te in ipairs(text_edits) do
local srow = te.range.start.line + 1 local srow = te.range.start.line + 1
local erow = te.range['end'].line + 1 local erow = te.range['end'].line + 1
if srow <= minrow and maxrow <= erow then if srow <= minrow and maxrow <= erow then
return true return true
end
end end
return false
end)()
if has_cursor_line_text_edit then
return
end end
vim.fn['cmp#apply_text_edits'](new.bufnr, text_edits) return false
end) end)()
else if has_cursor_line_text_edit then
vim.fn['cmp#apply_text_edits'](ctx.bufnr, e:get_completion_item().additionalTextEdits) return
end end
vim.fn['cmp#apply_text_edits'](new.bufnr, text_edits)
end)
else
vim.fn['cmp#apply_text_edits'](ctx.bufnr, e:get_completion_item().additionalTextEdits)
end
-- Prepare completion item for confirmation -- Prepare completion item for confirmation
local completion_item = misc.copy(e:get_completion_item()) local completion_item = misc.copy(e:get_completion_item())
if not misc.safe(completion_item.textEdit) then if not misc.safe(completion_item.textEdit) then
completion_item.textEdit = {} completion_item.textEdit = {}
completion_item.textEdit.newText = misc.safe(completion_item.insertText) or completion_item.word or completion_item.label completion_item.textEdit.newText = misc.safe(completion_item.insertText) or completion_item.word or completion_item.label
end end
local behavior = option.behavior or config.get().confirmation.default_behavior local behavior = option.behavior or config.get().confirmation.default_behavior
if behavior == types.cmp.ConfirmBehavior.Replace then if behavior == types.cmp.ConfirmBehavior.Replace then
completion_item.textEdit.range = e:get_replace_range() completion_item.textEdit.range = e:get_replace_range()
else else
completion_item.textEdit.range = e:get_insert_range() completion_item.textEdit.range = e:get_insert_range()
end end
local is_snippet = true local is_snippet = true
is_snippet = is_snippet and completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet is_snippet = is_snippet and completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet
is_snippet = is_snippet and vim.lsp.util.parse_snippet(completion_item.textEdit.newText) ~= completion_item.textEdit.newText is_snippet = is_snippet and vim.lsp.util.parse_snippet(completion_item.textEdit.newText) ~= completion_item.textEdit.newText
local keys = '' local keys = ''
if completion_item.textEdit.range['end'].character > e.context.cursor.character then if completion_item.textEdit.range['end'].character > e.context.cursor.character then
keys = keys .. string.rep('<C-g>U<Right><BS>', completion_item.textEdit.range['end'].character - e.context.cursor.character) keys = keys .. string.rep('<C-g>U<Right><BS>', completion_item.textEdit.range['end'].character - e.context.cursor.character)
end end
if e.context.cursor.character > completion_item.textEdit.range.start.character then if e.context.cursor.character > completion_item.textEdit.range.start.character then
keys = keys .. string.rep('<BS>', e.context.cursor.character - completion_item.textEdit.range.start.character) keys = keys .. string.rep('<BS>', e.context.cursor.character - completion_item.textEdit.range.start.character)
end end
if is_snippet then
keys = keys .. '<C-g>u' .. e:get_word() .. '<C-g>u'
keys = keys .. string.rep('<BS>', vim.fn.strchars(e:get_word()))
else
keys = keys .. '<C-g>u' .. completion_item.textEdit.newText .. '<C-g>u'
end
keymap.feedkeys(keys, 'n', function()
if is_snippet then if is_snippet then
keys = keys .. '<C-g>u' .. e:get_word() .. '<C-g>u' config.get().snippet.expand({
keys = keys .. string.rep('<BS>', vim.fn.strchars(e:get_word())) body = completion_item.textEdit.newText,
else insert_text_mode = completion_item.insertTextMode,
keys = keys .. '<C-g>u' .. completion_item.textEdit.newText .. '<C-g>u' })
end end
keymap.feedkeys( e:execute(function()
keys, if callback then
'n', callback()
vim.schedule_wrap(function() end
if is_snippet then end)
config.get().snippet.expand({
body = completion_item.textEdit.newText,
insert_text_mode = completion_item.insertTextMode,
})
end
e:execute(function()
if callback then
callback()
end
end)
end)
)
end) end)
) end)
end) end)
---Reset current completion state ---Reset current completion state

View File

@@ -57,15 +57,23 @@ __call = function(self, keys, mode, callback)
if callback then if callback then
local current_mode = string.sub(vim.api.nvim_get_mode().mode, 1, 1) local current_mode = string.sub(vim.api.nvim_get_mode().mode, 1, 1)
local ctrl_r = current_mode == 'i'
local id = misc.id('cmp.utils.keymap.feedkeys') local id = misc.id('cmp.utils.keymap.feedkeys')
local cb = ('<Plug>(cmp-utils-keymap-feedkeys:%s)'):format(id) local cb = ('<Plug>(cmp-utils-keymap-feedkeys:%s)'):format(id)
self.callbacks[id] = function() self.callbacks[id] = function()
callback() self.callbacks[id] = nil
vim.api.nvim_buf_del_keymap(0, current_mode, cb) vim.api.nvim_buf_del_keymap(0, current_mode, cb)
callback()
if ctrl_r then
return ''
end
return keymap.t('<Ignore>') return keymap.t('<Ignore>')
end end
vim.api.nvim_buf_set_keymap(0, current_mode, cb, ('v:lua.cmp.utils.keymap.feedkeys.expr(%s)'):format(id), {
expr = true, local rhs = ctrl_r and '<C-r>=v:lua.cmp.utils.keymap.feedkeys.run(%s)<CR>' or ':<C-u>v:lua.cmp.utils.keymap.feedkeys.run(%s)<CR>'
vim.api.nvim_buf_set_keymap(0, current_mode, cb, string.format(rhs, id), {
expr = not ctrl_r,
noremap = true,
nowait = true, nowait = true,
silent = true, silent = true,
}) })
@@ -73,11 +81,11 @@ __call = function(self, keys, mode, callback)
end end
end end
}) })
misc.set(_G, { 'cmp', 'utils', 'keymap', 'feedkeys', 'expr' }, function(id) misc.set(_G, { 'cmp', 'utils', 'keymap', 'feedkeys', 'run' }, function(id)
if keymap.feedkeys.callbacks[id] then if keymap.feedkeys.callbacks[id] then
keymap.feedkeys.callbacks[id]() return keymap.feedkeys.callbacks[id]()
end end
return keymap.t('<Ignore>') return ''
end) end)
---Register keypress handler. ---Register keypress handler.
@@ -126,6 +134,7 @@ keymap.listen = setmetatable({
expr = true, expr = true,
nowait = true, nowait = true,
noremap = true, noremap = true,
silent = true,
}) })
end, end,
}) })
@@ -138,6 +147,7 @@ misc.set(_G, { 'cmp', 'utils', 'keymap', 'expr' }, function(keys)
vim.api.nvim_buf_set_keymap(0, 'i', '<Plug>(cmp-utils-keymap:_)', existing.rhs, { vim.api.nvim_buf_set_keymap(0, 'i', '<Plug>(cmp-utils-keymap:_)', existing.rhs, {
expr = existing.expr == 1, expr = existing.expr == 1,
noremap = existing.noremap == 1, noremap = existing.noremap == 1,
silent = true,
}) })
vim.fn.feedkeys(keymap.t('<Plug>(cmp-utils-keymap:_)'), 'i') vim.fn.feedkeys(keymap.t('<Plug>(cmp-utils-keymap:_)'), 'i')
end) end)