Skip to content

Commit 7260a2b

Browse files
committed
feat(spec)!: setting a table to Plugin.config is now deprecated. Please use Plugin.opts instead. (backward compatible for now)
1 parent 6a31b97 commit 7260a2b

File tree

7 files changed

+146
-55
lines changed

7 files changed

+146
-55
lines changed

README.md

+46-27
Large diffs are not rendered by default.

lua/lazy/core/loader.lua

+23-4
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function M._load(plugin, reason, opts)
220220
end
221221

222222
M.packadd(plugin.dir)
223-
if plugin.config then
223+
if plugin.config or plugin.opts then
224224
M.config(plugin)
225225
end
226226

@@ -231,13 +231,32 @@ function M._load(plugin, reason, opts)
231231
end)
232232
end
233233

234+
-- Merges super opts or runs the opts function to override opts or return new ones
235+
---@param plugin LazyPlugin
236+
function M.opts(plugin)
237+
local opts = plugin._.super and M.opts(plugin._.super) or {}
238+
---@type PluginOpts?
239+
local plugin_opts = rawget(plugin, "opts")
240+
241+
if type(plugin_opts) == "table" then
242+
opts = Util.merge(opts, plugin_opts)
243+
elseif type(plugin_opts) == "function" then
244+
local new_opts = plugin_opts(plugin, opts)
245+
if new_opts then
246+
opts = new_opts
247+
end
248+
end
249+
250+
return opts
251+
end
252+
234253
--- runs plugin config
235254
---@param plugin LazyPlugin
236255
function M.config(plugin)
237256
local fn
238257
if type(plugin.config) == "function" then
239258
fn = function()
240-
plugin.config(plugin)
259+
plugin.config(plugin, M.opts(plugin))
241260
end
242261
else
243262
local normname = Util.normname(plugin.name)
@@ -254,8 +273,8 @@ function M.config(plugin)
254273
end
255274
if #mods == 1 then
256275
fn = function()
257-
local opts = plugin.config
258-
if opts == true then
276+
local opts = M.opts(plugin)
277+
if next(opts) == nil then
259278
opts = nil
260279
end
261280
require(mods[1]).setup(opts)

lua/lazy/core/plugin.lua

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ function Spec:add(plugin, results, is_dep)
8787
plugin.cmd = type(plugin.cmd) == "string" and { plugin.cmd } or plugin.cmd
8888
plugin.ft = type(plugin.ft) == "string" and { plugin.ft } or plugin.ft
8989

90+
if type(plugin.config) == "table" then
91+
self:warn(
92+
"{" .. plugin.name .. "}: setting a table to `Plugin.config` is deprecated. Please use `Plugin.opts` instead"
93+
)
94+
---@diagnostic disable-next-line: assign-type-mismatch
95+
plugin.opts = plugin.config
96+
plugin.config = nil
97+
end
98+
9099
plugin._ = {}
91100
plugin._.dep = is_dep
92101

lua/lazy/example.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ return {
3131
config = true, -- run require("neorg").setup()
3232
},
3333

34-
-- or set a custom config:
34+
-- or set custom options:
3535
{
3636
"nvim-neorg/neorg",
3737
ft = "norg",
38-
config = { foo = "bar" }, -- run require("neorg").setup({foo = "bar"})
38+
opts = { foo = "bar" }, -- run require("neorg").setup({foo = "bar"})
3939
},
4040

