Skip to content

Commit

Permalink
feat(util): better deep merging with Util.merge
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 8, 2023
1 parent b178daf commit 6a31b97
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,31 @@ function M.debug(msg, level, opts)
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

0 comments on commit 6a31b97

Please sign in to comment.