Skip to content

Commit

Permalink
feat(spec)!: setting a table to Plugin.config is now deprecated. Pl…
Browse files Browse the repository at this point in the history
…ease use `Plugin.opts` instead. (backward compatible for now)
  • Loading branch information
folke committed Jan 8, 2023
1 parent 6a31b97 commit 7260a2b
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 55 deletions.
73 changes: 46 additions & 27 deletions README.md

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lua/lazy/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
},

{
Expand Down
44 changes: 25 additions & 19 deletions lua/lazy/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,44 @@ 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
end

M.valid = {
1,
"name",
"url",
"enabled",
"lazy",
"dev",
"dependencies",
"init",
"config",
"build",
"_",
"branch",
"tag",
"commit",
"version",
"module",
"pin",
"build",
"cmd",
"commit",
"cond",
"config",
"dependencies",
"dev",
"dir",
"enabled",
"event",
"keys",
"ft",
"dir",
"import",
"init",
"keys",
"lazy",
"module",
"name",
"opts",
"pin",
"priority",
"cond",
"_",
"tag",
"url",
"version",
}

return M
9 changes: 6 additions & 3 deletions lua/lazy/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
35 changes: 35 additions & 0 deletions tests/core/plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Config = require("lazy.core.config")
local Plugin = require("lazy.core.plugin")
local Loader = require("lazy.core.loader")

local assert = require("luassert")

Expand Down Expand Up @@ -272,3 +273,37 @@ describe("plugin spec opt", function()
end
end)
end)

describe("plugin opts", function()
it("correctly parses opts", function()
---@type {spec:LazySpec, opts:table}[]
local tests = {
{
spec = { { "foo/foo", opts = { a = 1, b = 1 } }, { "foo/foo", opts = { a = 2 } } },
opts = { a = 2, b = 1 },
},
{
spec = { { "foo/foo", config = { a = 1, b = 1 } }, { "foo/foo", opts = { a = 2 } } },
opts = { a = 2, b = 1 },
},
{
spec = { { "foo/foo", opts = { a = 1, b = 1 } }, { "foo/foo", config = { a = 2 } } },
opts = { a = 2, b = 1 },
},
{
spec = { { "foo/foo", config = { a = 1, b = 1 } }, { "foo/foo", config = { a = 2 } } },
opts = { a = 2, b = 1 },
},
{
spec = { { "foo/foo", config = { a = 1, b = 1 } }, { "foo/foo", config = { a = vim.NIL } } },
opts = { b = 1 },
},
}

for _, test in ipairs(tests) do
local spec = Plugin.Spec.new(test.spec)
assert(spec.plugins.foo)
assert.same(test.opts, Loader.opts(spec.plugins.foo))
end
end)
end)

4 comments on commit 7260a2b

@kuntau
Copy link

@kuntau kuntau commented on 7260a2b Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by this. is config in spec will be removed forever or we have to use it with opts? Any particular reason why this decisions is made?

If I understand correctly there's no more config=true and it will be opts={} instead?

@folke
Copy link
Owner Author

@folke folke commented on 7260a2b Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config still exists and can be a function as it used to be. For most use-cases you can just use opts. to run the automatic setup. Confg is for runnig a custom setup function.

I need to clarify the docs a bit.

@kuntau
Copy link

@kuntau kuntau commented on 7260a2b Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so config will only accept function and boolean after this?

@folke
Copy link
Owner Author

@folke folke commented on 7260a2b Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, although setting it to a table also still works.

The reason I wanted to split options from the setup code is to make it easier to extend/override other specs.

Please sign in to comment.