Support macro
This commit is contained in:
@@ -15,6 +15,9 @@ local core = {}
|
|||||||
|
|
||||||
core.SOURCE_TIMEOUT = 500
|
core.SOURCE_TIMEOUT = 500
|
||||||
|
|
||||||
|
---Suspending state.
|
||||||
|
core.suspending = false
|
||||||
|
|
||||||
---@type cmp.Menu
|
---@type cmp.Menu
|
||||||
core.menu = menu.new({
|
core.menu = menu.new({
|
||||||
on_select = function(e)
|
on_select = function(e)
|
||||||
@@ -59,6 +62,14 @@ core.set_context = function(ctx)
|
|||||||
core.context = ctx
|
core.context = ctx
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Suspend completion
|
||||||
|
core.suspend = function()
|
||||||
|
core.suspending = true
|
||||||
|
return function()
|
||||||
|
core.suspending = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---Get sources that sorted by priority
|
---Get sources that sorted by priority
|
||||||
---@param statuses cmp.SourceStatus[]
|
---@param statuses cmp.SourceStatus[]
|
||||||
---@return cmp.Source[]
|
---@return cmp.Source[]
|
||||||
@@ -130,6 +141,10 @@ end
|
|||||||
|
|
||||||
---Check auto-completion
|
---Check auto-completion
|
||||||
core.on_change = function(event)
|
core.on_change = function(event)
|
||||||
|
if core.suspending then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
core.autoindent(event, function()
|
core.autoindent(event, function()
|
||||||
local ctx = core.get_context({ reason = types.cmp.ContextReason.Auto })
|
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())
|
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 = {}
|
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()))
|
table.insert(restore, string.sub(e.context.cursor_before_line, e:get_offset()))
|
||||||
keymap.feedkeys(table.concat(restore, ''), 'n', function()
|
keymap.feedkeys(table.concat(restore, ''), 'n', 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
|
||||||
@@ -298,6 +319,8 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
e:execute(function()
|
e:execute(function()
|
||||||
|
suspending()
|
||||||
|
|
||||||
if config.get().event.on_confirm_done then
|
if config.get().event.on_confirm_done then
|
||||||
config.get().event.on_confirm_done(e)
|
config.get().event.on_confirm_done(e)
|
||||||
end
|
end
|
||||||
@@ -307,6 +330,7 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
---Reset current completion state
|
---Reset current completion state
|
||||||
|
|||||||
@@ -50,43 +50,22 @@ keymap.to_keymap = function(s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Feedkeys with callback
|
---Feedkeys with callback
|
||||||
keymap.feedkeys = setmetatable({
|
keymap.feedkeys = function(keys, mode, callback)
|
||||||
callbacks = {},
|
|
||||||
}, {
|
|
||||||
__call = function(self, keys, mode, callback)
|
|
||||||
if #keys ~= 0 then
|
if #keys ~= 0 then
|
||||||
vim.fn.feedkeys(keys, mode)
|
vim.fn.feedkeys(keys, mode)
|
||||||
end
|
end
|
||||||
|
|
||||||
if callback then
|
if callback then
|
||||||
local current_mode = string.sub(vim.api.nvim_get_mode().mode, 1, 1)
|
local wait
|
||||||
local ctrl_r = current_mode == 'i'
|
wait = vim.schedule_wrap(function()
|
||||||
local id = misc.id('cmp.utils.keymap.feedkeys')
|
if vim.fn.getchar(1) == 0 then
|
||||||
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)
|
|
||||||
callback()
|
callback()
|
||||||
return ''
|
else
|
||||||
|
vim.defer_fn(wait, 1)
|
||||||
end
|
end
|
||||||
|
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>'
|
wait()
|
||||||
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
|
||||||
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)
|
|
||||||
|
|
||||||
---Register keypress handler.
|
---Register keypress handler.
|
||||||
keymap.listen = setmetatable({
|
keymap.listen = setmetatable({
|
||||||
|
|||||||
Reference in New Issue
Block a user