Support macro
This commit is contained in:
@@ -15,6 +15,9 @@ local core = {}
|
||||
|
||||
core.SOURCE_TIMEOUT = 500
|
||||
|
||||
---Suspending state.
|
||||
core.suspending = false
|
||||
|
||||
---@type cmp.Menu
|
||||
core.menu = menu.new({
|
||||
on_select = function(e)
|
||||
@@ -59,6 +62,14 @@ core.set_context = function(ctx)
|
||||
core.context = ctx
|
||||
end
|
||||
|
||||
---Suspend completion
|
||||
core.suspend = function()
|
||||
core.suspending = true
|
||||
return function()
|
||||
core.suspending = false
|
||||
end
|
||||
end
|
||||
|
||||
---Get sources that sorted by priority
|
||||
---@param statuses cmp.SourceStatus[]
|
||||
---@return cmp.Source[]
|
||||
@@ -130,6 +141,10 @@ end
|
||||
|
||||
---Check auto-completion
|
||||
core.on_change = function(event)
|
||||
if core.suspending then
|
||||
return
|
||||
end
|
||||
|
||||
core.autoindent(event, function()
|
||||
local ctx = core.get_context({ reason = types.cmp.ContextReason.Auto })
|
||||
|
||||
@@ -225,9 +240,15 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
||||
|
||||
debug.log('entry.confirm', e:get_completion_item())
|
||||
|
||||
local ctx = context.new()
|
||||
local suspending = core.suspend()
|
||||
local ctx = core.get_context()
|
||||
|
||||
local confirm = {}
|
||||
table.insert(confirm, keymap.t(string.rep('<C-g>U<Left><Del>', ctx.cursor.character - misc.to_utfindex(e.context.cursor_before_line, e:get_offset()))))
|
||||
table.insert(confirm, e:get_word())
|
||||
keymap.feedkeys(table.concat(confirm, ''), 'nt', function()
|
||||
local restore = {}
|
||||
table.insert(restore, keymap.t(string.rep('<C-g>U<Left><Del>', ctx.cursor.character - misc.to_utfindex(e.context.cursor_before_line, e:get_offset()))))
|
||||
table.insert(restore, keymap.t(string.rep('<BS>', vim.fn.strchars(e:get_word()))))
|
||||
table.insert(restore, string.sub(e.context.cursor_before_line, e:get_offset()))
|
||||
keymap.feedkeys(table.concat(restore, ''), 'n', function()
|
||||
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
||||
@@ -298,6 +319,8 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
||||
})
|
||||
end
|
||||
e:execute(function()
|
||||
suspending()
|
||||
|
||||
if config.get().event.on_confirm_done then
|
||||
config.get().event.on_confirm_done(e)
|
||||
end
|
||||
@@ -308,6 +331,7 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
---Reset current completion state
|
||||
core.reset = function()
|
||||
|
||||
@@ -50,43 +50,22 @@ keymap.to_keymap = function(s)
|
||||
end
|
||||
|
||||
---Feedkeys with callback
|
||||
keymap.feedkeys = setmetatable({
|
||||
callbacks = {},
|
||||
}, {
|
||||
__call = function(self, keys, mode, callback)
|
||||
keymap.feedkeys = function(keys, mode, callback)
|
||||
if #keys ~= 0 then
|
||||
vim.fn.feedkeys(keys, mode)
|
||||
end
|
||||
|
||||
if callback then
|
||||
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 cb = ('<Plug>(cmp-utils-keymap-feedkeys:%s)'):format(id)
|
||||
self.callbacks[id] = function()
|
||||
self.callbacks[id] = nil
|
||||
vim.api.nvim_buf_del_keymap(0, current_mode, cb)
|
||||
local wait
|
||||
wait = vim.schedule_wrap(function()
|
||||
if vim.fn.getchar(1) == 0 then
|
||||
callback()
|
||||
return ''
|
||||
else
|
||||
vim.defer_fn(wait, 1)
|
||||
end
|
||||
|
||||
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,
|
||||
silent = true,
|
||||
})
|
||||
vim.fn.feedkeys(keymap.t(cb), '')
|
||||
end
|
||||
end
|
||||
})
|
||||
misc.set(_G, { 'cmp', 'utils', 'keymap', 'feedkeys', 'run' }, function(id)
|
||||
if keymap.feedkeys.callbacks[id] then
|
||||
return keymap.feedkeys.callbacks[id]()
|
||||
end
|
||||
return ''
|
||||
end)
|
||||
wait()
|
||||
end
|
||||
end
|
||||
|
||||
---Register keypress handler.
|
||||
keymap.listen = setmetatable({
|
||||
|
||||
Reference in New Issue
Block a user