This commit is contained in:
hrsh7th
2022-01-24 23:46:07 +09:00
parent 7cb14b0c0f
commit 27123b1297
4 changed files with 59 additions and 35 deletions

View File

@@ -261,15 +261,27 @@ end
---Update completion menu ---Update completion menu
core.filter = async.throttle( core.filter = async.throttle(
vim.schedule_wrap(function(self) setmetatable({
ctx = nil,
}, {
__call = function(this, self)
self.filter.timeout = self.view:visible() and THROTTLE_TIME or 0 self.filter.timeout = self.view:visible() and THROTTLE_TIME or 0
-- Check context changed.
local ctx = self:get_context()
if this.ctx and not this.ctx:changed(ctx) then
return
end
this.ctx = ctx
-- Check invalid condition.
local ignore = false local ignore = false
ignore = ignore or not api.is_suitable_mode() ignore = ignore or not api.is_suitable_mode()
if ignore then if ignore then
return return
end end
-- Check fetching sources.
local sources = {} local sources = {}
for _, s in ipairs(self:get_sources({ source.SourceStatus.FETCHING, source.SourceStatus.COMPLETED })) do for _, s in ipairs(self:get_sources({ source.SourceStatus.FETCHING, source.SourceStatus.COMPLETED })) do
if not s.incomplete and SOURCE_TIMEOUT > s:get_fetching_time() then if not s.incomplete and SOURCE_TIMEOUT > s:get_fetching_time() then
@@ -281,8 +293,6 @@ core.filter = async.throttle(
table.insert(sources, s) table.insert(sources, s)
end end
local ctx = self:get_context()
-- Display completion results. -- Display completion results.
self.view:open(ctx, sources) self.view:open(ctx, sources)
@@ -297,7 +307,8 @@ core.filter = async.throttle(
end) == 0 then end) == 0 then
config.set_onetime({}) config.set_onetime({})
end end
end), end
}),
THROTTLE_TIME THROTTLE_TIME
) )

View File

@@ -67,11 +67,13 @@ end
---Get current selected entry or nil ---Get current selected entry or nil
cmp.get_selected_entry = function() cmp.get_selected_entry = function()
cmp.core.filter.raw(cmp.core)
return cmp.core.view:get_selected_entry() return cmp.core.view:get_selected_entry()
end end
---Get current active entry or nil ---Get current active entry or nil
cmp.get_active_entry = function() cmp.get_active_entry = function()
cmp.core.filter.raw(cmp.core)
return cmp.core.view:get_active_entry() return cmp.core.view:get_active_entry()
end end

View File

@@ -3,6 +3,7 @@ local async = {}
---@class cmp.AsyncThrottle ---@class cmp.AsyncThrottle
---@field public timeout number ---@field public timeout number
---@field public stop function ---@field public stop function
---@field public raw function
---@field public __call function ---@field public __call function
---@param fn function ---@param fn function
@@ -12,11 +13,15 @@ async.throttle = function(fn, timeout)
local time = nil local time = nil
local timer = vim.loop.new_timer() local timer = vim.loop.new_timer()
return setmetatable({ return setmetatable({
running = false,
timeout = timeout, timeout = timeout,
stop = function() stop = function()
time = nil time = nil
timer:stop() timer:stop()
end, end,
raw = function(...)
fn(...)
end,
}, { }, {
__call = function(self, ...) __call = function(self, ...)
local args = { ... } local args = { ... }
@@ -28,9 +33,15 @@ async.throttle = function(fn, timeout)
local delta = math.max(1, self.timeout - (vim.loop.now() - time)) local delta = math.max(1, self.timeout - (vim.loop.now() - time))
timer:start(delta, 0, function() timer:start(delta, 0, function()
if not self.running then
time = nil time = nil
self.running = true
vim.schedule(function()
self.running = false
fn(unpack(args)) fn(unpack(args))
end) end)
end
end)
end, end,
}) })
end end

View File

@@ -198,7 +198,7 @@ end
---On entry change ---On entry change
view.on_entry_change = async.throttle( view.on_entry_change = async.throttle(
vim.schedule_wrap(function(self) function(self)
if not self:visible() then if not self:visible() then
return return
end end
@@ -225,7 +225,7 @@ view.on_entry_change = async.throttle(
else else
self.ghost_text_view:hide() self.ghost_text_view:hide()
end end
end), end,
20 20
) )