feat(spec): allow overriding `Plugin.enabled`

This commit is contained in:
Folke Lemaitre 2023-01-03 10:34:53 +01:00
parent dc9c92a9b3
commit 05aec48968
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 62 additions and 26 deletions

View File

@ -8,6 +8,7 @@ local M = {}
---@class LazySpecLoader ---@class LazySpecLoader
---@field plugins table<string, LazyPlugin> ---@field plugins table<string, LazyPlugin>
---@field disabled table<string, LazyPlugin>
---@field modules string[] ---@field modules string[]
---@field notifs {msg:string, level:number, file?:string}[] ---@field notifs {msg:string, level:number, file?:string}[]
---@field importing? string ---@field importing? string
@ -18,6 +19,7 @@ M.Spec = Spec
function Spec.new(spec) function Spec.new(spec)
local self = setmetatable({}, { __index = Spec }) local self = setmetatable({}, { __index = Spec })
self.plugins = {} self.plugins = {}
self.disabled = {}
self.modules = {} self.modules = {}
self.notifs = {} self.notifs = {}
if spec then if spec then
@ -34,8 +36,15 @@ function Spec.get_name(pkg)
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param results? string[]
---@param is_dep? boolean ---@param is_dep? boolean
function Spec:add(plugin, is_dep) function Spec:add(plugin, results, is_dep)
-- check if we already processed this spec. Can happen when a user uses the same instance of a spec in multiple specs
-- see https://github.com/folke/lazy.nvim/issues/45
if plugin._ then
return results and table.insert(results, plugin.name)
end
if not plugin.url and plugin[1] then if not plugin.url and plugin[1] then
plugin.url = Config.options.git.url_format:format(plugin[1]) plugin.url = Config.options.git.url_format:format(plugin[1])
end end
@ -74,9 +83,25 @@ function Spec:add(plugin, is_dep)
plugin._ = {} plugin._ = {}
plugin._.dep = is_dep plugin._.dep = is_dep
local other = self.plugins[plugin.name] local enabled = plugin.enabled
self.plugins[plugin.name] = other and self:merge(other, plugin) or plugin if enabled == nil then
return self.plugins[plugin.name] enabled = self.disabled[plugin.name] == nil
else
enabled = plugin.enabled == true or (type(plugin.enabled) == "function" and plugin.enabled())
end
if enabled then
plugin.dependencies = plugin.dependencies and self:normalize(plugin.dependencies, {}, true) or nil
self.disabled[plugin.name] = nil
if self.plugins[plugin.name] then
plugin = self:merge(self.plugins[plugin.name], plugin)
end
self.plugins[plugin.name] = plugin
return results and table.insert(results, plugin.name)
else
self.disabled[plugin.name] = plugin
self.plugins[plugin.name] = nil
end
end end
function Spec:error(msg) function Spec:error(msg)
@ -106,13 +131,14 @@ end
---@param results? string[] ---@param results? string[]
---@param is_dep? boolean ---@param is_dep? boolean
function Spec:normalize(spec, results, is_dep) function Spec:normalize(spec, results, is_dep)
results = results or {}
if type(spec) == "string" then if type(spec) == "string" then
if is_dep and not spec:find("/", 1, true) then if is_dep and not spec:find("/", 1, true) then
-- spec is a plugin name -- spec is a plugin name
table.insert(results, spec) if results then
table.insert(results, spec)
end
else else
table.insert(results, self:add({ spec }, is_dep).name) self:add({ spec }, results, is_dep)
end end
elseif #spec > 1 or Util.is_list(spec) then elseif #spec > 1 or Util.is_list(spec) then
---@cast spec LazySpec[] ---@cast spec LazySpec[]
@ -123,20 +149,8 @@ function Spec:normalize(spec, results, is_dep)
---@cast spec LazySpecImport ---@cast spec LazySpecImport
self:import(spec) self:import(spec)
else else
---@cast spec LazyPluginSpec ---@cast spec LazyPlugin
if spec.enabled == nil or spec.enabled == true or (type(spec.enabled) == "function" and spec.enabled()) then self:add(spec, results, is_dep)
local plugin
-- check if we already processed this spec. Can happen when a user uses the same instance of a spec in multiple specs
-- see https://github.com/folke/lazy.nvim/issues/45
if spec._ then
plugin = spec
else
---@cast spec LazyPlugin
spec.dependencies = spec.dependencies and self:normalize(spec.dependencies, {}, true) or nil
plugin = self:add(spec, is_dep)
end
table.insert(results, plugin.name)
end
end end
return results return results
end end
@ -266,12 +280,14 @@ function M.load()
Config.spec:add({ "folke/lazy.nvim" }) Config.spec:add({ "folke/lazy.nvim" })
-- override some lazy props -- override some lazy props
local lazy = Config.spec.plugins["lazy.nvim"] local lazy = Config.spec.plugins["lazy.nvim"]
lazy.lazy = true if lazy then
lazy.dir = Config.me lazy.lazy = true
lazy.config = function() lazy.dir = Config.me
error("lazy config should not be called") lazy.config = function()
error("lazy config should not be called")
end
lazy._.loaded = {}
end end
lazy._.loaded = {}
local existing = Config.plugins local existing = Config.plugins
Config.plugins = Config.spec.plugins Config.plugins = Config.spec.plugins

View File

@ -250,4 +250,24 @@ describe("plugin spec opt", function()
assert(vim.tbl_contains(spec.plugins.bar.event, "mod2")) assert(vim.tbl_contains(spec.plugins.bar.event, "mod2"))
end end
end) end)
it("handles disabled", function()
local tests = {
[{ { "foo/bar" }, { "foo/bar", enabled = false } }] = false,
[{ { "foo/bar", enabled = false }, { "foo/bar" } }] = false,
[{ { "foo/bar", enabled = false }, { "foo/bar", enabled = true } }] = true,
[{ { "foo/bar" }, { "foo/bar", enabled = true } }] = true,
}
for test, ret in pairs(tests) do
local spec = Plugin.Spec.new(test)
assert(#spec.notifs == 0)
if ret then
assert(spec.plugins.bar)
assert(not spec.disabled.bar)
else
assert(not spec.plugins.bar)
assert(spec.disabled.bar)
end
end
end)
end) end)