mirror of https://github.com/folke/lazy.nvim.git
feat(util): better deep merging with `Util.merge`
This commit is contained in:
parent
b178daf9dc
commit
6a31b97e37
|
@ -286,4 +286,31 @@ function M.debug(msg, level, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function can_merge(v)
|
||||||
|
return type(v) == "table" and (vim.tbl_isempty(v) or not M.is_list(v))
|
||||||
|
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
|
||||||
|
--- a new table, then simply pass an empty table as the first argument `vim.merge({}, ...)`
|
||||||
|
--- Supports clearing values by setting a key to `vim.NIL`
|
||||||
|
function M.merge(...)
|
||||||
|
local values = { ... }
|
||||||
|
local ret = values[1]
|
||||||
|
for i = 2, #values, 1 do
|
||||||
|
local value = values[i]
|
||||||
|
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 value == vim.NIL then
|
||||||
|
ret = nil
|
||||||
|
else
|
||||||
|
ret = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Reference in New Issue