feat(plugin): dont include plugin spec fragments for disabled or optional plugins

This commit is contained in:
Folke Lemaitre 2023-09-29 10:05:43 +02:00
parent 6b55e4695a
commit aa14c5e7b8
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 111 additions and 65 deletions

View File

@ -8,21 +8,27 @@ M.loading = false
---@class LazySpecLoader ---@class LazySpecLoader
---@field plugins table<string, LazyPlugin> ---@field plugins table<string, LazyPlugin>
---@field fragments table<number, LazyPlugin>
---@field disabled table<string, LazyPlugin> ---@field disabled table<string, LazyPlugin>
---@field dirty table<string, true>
---@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
---@field optional? boolean ---@field optional? boolean
local Spec = {} local Spec = {}
M.Spec = Spec M.Spec = Spec
M.last_fid = 0
M.fid_stack = {} ---@type number[]
---@param spec? LazySpec ---@param spec? LazySpec
---@param opts? {optional?:boolean} ---@param opts? {optional?:boolean}
function Spec.new(spec, opts) function Spec.new(spec, opts)
local self = setmetatable({}, { __index = Spec }) local self = setmetatable({}, { __index = Spec })
self.plugins = {} self.plugins = {}
self.fragments = {}
self.disabled = {} self.disabled = {}
self.modules = {} self.modules = {}
self.dirty = {}
self.notifs = {} self.notifs = {}
self.optional = opts and opts.optional self.optional = opts and opts.optional
if spec then if spec then
@ -56,8 +62,7 @@ end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param results? string[] ---@param results? string[]
---@param is_dep? boolean function Spec:add(plugin, results)
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 -- 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 -- see https://github.com/folke/lazy.nvim/issues/45
if rawget(plugin, "_") then if rawget(plugin, "_") then
@ -124,10 +129,28 @@ function Spec:add(plugin, results, is_dep)
plugin.config = nil plugin.config = nil
end end
plugin._ = {} local fpid = M.fid_stack[#M.fid_stack]
plugin._.dep = is_dep
M.last_fid = M.last_fid + 1
plugin._ = {
fid = M.last_fid,
fpid = fpid,
dep = fpid ~= nil,
}
self.fragments[plugin._.fid] = plugin
if fpid then
local parent = self.fragments[fpid]
parent._.fdeps = parent._.fdeps or {}
table.insert(parent._.fdeps, plugin._.fid)
end
if plugin.dependencies then
table.insert(M.fid_stack, plugin._.fid)
plugin.dependencies = self:normalize(plugin.dependencies, {})
table.remove(M.fid_stack)
end
plugin.dependencies = plugin.dependencies and self:normalize(plugin.dependencies, {}, true) or nil
if self.plugins[plugin.name] then if self.plugins[plugin.name] then
plugin = self:merge(self.plugins[plugin.name], plugin) plugin = self:merge(self.plugins[plugin.name], plugin)
end end
@ -146,27 +169,71 @@ function Spec:warn(msg)
self:log(msg, vim.log.levels.WARN) self:log(msg, vim.log.levels.WARN)
end end
---@param gathered_deps string[] --- Rebuilds a plugin spec excluding any removed fragments
---@param dep_of table<string,string[]> ---@param name string
---@param on_disable fun(string):nil function Spec:rebuild(name)
function Spec:fix_dependencies(gathered_deps, dep_of, on_disable) local plugin = self.plugins[name]
local function should_disable(dep_name) if not plugin then
for _, parent in ipairs(dep_of[dep_name] or {}) do return
if self.plugins[parent] then
return false
end
end
return true
end end
for _, dep_name in ipairs(gathered_deps) do local fragments = {} ---@type LazyPlugin[]
-- only check if the plugin is still enabled and it is a dep
if self.plugins[dep_name] and self.plugins[dep_name]._.dep then repeat
-- check if the dep is still used by another plugin if self.fragments[plugin._.fid] then
if should_disable(dep_name) then plugin._.dep = plugin._.fpid ~= nil
-- disable the dep when no longer needed if plugin._.fdeps then
on_disable(dep_name) plugin.dependencies = {}
for _, cid in ipairs(plugin._.fdeps) do
if self.fragments[cid] then
table.insert(plugin.dependencies, self.fragments[cid].name)
end
end
end end
setmetatable(plugin, nil)
table.insert(fragments, 1, plugin)
end
plugin = plugin._.super
until not plugin
if #fragments == 0 then
self.plugins[name] = nil
return
end
plugin = fragments[1]
for i = 2, #fragments do
plugin = self:merge(plugin, fragments[i])
end
self.plugins[name] = plugin
end
--- Recursively removes all fragments from a plugin spec or a given fragment
---@param id string|number Plugin name or fragment id
---@param opts {self: boolean}
function Spec:remove_fragments(id, opts)
local fids = {} ---@type number[]
if type(id) == "number" then
fids[1] = id
else
local plugin = self.plugins[id]
repeat
fids[#fids + 1] = plugin._.fid
plugin = plugin._.super
until not plugin
end
for _, fid in ipairs(fids) do
local fragment = self.fragments[fid]
if fragment then
for _, cid in ipairs(fragment._.fdeps or {}) do
self:remove_fragments(cid, { self = true })
end
if opts.self then
self.fragments[fid] = nil
end
self.dirty[fragment.name] = true
end end
end end
end end
@ -184,9 +251,7 @@ function Spec:fix_cond()
end end
end end
---@return string[]
function Spec:fix_optional() function Spec:fix_optional()
local all_optional_deps = {}
if not self.optional then if not self.optional then
---@param plugin LazyPlugin ---@param plugin LazyPlugin
local function all_optional(plugin) local function all_optional(plugin)
@ -196,14 +261,12 @@ function Spec:fix_optional()
-- handle optional plugins -- handle optional plugins
for _, plugin in pairs(self.plugins) do for _, plugin in pairs(self.plugins) do
if plugin.optional and all_optional(plugin) then if plugin.optional and all_optional(plugin) then
-- remove all optional fragments
self:remove_fragments(plugin.name, { self = true })
self.plugins[plugin.name] = nil self.plugins[plugin.name] = nil
if plugin.dependencies then
vim.list_extend(all_optional_deps, plugin.dependencies)
end
end end
end end
end end
return all_optional_deps
end end
function Spec:fix_disabled() function Spec:fix_disabled()
@ -214,44 +277,24 @@ function Spec:fix_disabled()
end end
end end
---@type table<string,string[]> plugin to parent plugin self:fix_optional()
local dep_of = {}
---@type string[] dependencies of disabled plugins
local disabled_deps = {}
---@type string[] dependencies of plugins that are completely optional
local all_optional_deps = self:fix_optional()
self:fix_cond() self:fix_cond()
for _, plugin in pairs(self.plugins) do for _, plugin in pairs(self.plugins) do
local enabled = not (plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled())) local disabled = plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled())
if enabled then if disabled 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" plugin._.kind = "disabled"
-- remove all child fragments
self:remove_fragments(plugin.name, { self = false })
self.plugins[plugin.name] = nil self.plugins[plugin.name] = nil
self.disabled[plugin.name] = plugin self.disabled[plugin.name] = plugin
if plugin.dependencies then
vim.list_extend(disabled_deps, plugin.dependencies)
end
end end
end end
-- fix deps of plugins that are completely optional -- rebuild any plugin specs that were modified
self:fix_dependencies(all_optional_deps, dep_of, function(dep_name) for name, _ in pairs(self.dirty) do
self.plugins[dep_name] = nil self:rebuild(name)
end) end
-- fix deps of disabled plugins
self:fix_dependencies(disabled_deps, dep_of, function(dep_name)
local plugin = self.plugins[dep_name]
plugin._.kind = "disabled"
self.plugins[plugin.name] = nil
self.disabled[plugin.name] = plugin
end)
end end
---@param msg string ---@param msg string
@ -272,24 +315,24 @@ end
---@param spec LazySpec|LazySpecImport ---@param spec LazySpec|LazySpecImport
---@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)
if type(spec) == "string" then if type(spec) == "string" then
if is_dep and not spec:find("/", 1, true) then if not spec:find("/", 1, true) then
-- spec is a plugin name -- spec is a plugin name
if results then if results then
table.insert(results, spec) table.insert(results, spec)
end end
else else
self:add({ spec }, results, is_dep) self:add({ spec }, results)
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[]
for _, s in ipairs(spec) do for _, s in ipairs(spec) do
self:normalize(s, results, is_dep) self:normalize(s, results)
end end
elseif spec[1] or spec.dir or spec.url then elseif spec[1] or spec.dir or spec.url then
---@cast spec LazyPlugin ---@cast spec LazyPlugin
local plugin = self:add(spec, results, is_dep) local plugin = self:add(spec, results)
---@diagnostic disable-next-line: cast-type-mismatch ---@diagnostic disable-next-line: cast-type-mismatch
---@cast plugin LazySpecImport ---@cast plugin LazySpecImport
if plugin and plugin.import then if plugin and plugin.import then

View File

@ -2,12 +2,15 @@
---@alias LazyPluginKind "normal"|"clean"|"disabled" ---@alias LazyPluginKind "normal"|"clean"|"disabled"
---@class LazyPluginState ---@class LazyPluginState
---@field fid number id of the plugin spec fragment
---@field fpid? number parent id of the plugin spec fragment
---@field fdeps? number[] children ids of the fragment
---@field loaded? {[string]:string}|{time:number} ---@field loaded? {[string]:string}|{time:number}
---@field installed boolean ---@field installed? boolean
---@field tasks? LazyTask[] ---@field tasks? LazyTask[]
---@field dirty? boolean ---@field dirty? boolean
---@field updated? {from:string, to:string} ---@field updated? {from:string, to:string}
---@field is_local boolean ---@field is_local? boolean
---@field updates? {from:GitInfo, to:GitInfo} ---@field updates? {from:GitInfo, to:GitInfo}
---@field cloned? boolean ---@field cloned? boolean
---@field kind? LazyPluginKind ---@field kind? LazyPluginKind