lua: vim.wait implementation

This commit is contained in:
TJ DeVries
2020-05-20 11:08:19 -04:00
parent 504d6878da
commit be662fe5c7
4 changed files with 271 additions and 56 deletions

View File

@@ -827,6 +827,62 @@ vim.schedule({callback}) *vim.schedule()*
Schedules {callback} to be invoked soon by the main event-loop. Useful
to avoid |textlock| or other temporary restrictions.
vim.defer_fn({fn}, {timeout}) *vim.defer_fn*
Defers calling {fn} until {timeout} ms passes. Use to do a one-shot timer
that calls {fn}.
Parameters: ~
{fn} Callback to call once {timeout} expires
{timeout} Time in ms to wait before calling {fn}
Returns: ~
|vim.loop|.new_timer() object
vim.wait({time}, {callback} [, {interval}]) *vim.wait()*
Wait for {time} in milliseconds until {callback} returns `true`.
Executes {callback} immediately and at approximately {interval}
milliseconds (default 200). Nvim still processes other events during
this time.
Returns: ~
If {callback} returns `true` during the {time}:
`true, nil`
If {callback} never returns `true` during the {time}:
`false, -1`
If {callback} is interrupted during the {time}:
`false, -2`
If {callback} errors, the error is raised.
Examples: >
---
-- Wait for 100 ms, allowing other events to process
vim.wait(100, function() end)
---
-- Wait for 100 ms or until global variable set.
vim.wait(100, function() return vim.g.waiting_for_var end)
---
-- Wait for 1 second or until global variable set, checking every ~500 ms
vim.wait(1000, function() return vim.g.waiting_for_var end, 500)
---
-- Schedule a function to set a value in 100ms
vim.defer_fn(function() vim.g.timer_result = true end, 100)
-- Would wait ten seconds if results blocked. Actually only waits 100 ms
if vim.wait(10000, function() return vim.g.timer_result end) then
print('Only waiting a little bit of time!')
end
<
vim.fn.{func}({...}) *vim.fn*
Invokes |vim-function| or |user-function| {func} with arguments {...}.
To call autoload functions, use the syntax: >

View File

@@ -46,31 +46,6 @@ local function is_dir(filename)
return stat and stat.type == 'directory' or false
end
-- TODO Use vim.wait when that is available, but provide an alternative for now.
local wait = vim.wait or function(timeout_ms, condition, interval)
validate {
timeout_ms = { timeout_ms, 'n' };
condition = { condition, 'f' };
interval = { interval, 'n', true };
}
assert(timeout_ms > 0, "timeout_ms must be > 0")
local _ = log.debug() and log.debug("wait.fallback", timeout_ms)
interval = interval or 200
local interval_cmd = "sleep "..interval.."m"
local timeout = timeout_ms + uv.now()
-- TODO is there a better way to sync this?
while true do
uv.update_time()
if condition() then
return 0
end
if uv.now() >= timeout then
return -1
end
nvim_command(interval_cmd)
-- vim.loop.sleep(10)
end
end
local wait_result_reason = { [-1] = "timeout"; [-2] = "interrupted"; [-3] = "error" }
local valid_encodings = {
@@ -810,8 +785,8 @@ function lsp._vim_exit_handler()
for _, client in pairs(active_clients) do
client.stop()
end
local wait_result = wait(500, function() return tbl_isempty(active_clients) end, 50)
if wait_result ~= 0 then
if not vim.wait(500, function() return tbl_isempty(active_clients) end, 50) then
for _, client in pairs(active_clients) do
client.stop(true)
end
@@ -889,12 +864,14 @@ function lsp.buf_request_sync(bufnr, method, params, timeout_ms)
for _ in pairs(client_request_ids) do
expected_result_count = expected_result_count + 1
end
local wait_result = wait(timeout_ms or 100, function()
local wait_result, reason = vim.wait(timeout_ms or 100, function()
return result_count >= expected_result_count
end, 10)
if wait_result ~= 0 then
if not wait_result then
cancel()
return nil, wait_result_reason[wait_result]
return nil, wait_result_reason[reason]
end
return request_results
end