Skip to content

Commit 1f7b720

Browse files
committed
feat(plugin): opts_extend can be a list of dotted keys that will be extended instead of merged
1 parent 89ddc59 commit 1f7b720

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Diff for: lua/lazy/core/plugin.lua

+21-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,27 @@ function M._values(root, plugin, prop, is_list)
667667
end
668668

669669
values = type(values) == "table" and values or { values }
670-
return is_list and Util.extend(ret, values) or Util.merge(ret, values)
670+
if is_list then
671+
return Util.extend(ret, values)
672+
else
673+
---@type {path:string[], list:any[]}[]
674+
local lists = {}
675+
for _, key in ipairs(plugin[prop .. "_extend"] or {}) do
676+
local path = vim.split(key, ".", { plain = true })
677+
local r = Util.key_get(ret, path)
678+
local v = Util.key_get(values, path)
679+
if type(r) == "table" and type(v) == "table" then
680+
lists[key] = { path = path, list = {} }
681+
vim.list_extend(lists[key].list, r)
682+
vim.list_extend(lists[key].list, v)
683+
end
684+
end
685+
local t = Util.merge(ret, values)
686+
for _, list in pairs(lists) do
687+
Util.key_set(t, list.path, list.list)
688+
end
689+
return t
690+
end
671691
end
672692

673693
return M

Diff for: lua/lazy/core/util.lua

+31
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,35 @@ function M.lazy_require(module)
428428
})
429429
end
430430

431+
---@param t table
432+
---@param key string|string[]
433+
---@return any
434+
function M.key_get(t, key)
435+
local path = type(key) == "table" and key or vim.split(key, ".", true)
436+
local value = t
437+
for _, k in ipairs(path) do
438+
if type(value) ~= "table" then
439+
return value
440+
end
441+
value = value[k]
442+
end
443+
return value
444+
end
445+
446+
---@param t table
447+
---@param key string|string[]
448+
---@param value any
449+
function M.key_set(t, key, value)
450+
local path = type(key) == "table" and key or vim.split(key, ".", true)
451+
local last = t
452+
for i = 1, #path - 1 do
453+
local k = path[i]
454+
if type(last[k]) ~= "table" then
455+
last[k] = {}
456+
end
457+
last = last[k]
458+
end
459+
last[path[#path]] = value
460+
end
461+
431462
return M

0 commit comments

Comments
 (0)