feat(plugin): `opts_extend` can be a list of dotted keys that will be extended instead of merged

This commit is contained in:
Folke Lemaitre 2024-06-07 09:02:52 +02:00
parent 89ddc59d19
commit 1f7b720cff
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 52 additions and 1 deletions

View File

@ -667,7 +667,27 @@ function M._values(root, plugin, prop, is_list)
end end
values = type(values) == "table" and values or { values } 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 end
return M return M

View File

@ -428,4 +428,35 @@ function M.lazy_require(module)
}) })
end 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 return M