Skip to content

Commit 74fd361

Browse files
committed
feat(util): opts merging now supports lists extending by tagging a table with __extend = true. Use with care
1 parent 70f2c09 commit 74fd361

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lua/lazy/core/util.lua

+13-1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ local function can_merge(v)
387387
return type(v) == "table" and (vim.tbl_isempty(v) or not M.is_list(v))
388388
end
389389

390+
local function can_extend(v)
391+
return type(v) == "table" and v.__extend
392+
end
393+
390394
--- Merges the values similar to vim.tbl_deep_extend with the **force** behavior,
391395
--- but the values can be any type, in which case they override the values on the left.
392396
--- Values will me merged in-place in the first left-most table. If you want the result to be in
@@ -402,7 +406,15 @@ function M.merge(...)
402406
end
403407
for i = 2, select("#", ...) do
404408
local value = select(i, ...)
405-
if can_merge(ret) and can_merge(value) then
409+
local are_t = type(ret) == "table" and type(value) == "table"
410+
if are_t and (can_extend(ret) or can_extend(value)) then
411+
for k, v in pairs(value) do
412+
if k ~= "__extend" then
413+
table.insert(ret, v)
414+
end
415+
end
416+
ret.__extend = true
417+
elseif are_t and can_merge(ret) and can_merge(value) then
406418
for k, v in pairs(value) do
407419
ret[k] = M.merge(ret[k], v)
408420
end

tests/core/util_spec.lua

+12
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ describe("util", function()
134134
input = { { a = 1 }, { b = 2, a = vim.NIL } },
135135
output = { b = 2 },
136136
},
137+
{
138+
input = { { 1, 2, __extend = true }, { 3, 4 } },
139+
output = { 1, 2, 3, 4, __extend = true },
140+
},
141+
{
142+
input = { { 1, 2, __extend = true }, { __extend = true, 3, 4 } },
143+
output = { 1, 2, 3, 4, __extend = true },
144+
},
145+
{
146+
input = { { 1, 2 }, { 3, 4, __extend = true } },
147+
output = { 1, 2, 3, 4, __extend = true },
148+
},
137149
}
138150

139151
for _, test in ipairs(tests) do

0 commit comments

Comments
 (0)