feat(spec): event, keys, ft and cmd can now also be a function that returns the values to be used

This commit is contained in:
Folke Lemaitre 2023-01-12 13:07:51 +01:00
parent bcd87a0215
commit 2128ca90fb
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
6 changed files with 76 additions and 75 deletions

View file

@ -231,33 +231,14 @@ function M._load(plugin, reason, opts)
end)
end
-- Merges super opts or runs the opts function to override opts or return new ones
---@param plugin LazyPlugin
function M.opts(plugin)
---@type table
local opts = plugin._.super and M.opts(plugin._.super) or {}
---@type PluginOpts?
local plugin_opts = rawget(plugin, "opts")
if type(plugin_opts) == "table" then
opts = Util.merge(opts, plugin_opts)
elseif type(plugin_opts) == "function" then
local new_opts = plugin_opts(plugin, opts)
if new_opts then
opts = new_opts
end
end
return opts
end
--- runs plugin config
---@param plugin LazyPlugin
function M.config(plugin)
local opts = Plugin.values(plugin, "opts", false)
local fn
if type(plugin.config) == "function" then
fn = function()
plugin.config(plugin, M.opts(plugin))
plugin.config(plugin, opts)
end
else
local normname = Util.normname(plugin.name)
@ -274,7 +255,7 @@ function M.config(plugin)
end
if #mods == 1 then
fn = function()
require(mods[1]).setup(M.opts(plugin))
require(mods[1]).setup(opts)
end
else
return Util.error(