-- Functions shared by Nvim and its test-suite.
--
-- These are "pure" lua functions not depending of the state of the editor.
-- Thus they should always be available whenever nvim-related lua code is run,
-- regardless if it is code in the editor itself, or in worker threads/processes,
-- or the test suite. (Eventually the test suite will be run in a worker process,
-- so this wouldn't be a separate case to consider)
---@nodoc
_G.vim = _G.vim or {} --[[@as table]]
-- TODO(lewis6991): better fix for flaky luals
---@generic T
---@param orig T
---@param cache? table<any,any>
---@return T
local function deepcopy(orig, cache)
if orig == vim.NIL then
return vim.NIL
elseif type(orig) == 'userdata' or type(orig) == 'thread' then
error('Cannot deepcopy object of type ' .. type(orig))
elseif type(orig) ~= 'table' then
return orig
end
--- @cast orig table<any,any>
if cache and cache[orig] then
return cache[orig]
end
local copy = {} --- @type table<any,any>
if cache then
cache[orig] = copy
end
for k, v in pairs(orig) do
copy[deepcopy(k, cache)] = deepcopy(v, cache)
end
return setmetatable(copy, getmetatable(orig))
end
--- Returns a deep copy of the given object. Non-table objects are copi
|