Improve keymap handling
This commit is contained in:
@@ -200,10 +200,7 @@ 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),
|
|
||||||
'n',
|
|
||||||
vim.schedule_wrap(function()
|
|
||||||
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
||||||
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
|
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
|
||||||
local pre = context.new()
|
local pre = context.new()
|
||||||
@@ -266,10 +263,7 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
|||||||
else
|
else
|
||||||
keys = keys .. '<C-g>u' .. completion_item.textEdit.newText .. '<C-g>u'
|
keys = keys .. '<C-g>u' .. completion_item.textEdit.newText .. '<C-g>u'
|
||||||
end
|
end
|
||||||
keymap.feedkeys(
|
keymap.feedkeys(keys, 'n', function()
|
||||||
keys,
|
|
||||||
'n',
|
|
||||||
vim.schedule_wrap(function()
|
|
||||||
if is_snippet then
|
if is_snippet then
|
||||||
config.get().snippet.expand({
|
config.get().snippet.expand({
|
||||||
body = completion_item.textEdit.newText,
|
body = completion_item.textEdit.newText,
|
||||||
@@ -282,9 +276,7 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
)
|
|
||||||
end)
|
end)
|
||||||
)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
---Reset current completion state
|
---Reset current completion state
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user