Fix #754
This commit is contained in:
@@ -261,15 +261,27 @@ end
|
||||
|
||||
---Update completion menu
|
||||
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
|
||||
|
||||
-- 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
|
||||
ignore = ignore or not api.is_suitable_mode()
|
||||
if ignore then
|
||||
return
|
||||
end
|
||||
|
||||
-- Check fetching sources.
|
||||
local sources = {}
|
||||
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
|
||||
@@ -281,8 +293,6 @@ core.filter = async.throttle(
|
||||
table.insert(sources, s)
|
||||
end
|
||||
|
||||
local ctx = self:get_context()
|
||||
|
||||
-- Display completion results.
|
||||
self.view:open(ctx, sources)
|
||||
|
||||
@@ -297,7 +307,8 @@ core.filter = async.throttle(
|
||||
end) == 0 then
|
||||
config.set_onetime({})
|
||||
end
|
||||
end),
|
||||
end
|
||||
}),
|
||||
THROTTLE_TIME
|
||||
)
|
||||
|
||||
|
||||
@@ -67,11 +67,13 @@ end
|
||||
|
||||
---Get current selected entry or nil
|
||||
cmp.get_selected_entry = function()
|
||||
cmp.core.filter.raw(cmp.core)
|
||||
return cmp.core.view:get_selected_entry()
|
||||
end
|
||||
|
||||
---Get current active entry or nil
|
||||
cmp.get_active_entry = function()
|
||||
cmp.core.filter.raw(cmp.core)
|
||||
return cmp.core.view:get_active_entry()
|
||||
end
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ local async = {}
|
||||
---@class cmp.AsyncThrottle
|
||||
---@field public timeout number
|
||||
---@field public stop function
|
||||
---@field public raw function
|
||||
---@field public __call function
|
||||
|
||||
---@param fn function
|
||||
@@ -12,11 +13,15 @@ async.throttle = function(fn, timeout)
|
||||
local time = nil
|
||||
local timer = vim.loop.new_timer()
|
||||
return setmetatable({
|
||||
running = false,
|
||||
timeout = timeout,
|
||||
stop = function()
|
||||
time = nil
|
||||
timer:stop()
|
||||
end,
|
||||
raw = function(...)
|
||||
fn(...)
|
||||
end,
|
||||
}, {
|
||||
__call = function(self, ...)
|
||||
local args = { ... }
|
||||
@@ -28,9 +33,15 @@ async.throttle = function(fn, timeout)
|
||||
|
||||
local delta = math.max(1, self.timeout - (vim.loop.now() - time))
|
||||
timer:start(delta, 0, function()
|
||||
if not self.running then
|
||||
time = nil
|
||||
self.running = true
|
||||
vim.schedule(function()
|
||||
self.running = false
|
||||
fn(unpack(args))
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
@@ -198,7 +198,7 @@ end
|
||||
|
||||
---On entry change
|
||||
view.on_entry_change = async.throttle(
|
||||
vim.schedule_wrap(function(self)
|
||||
function(self)
|
||||
if not self:visible() then
|
||||
return
|
||||
end
|
||||
@@ -225,7 +225,7 @@ view.on_entry_change = async.throttle(
|
||||
else
|
||||
self.ghost_text_view:hide()
|
||||
end
|
||||
end),
|
||||
end,
|
||||
20
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user