aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/shared.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/shared.lua')
-rw-r--r--runtime/lua/vim/shared.lua83
1 files changed, 42 insertions, 41 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua
index dd05299295..291b003d87 100644
--- a/runtime/lua/vim/shared.lua
+++ b/runtime/lua/vim/shared.lua
@@ -8,6 +8,45 @@
vim = vim or {}
+local function _id(v)
+ return v
+end
+
+local deepcopy
+
+local deepcopy_funcs = {
+ table = function(orig, cache)
+ if cache[orig] then
+ return cache[orig]
+ end
+ local copy = {}
+
+ cache[orig] = copy
+ local mt = getmetatable(orig)
+ for k, v in pairs(orig) do
+ copy[deepcopy(k, cache)] = deepcopy(v, cache)
+ end
+ return setmetatable(copy, mt)
+ end,
+ number = _id,
+ string = _id,
+ ['nil'] = _id,
+ boolean = _id,
+ ['function'] = _id,
+}
+
+deepcopy = function(orig, _cache)
+ local f = deepcopy_funcs[type(orig)]
+ if f then
+ return f(orig, _cache or {})
+ else
+ if type(orig) == 'userdata' and orig == vim.NIL then
+ return vim.NIL
+ end
+ error('Cannot deepcopy object of type ' .. type(orig))
+ end
+end
+
--- Returns a deep copy of the given object. Non-table objects are copied as
--- in a typical Lua assignment, whereas table objects are copied recursively.
--- Functions are naively copied, so functions in the copied table point to the
@@ -17,45 +56,9 @@ vim = vim or {}
---@generic T: table
---@param orig T Table to copy
---@return T Table of copied keys and (nested) values.
-function vim.deepcopy(orig) end -- luacheck: no unused
-vim.deepcopy = (function()
- local function _id(v)
- return v
- end
-
- local deepcopy_funcs = {
- table = function(orig, cache)
- if cache[orig] then
- return cache[orig]
- end
- local copy = {}
-
- cache[orig] = copy
- local mt = getmetatable(orig)
- for k, v in pairs(orig) do
- copy[vim.deepcopy(k, cache)] = vim.deepcopy(v, cache)
- end
- return setmetatable(copy, mt)
- end,
- number = _id,
- string = _id,
- ['nil'] = _id,
- boolean = _id,
- ['function'] = _id,
- }
-
- return function(orig, cache)
- local f = deepcopy_funcs[type(orig)]
- if f then
- return f(orig, cache or {})
- else
- if type(orig) == 'userdata' and orig == vim.NIL then
- return vim.NIL
- end
- error('Cannot deepcopy object of type ' .. type(orig))
- end
- end
-end)()
+function vim.deepcopy(orig)
+ return deepcopy(orig)
+end
--- Splits a string at each instance of a separator.
---
@@ -318,7 +321,6 @@ function vim.tbl_isempty(t)
end
--- We only merge empty tables or tables that are not an array (indexed by integers)
----@private
local function can_merge(v)
return type(v) == 'table' and (vim.tbl_isempty(v) or not vim.tbl_isarray(v))
end
@@ -770,7 +772,6 @@ do
return type(val) == t or (t == 'callable' and vim.is_callable(val))
end
- ---@private
local function is_valid(opt)
if type(opt) ~= 'table' then
return false, string.format('opt: expected table, got %s', type(opt))