* WIP * WIP * Fix #226 * Insert text * Emulate vim native * テキトウ * Tekito * Move scrollbar impl * aaa * Ignore unexpected event * fix * fix scroll * Refactor (conflict...) * Fix bug * Positive integer * Refactor a bit * Fix for pumheight=0 * fx * Improve matching highlight * Improve colorscheme handling * fmt * Add cmp.visible * Fix pum pos * ABBR_MARGIN * Fix cel calculation * up * refactor * fix * a * a * compat * Remove current completion state * Fix ghost text * Add feature toggle * highlight customization * Update * Add breaking change announcement * Add README.md * Remove unused function * extmark ephemeral ghost text * Support native comp * Fix docs pos * a * Remove if native menu visible * theme async * Improvement idea: option to disables insert on select item (#240) * use ghost text instead of insertion on prev/next item * add disables_insert_on_selection option * move disable_insert_on_select option as argumet on * update README * use an enum behavior to disable insert on select * Adopt contribution * Preselect * Improve * Change configuration option * a * Improve * Improve * Implement proper <C-e> behavior to native/custom * Support <C-c> maybe * Improve docs view * Improve * Avoid syntax leak * TODO: refactor * Fix * Revert win pos * fmt * ghost text remaining * Don't use italic by default * bottom * dedup by label * Ignore events * up * Hacky native view partial support * up * perf * improve * more cache * fmt * Fix format option * fmt * recheck * Fix * Improve * Improve * compat * implement redraw * improve * up * fmt/lint * immediate ghost text * source timeout * up * Support multibyte * disable highlight * up * improve * fmt * fmt * fix * fix * up * up * Use screenpos * Add undojoin check * Fix height * matcher bug * Fix dot-repeat * Remove undojoin * macro * Support dot-repeat * MacroSafe * Default item count is 200 * fmt Co-authored-by: Eric Puentes <eric.puentes@mercadolibre.com.co>
90 lines
1.7 KiB
Lua
90 lines
1.7 KiB
Lua
local async = {}
|
|
|
|
---@class cmp.AsyncThrottle
|
|
---@field public timeout number
|
|
---@field public stop function
|
|
---@field public __call function
|
|
|
|
---@param fn function
|
|
---@param timeout number
|
|
---@return cmp.AsyncThrottle
|
|
async.throttle = function(fn, timeout)
|
|
local time = nil
|
|
local timer = vim.loop.new_timer()
|
|
return setmetatable({
|
|
timeout = timeout,
|
|
stop = function()
|
|
time = nil
|
|
timer:stop()
|
|
end,
|
|
}, {
|
|
__call = function(self, ...)
|
|
local args = { ... }
|
|
|
|
if time == nil then
|
|
time = vim.loop.now()
|
|
end
|
|
timer:stop()
|
|
|
|
local delta = math.max(1, self.timeout - (vim.loop.now() - time))
|
|
timer:start(delta, 0, function()
|
|
time = nil
|
|
fn(unpack(args))
|
|
end)
|
|
end,
|
|
})
|
|
end
|
|
|
|
---Timeout callback function
|
|
---@param fn function
|
|
---@param timeout number
|
|
---@return function
|
|
async.timeout = function(fn, timeout)
|
|
local timer
|
|
local done = false
|
|
local callback = function(...)
|
|
if not done then
|
|
done = true
|
|
timer:stop()
|
|
timer:close()
|
|
fn(...)
|
|
end
|
|
end
|
|
timer = vim.loop.new_timer()
|
|
timer:start(timeout, 0, function()
|
|
callback()
|
|
end)
|
|
return callback
|
|
end
|
|
|
|
---@alias cmp.AsyncDedup fun(callback: function): function
|
|
|
|
---Create deduplicated callback
|
|
---@return function
|
|
async.dedup = function()
|
|
local id = 0
|
|
return function(callback)
|
|
id = id + 1
|
|
|
|
local current = id
|
|
return function(...)
|
|
if current == id then
|
|
callback(...)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
---Convert async process as sync
|
|
async.sync = function(runner, timeout)
|
|
local done = false
|
|
runner(function()
|
|
done = true
|
|
end)
|
|
vim.wait(timeout, function()
|
|
return done
|
|
end, 10, false)
|
|
end
|
|
|
|
return async
|