Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
Fix #825
  • Loading branch information
hrsh7th committed May 3, 2022
1 parent 6342643 commit 3e67778
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 78 deletions.
37 changes: 22 additions & 15 deletions lua/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,18 @@ cmp.setup = setmetatable({
end,
})

autocmd.subscribe({ 'InsertEnter', 'CmdlineEnter' }, function()
-- In InsertEnter autocmd, vim will detects mode=normal unexpectedly.
feedkeys.call('', 'i', function()
-- In InsertEnter autocmd, vim will detects mode=normal unexpectedly.
autocmd.subscribe(
{ 'InsertEnter', 'CmdlineEnter' },
async.debounce_safe_state(function()
if config.enabled() then
cmp.config.compare.scopes:update()
cmp.config.compare.locality:update()
cmp.core:prepare()
cmp.core:on_change('InsertEnter')
end
end)
end)
)

-- async.throttle is needed for performance. The mapping `:<C-u>...<CR>` will fire `CmdlineChanged` for each character.
autocmd.subscribe(
Expand All @@ -306,19 +307,25 @@ autocmd.subscribe(
end)
)

autocmd.subscribe({ 'InsertLeave', 'CmdlineLeave' }, function()
cmp.core:reset()
cmp.core.view:close()
end)

autocmd.subscribe('CursorMoved', function()
if config.enabled() then
cmp.core:on_moved()
else
autocmd.subscribe(
{ 'InsertLeave', 'CmdlineLeave' },
async.debounce_safe_state(function()
cmp.core:reset()
cmp.core.view:close()
end
end)
end)
)

autocmd.subscribe(
'CursorMoved',
async.debounce_safe_state(function()
if config.enabled() then
cmp.core:on_moved()
else
cmp.core:reset()
cmp.core.view:close()
end
end)
)

cmp.event:on('complete_done', function(evt)
if evt.entry then
Expand Down
69 changes: 6 additions & 63 deletions lua/cmp/utils/feedkeys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ feedkeys.call = setmetatable({
callbacks = {},
}, {
__call = function(self, keys, mode, callback)
if vim.fn.reg_recording() ~= '' then
return feedkeys.call_macro(keys, mode, callback)
end

local is_insert = string.match(mode, 'i') ~= nil
local is_immediate = string.match(mode, 'x') ~= nil

local queue = {}
if #keys > 0 then
table.insert(queue, { keymap.t('<Cmd>set lazyredraw<CR>'), 'n' })
table.insert(queue, { keymap.t('<Cmd>set textwidth=0<CR>'), 'n' })
table.insert(queue, { keymap.t('<Cmd>set eventignore=all<CR>'), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal lazyredraw<CR>'), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal textwidth=0<CR>'), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal backspace=2<CR>'), 'n' })
table.insert(queue, { keys, string.gsub(mode, '[itx]', ''), true })
table.insert(queue, { keymap.t('<Cmd>set %slazyredraw<CR>'):format(vim.o.lazyredraw and '' or 'no'), 'n' })
table.insert(queue, { keymap.t('<Cmd>set textwidth=%s<CR>'):format(vim.bo.textwidth or 0), 'n' })
table.insert(queue, { keymap.t('<Cmd>set eventignore=%s<CR>'):format(vim.o.eventignore or ''), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal %slazyredraw<CR>'):format(vim.o.lazyredraw and '' or 'no'), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal textwidth=%s<CR>'):format(vim.bo.textwidth or 0), 'n' })
table.insert(queue, { keymap.t('<Cmd>setlocal backspace=%s<CR>'):format(vim.go.backspace or 2), 'n' })
end

if callback then
Expand Down Expand Up @@ -54,57 +50,4 @@ misc.set(_G, { 'cmp', 'utils', 'feedkeys', 'call', 'run' }, function(id)
return ''
end)

feedkeys.call_macro = setmetatable({
queue = {},
current = nil,
timer = vim.loop.new_timer(),
running = false,
}, {
__call = function(self, keys, mode, callback)
local is_insert = string.match(mode, 'i') ~= nil
table.insert(self.queue, is_insert and 1 or #self.queue + 1, {
keys = keys,
mode = mode,
callback = callback,
})

if not self.running then
self.running = true
local consume
consume = vim.schedule_wrap(function()
if vim.fn.getchar(1) == 0 then
if self.current then
vim.cmd(('set backspace=%s'):format(self.current.backspace or ''))
vim.cmd(('set eventignore=%s'):format(self.current.eventignore or ''))
if self.current.callback then
self.current.callback()
end
self.current = nil
end

local current = table.remove(self.queue, 1)
if current then
self.current = {
keys = current.keys,
callback = current.callback,
backspace = vim.o.backspace,
eventignore = vim.o.eventignore,
}
vim.api.nvim_feedkeys(keymap.t('<Cmd>set backspace=start<CR>'), 'n', true)
vim.api.nvim_feedkeys(keymap.t('<Cmd>set eventignore=all<CR>'), 'n', true)
vim.api.nvim_feedkeys(current.keys, string.gsub(current.mode, '[i]', ''), true) -- 'i' flag is manually resolved.
end
end

if #self.queue ~= 0 or self.current then
vim.defer_fn(consume, 1)
else
self.running = false
end
end)
vim.defer_fn(consume, 1)
end
end,
})

return feedkeys
9 changes: 9 additions & 0 deletions lua/cmp/utils/feedkeys_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ describe('feedkeys', function()
})
end)

it('bacckspace', function()
vim.cmd([[setlocal backspace=0]])
feedkeys.call(keymap.t('iaiueo'), 'nx')
feedkeys.call(keymap.t('a<BS><BS>'), 'nx')
assert.are.same(vim.api.nvim_buf_get_lines(0, 0, -1, false), {
'aiu',
})
end)

it('testability', function()
feedkeys.call('i', 'n', function()
feedkeys.call('', 'n', function()
Expand Down

0 comments on commit 3e67778

Please sign in to comment.