feat(spec)!: setting a table to Plugin.config is now deprecated. Please use Plugin.opts instead. (backward compatible for now)

This commit is contained in:
Folke Lemaitre 2023-01-08 15:01:49 +01:00
parent 6a31b97e37
commit 7260a2b28b
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
7 changed files with 150 additions and 59 deletions

View file

@ -220,7 +220,7 @@ function M._load(plugin, reason, opts)
end
M.packadd(plugin.dir)
if plugin.config then
if plugin.config or plugin.opts then
M.config(plugin)
end
@ -231,13 +231,32 @@ 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)
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 fn
if type(plugin.config) == "function" then
fn = function()
plugin.config(plugin)
plugin.config(plugin, M.opts(plugin))
end
else
local normname = Util.normname(plugin.name)
@ -254,8 +273,8 @@ function M.config(plugin)
end
if #mods == 1 then
fn = function()
local opts = plugin.config
if opts == true then
local opts = M.opts(plugin)
if next(opts) == nil then
opts = nil
end
require(mods[1]).setup(opts)

View file

@ -87,6 +87,15 @@ function Spec:add(plugin, results, is_dep)
plugin.cmd = type(plugin.cmd) == "string" and { plugin.cmd } or plugin.cmd
plugin.ft = type(plugin.ft) == "string" and { plugin.ft } or plugin.ft
if type(plugin.config) == "table" then
self:warn(
"{" .. plugin.name .. "}: setting a table to `Plugin.config` is deprecated. Please use `Plugin.opts` instead"
)
---@diagnostic disable-next-line: assign-type-mismatch
plugin.opts = plugin.config
plugin.config = nil
end
plugin._ = {}
plugin._.dep = is_dep

View file

@ -31,11 +31,11 @@ return {
config = true, -- run require("neorg").setup()
},
-- or set a custom config:
-- or set custom options:
{
"nvim-neorg/neorg",
ft = "norg",
config = { foo = "bar" }, -- run require("neorg").setup({foo = "bar"})
opts = { foo = "bar" }, -- run require("neorg").setup({foo = "bar"})
},
{

View file

@ -68,8 +68,12 @@ function M.check_override(plugin)
return
end
local Handler = require("lazy.core.handler")
local skip = { "dependencies", "_", "opts" }
vim.list_extend(skip, vim.tbl_values(Handler.types))
for key, value in pairs(plugin._.super) do
if key ~= "_" and plugin[key] and plugin[key] ~= value then
if not vim.tbl_contains(skip, key) and plugin[key] and plugin[key] ~= value then
vim.health.report_warn("{" .. plugin.name .. "}: overriding <" .. key .. ">")
end
end
@ -77,29 +81,31 @@ end
M.valid = {
1,
"name",
"url",
"enabled",
"lazy",
"dev",
"dependencies",
"init",
"config",
"build",
"branch",
"tag",
"commit",
"version",
"module",
"pin",
"cmd",
"event",
"keys",
"ft",
"dir",
"priority",
"cond",
"_",
"branch",
"build",
"cmd",
"commit",
"cond",
"config",
"dependencies",
"dev",
"dir",
"enabled",
"event",
"ft",
"import",
"init",
"keys",
"lazy",
"module",
"name",
"opts",
"pin",
"priority",
"tag",
"url",
"version",
}
return M

View file

@ -15,10 +15,13 @@
---@field cond? boolean
---@field super? LazyPlugin
---@alias PluginOpts table|fun(self:LazyPlugin, opts:table):table?
---@class LazyPluginHooks
---@field init? fun(LazyPlugin) Will always be run
---@field config? fun(LazyPlugin)|true|table Will be executed when loading the plugin
---@field build? string|fun(LazyPlugin)|(string|fun(LazyPlugin))[]
---@field init? fun(self:LazyPlugin) Will always be run
---@field config? fun(self:LazyPlugin, opts:table)|true Will be executed when loading the plugin
---@field build? string|fun(self:LazyPlugin)|(string|fun(self:LazyPlugin))[]
---@field opts? PluginOpts
---@class LazyPluginHandlers
---@field event? string[]