local tointeger = vim._tointeger
-- Nvim-Lua stdlib: the `vim` module (:help lua-stdlib)
--
-- These are for loading runtime modules lazily since they aren't available in
-- the nvim binary as specified in executor.c
for k, v in pairs({
treesitter = true,
filetype = true,
loader = true,
func = true,
F = true,
log = true,
lsp = true,
hl = true,
diagnostic = true,
keymap = true,
ui = true,
health = true,
secure = true,
snippet = true,
pack = true,
async = true,
_watch = true,
net = true,
pos = true,
range = true,
}) do
vim._submodules[k] = v
end
-- There are things which have special rules in vim._init_packages
-- for legacy reasons (uri) or for performance (_inspector).
-- most new things should go into a submodule namespace ( vim.foobar.do_thing() )
vim._extra = {
uri_from_fname = true,
uri_from_bufnr = true,
uri_to_fname = true,
uri_to_bufnr = true,
show_pos = true,
inspect_pos = true,
}
--- Waits up to `time` milliseconds, until `callback` returns `true` (success). Executes
--- `callback` immediately, then on user events, internal events, and approximately every
--- `interval` milliseconds. Returns `true` plus any remaining callback results on success.
---
--- Nvim processes other events while waiting.
--- Cannot be called during an |api-fast| event.
---
--- Examples:
---
--- ```lua
--- -- Wait for 100 ms, allowing other events to process.
--- vim.wait(100)
---
--- -- Wait up to 1000 ms or until `vim.g.foo` is true, at intervals of ~500 ms.
--- vim.wait(1000, function() return vim.g.foo end, 500)
---
--- -- Wait indefinitely until `vim.g.foo` is true, and get the callback results.
--- local ok, rv1, rv2, rv3 = vim.wait(math.huge, function()
--- return vim.g.foo, 'a', 42, { ok = { 'yes' } }
--- end)
---
--- -- Schedule a function to set a value in 100ms. This would wait 10s if blocked, but actually
--- -- only waits 100ms because `vim.wait` processes other events while waiting.
--- vim.defer_fn(function() vim.g.timer_result = true end, 100)
--- if vim.wait(10000, function() return vim.g.timer_result end) then
--- print('Only waiting a little bit of time!')
--- end
---
--- -- Yield via vim.wait() to allow CTRL-C to interrupt Lua code.
--- while true do
--- -- ...work...
--- local _, code = vim.wait(0, nil, 0)
--- if code == -2 then -- CTRL-C.
--- break
--- end
--- end
--- ```
---
--- @param time number Number of milliseconds to wait. Must be non-negative number, any fractional
--- part is truncated.
--- @param callback? fun(): boolean, ... Optional callback. Waits until {callback} returns true
--- @param interval? integer (default: 200) Milliseconds (approximate) to wait between polls.
--- @param fast_only? boolean If true, only |api-fast| events will be processed.
--- @return boolean, nil|-1|-2, ...
--- - If callback returns `true` before timeout: `true, ...` (remaining callback results).
--- - On timeout: `false, -1`
--- - On interrupt: `false, -2`
--- - On error: the error is raised.
function vim.wait(time, callback, interval, fast_only)
if vim.in_fast_event() then
error('E5560: vim.wait must not be called in a fast event context', 0)
end
vim.validate('time', time, 'number')
if time < 0 then
error('timeout must be >= 0')
end
local has_deadline = time == time and time ~= math.huge
if has_deadline then
time = math.floor(time)
end
vim.
|