4141
{

lua/lazy/health.lua

+25-19
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,44 @@ function M.check_override(plugin)
6868
return
6969
end
7070

71+
local Handler = require("lazy.core.handler")
72+
local skip = { "dependencies", "_", "opts" }
73+
vim.list_extend(skip, vim.tbl_values(Handler.types))
74+
7175
for key, value in pairs(plugin._.super) do
72-
if key ~= "_" and plugin[key] and plugin[key] ~= value then
76+
if not vim.tbl_contains(skip, key) and plugin[key] and plugin[key] ~= value then
7377
vim.health.report_warn("{" .. plugin.name .. "}: overriding <" .. key .. ">")
7478
end
7579
end
7680
end
7781

7882
M.valid = {
7983
1,
80-
"name",
81-
"url",
82-
"enabled",
83-
"lazy",
84-
"dev",
85-
"dependencies",
86-
"init",
87-
"config",
88-
"build",
84+
"_",
8985
"branch",
90-
"tag",
91-
"commit",
92-
"version",
93-
"module",
94-
"pin",
86+
"build",
9587
"cmd",
88+
"commit",
89+
"cond",
90+
"config",
91+
"dependencies",
92+
"dev",
93+
"dir",
94+
"enabled",
9695
"event",
97-
"keys",
9896
"ft",
99-
"dir",
97+
"import",
98+
"init",
99+
"keys",
100+
"lazy",
101+
"module",
102+
"name",
103+
"opts",
104+
"pin",
100105
"priority",
101-
"cond",
102-
"_",
106+
"tag",
107+
"url",
108+
"version",
103109
}
104110

105111
return M

lua/lazy/types.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
---@field cond? boolean
1616
---@field super? LazyPlugin
1717

18+
---@alias PluginOpts table|fun(self:LazyPlugin, opts:table):table?
19+
1820
---@class LazyPluginHooks
19-
---@field init? fun(LazyPlugin) Will always be run
20-
---@field config? fun(LazyPlugin)|true|table Will be executed when loading the plugin
21-
---@field build? string|fun(LazyPlugin)|(string|fun(LazyPlugin))[]
21+
---@field init? fun(self:LazyPlugin) Will always be run
22+
---@field config? fun(self:LazyPlugin, opts:table)|true Will be executed when loading the plugin
23+
---@field build? string|fun(self:LazyPlugin)|(string|fun(self:LazyPlugin))[]
24+
---@field opts? PluginOpts
2225

2326
---@class LazyPluginHandlers
2427
---@field event? string[]

tests/core/plugin_spec.lua

+35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local Config = require("lazy.core.config")
22
local Plugin = require("lazy.core.plugin")
3+
local Loader = require("lazy.core.loader")
34

45
local assert = require("luassert")
56

@@ -272,3 +273,37 @@ describe("plugin spec opt", function()
272273
end
273274
end)
274275
end)
276+
277+
describe("plugin opts", function()
278+
it("correctly parses opts", function()
279+
---@type {spec:LazySpec, opts:table}[]
280+
local tests = {
281+
{
282+
spec = { { "foo/foo", opts = { a = 1, b = 1 } }, { "foo/foo", opts = { a = 2 } } },
283+
opts = { a = 2, b = 1 },
284+
},
285+
{
286+
spec = { { "foo/foo", config = { a = 1, b = 1 } }, { "foo/foo", opts = { a = 2 } } },
287+
opts = { a = 2, b = 1 },
288+
},
289+
{
290+
spec = { { "foo/foo", opts = { a = 1, b = 1 } }, { "foo/foo", config = { a = 2 } } },
291+
opts = { a = 2, b = 1 },
292+
},
293+
{
294+
spec = { { "foo/foo", config = { a = 1, b = 1 } }, { "foo/foo", config = { a = 2 } } },
295+
opts = { a = 2, b = 1 },
296+
},
297+
{
298+
spec = { { "foo/foo", config = { a = 1, b = 1 } }, { "foo/foo", config = { a = vim.NIL } } },
299+
opts = { b = 1 },
300+
},
301+
}
302+
303+
for _, test in ipairs(tests) do
304+
local spec = Plugin.Spec.new(test.spec)
305+
assert(spec.plugins.foo)
306+
assert.same(test.opts, Loader.opts(spec.plugins.foo))
307+
end
308+
end)
309+
end)

0 commit comments

Comments
 (0)