refactor: removed special `__merge` functionality

This commit is contained in:
Folke Lemaitre 2023-01-12 13:13:30 +01:00
parent 2128ca90fb
commit 784bb3c100
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 1 additions and 25 deletions

View File

@ -298,27 +298,10 @@ function M.debug(msg, opts)
end
end
local MERGE = "__merge"
local function can_merge(v)
return type(v) == "table" and (vim.tbl_isempty(v) or not M.is_list(v))
end
local function can_extend(v)
return type(v) == "table" and (vim.tbl_isempty(v) or M.is_list(v))
end
---@param v any|{__merge:boolean}
---@param merge? boolean
---@return boolean?
local function check_merge(v, merge)
if type(v) == "table" and v[MERGE] ~= nil then
merge = v[MERGE]
v[MERGE] = nil
end
return merge
end
--- Merges the values similar to vim.tbl_deep_extend with the **force** behavior,
--- but the values can be any type, in which case they override the values on the left.
--- Values will me merged in-place in the first left-most table. If you want the result to be in
@ -335,19 +318,12 @@ function M.merge(...)
ret = nil
end
local merge = check_merge(ret)
for i = 2, #values, 1 do
local value = values[i]
merge = check_merge(value, merge)
if can_merge(ret) and can_merge(value) and merge ~= false then
if can_merge(ret) and can_merge(value) then
for k, v in pairs(value) do
ret[k] = M.merge(ret[k], v)
end
elseif can_extend(ret) and can_extend(value) and merge then
for _, v in ipairs(value) do
ret[#ret + 1] = v
end
elseif value == vim.NIL then
ret = nil
else