Skip to content

Commit

Permalink
feat(spec): spec merging now properly works with Plugin.enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 4, 2023
1 parent bce0c6e commit 81cb352
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
73 changes: 53 additions & 20 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,12 @@ function Spec:add(plugin, results, is_dep)
plugin._ = {}
plugin._.dep = is_dep

local enabled = plugin.enabled
if enabled == nil then
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
-- FIXME: we should somehow merge when needed
plugin._.kind = "disabled"
self.disabled[plugin.name] = plugin
self.plugins[plugin.name] = nil
plugin.dependencies = plugin.dependencies and self:normalize(plugin.dependencies, {}, true) or 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)
end

function Spec:error(msg)
Expand All @@ -117,6 +102,53 @@ function Spec:warn(msg)
self:log(msg, vim.log.levels.WARN)
end

function Spec:fix_disabled()
---@type table<string,string[]> plugin to parent plugin
local dep_of = {}

---@type string[] dependencies of disabled plugins
local disabled_deps = {}

for _, plugin in pairs(self.plugins) do
local enabled = not (plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled()))
if enabled then
for _, dep in ipairs(plugin.dependencies or {}) do
dep_of[dep] = dep_of[dep] or {}
table.insert(dep_of[dep], plugin.name)
end
else
plugin._.kind = "disabled"
self.plugins[plugin.name] = nil
self.disabled[plugin.name] = plugin
if plugin.dependencies then
vim.list_extend(disabled_deps, plugin.dependencies)
end
end
end

-- check deps of disabled plugins
for _, dep in ipairs(disabled_deps) do
-- only check if the plugin is still enabled and it is a dep
if self.plugins[dep] and self.plugins[dep]._.dep then
-- check if the dep is still used by another plugin
local keep = false
for _, parent in ipairs(dep_of[dep] or {}) do
if self.plugins[parent] then
keep = true
break
end
end
-- disable the dep when no longer needed
if not keep then
local plugin = self.plugins[dep]
plugin._.kind = "disabled"
self.plugins[plugin.name] = nil
self.disabled[plugin.name] = plugin
end
end
end
end

---@param msg string
---@param level number
function Spec:log(msg, level)
Expand Down Expand Up @@ -289,6 +321,7 @@ function M.load()
end
lazy._.loaded = {}
end
Config.spec:fix_disabled()

local existing = Config.plugins
Config.plugins = Config.spec.plugins
Expand Down
1 change: 1 addition & 0 deletions tests/core/plugin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ describe("plugin spec opt", function()
}
for test, ret in pairs(tests) do
local spec = Plugin.Spec.new(test)
spec:fix_disabled()
assert(#spec.notifs == 0)
if ret then
assert(spec.plugins.bar)
Expand Down

0 comments on commit 81cb352

Please sign in to comment.