Skip to content

Commit 6a31b97

Browse files
committed
feat(util): better deep merging with Util.merge
1 parent b178daf commit 6a31b97

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lua/lazy/core/util.lua

+27
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,31 @@ function M.debug(msg, level, opts)
286286
end
287287
end
288288

289+
local function can_merge(v)
290+
return type(v) == "table" and (vim.tbl_isempty(v) or not M.is_list(v))
291+
end
292+
293+
--- Merges the values similar to vim.tbl_deep_extend with the **force** behavior,
294+
--- but the values can be any type, in which case they override the values on the left.
295+
--- Values will me merged in-place in the first left-most table. If you want the result to be in
296+
--- a new table, then simply pass an empty table as the first argument `vim.merge({}, ...)`
297+
--- Supports clearing values by setting a key to `vim.NIL`
298+
function M.merge(...)
299+
local values = { ... }
300+
local ret = values[1]
301+
for i = 2, #values, 1 do
302+
local value = values[i]
303+
if can_merge(ret) and can_merge(value) then
304+
for k, v in pairs(value) do
305+
ret[k] = M.merge(ret[k], v)
306+
end
307+
elseif value == vim.NIL then
308+
ret = nil
309+
else
310+
ret = value
311+
end
312+
end
313+
return ret
314+
end
315+
289316
return M

0 commit comments

Comments
 (0)