Skip to content

Commit

Permalink
feat(plugin): opts_extend can be a list of dotted keys that will be…
Browse files Browse the repository at this point in the history
… extended instead of merged
  • Loading branch information
folke committed Jun 7, 2024
1 parent 89ddc59 commit 1f7b720
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,27 @@ function M._values(root, plugin, prop, is_list)
end

values = type(values) == "table" and values or { values }
return is_list and Util.extend(ret, values) or Util.merge(ret, values)
if is_list then
return Util.extend(ret, values)
else
---@type {path:string[], list:any[]}[]
local lists = {}
for _, key in ipairs(plugin[prop .. "_extend"] or {}) do
local path = vim.split(key, ".", { plain = true })
local r = Util.key_get(ret, path)
local v = Util.key_get(values, path)
if type(r) == "table" and type(v) == "table" then
lists[key] = { path = path, list = {} }
vim.list_extend(lists[key].list, r)
vim.list_extend(lists[key].list, v)
end
end
local t = Util.merge(ret, values)
for _, list in pairs(lists) do
Util.key_set(t, list.path, list.list)
end
return t
end
end

return M
31 changes: 31 additions & 0 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,35 @@ function M.lazy_require(module)
})
end

---@param t table
---@param key string|string[]
---@return any
function M.key_get(t, key)
local path = type(key) == "table" and key or vim.split(key, ".", true)
local value = t
for _, k in ipairs(path) do
if type(value) ~= "table" then
return value
end
value = value[k]
end
return value
end

---@param t table
---@param key string|string[]
---@param value any
function M.key_set(t, key, value)
local path = type(key) == "table" and key or vim.split(key, ".", true)
local last = t
for i = 1, #path - 1 do
local k = path[i]
if type(last[k]) ~= "table" then
last[k] = {}
end
last = last[k]
end
last[path[#path]] = value
end

return M

0 comments on commit 1f7b720

Please sign in to comment.