diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index 49dd19e..08949a9 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -74,9 +74,10 @@ end ---Suspend completion core.suspend = function(self) self.suspending = true - return function() + -- It's needed to avoid conflicting with autocmd debouncing. + return vim.schedule_wrap(function() self.suspending = false - end + end) end ---Get sources that sorted by priority diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index 93e95df..c27acca 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -287,7 +287,7 @@ cmp.setup = setmetatable({ -- In InsertEnter autocmd, vim will detects mode=normal unexpectedly. autocmd.subscribe( { 'InsertEnter', 'CmdlineEnter' }, - async.debounce_safe_state(function() + async.debounce_next_tick(function() if config.enabled() then cmp.config.compare.scopes:update() cmp.config.compare.locality:update() @@ -300,31 +300,31 @@ autocmd.subscribe( -- async.throttle is needed for performance. The mapping `:...` will fire `CmdlineChanged` for each character. autocmd.subscribe( { 'TextChangedI', 'TextChangedP', 'CmdlineChanged' }, - async.debounce_safe_state(function() + async.debounce_next_tick(function() if config.enabled() then cmp.core:on_change('TextChanged') end end) ) +autocmd.subscribe( + 'CursorMovedI', + async.debounce_next_tick(function() + if config.enabled() then + cmp.core:on_moved() + else + cmp.core:reset() + cmp.core.view:close() + end + end) +) + -- If make this asynchronous, the completion menu will not close when the command output is displayed. autocmd.subscribe({ 'InsertLeave', 'CmdlineLeave' }, function() cmp.core:reset() cmp.core.view:close() end) -autocmd.subscribe( - 'CursorMovedI', - 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 cmp.config.compare.recently_used:add_entry(evt.entry) diff --git a/lua/cmp/utils/async.lua b/lua/cmp/utils/async.lua index a536357..13f126b 100644 --- a/lua/cmp/utils/async.lua +++ b/lua/cmp/utils/async.lua @@ -1,5 +1,3 @@ -local feedkeys = require('cmp.utils.feedkeys') - local async = {} ---@class cmp.AsyncThrottle @@ -112,14 +110,14 @@ async.sync = function(runner, timeout) end ---Wait and callback for next safe state. -async.debounce_safe_state = function(callback) +async.debounce_next_tick = function(callback) local running = false return function() if running then return end running = true - feedkeys.call('', 'n', function() + vim.schedule(function() running = false callback() end)