feat(spec): added support for importing multiple spec modules with `import = "foobar"`

This commit is contained in:
Folke Lemaitre 2023-01-01 21:07:05 +01:00
parent 3974a6cbe3
commit 39b66027a5
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
4 changed files with 57 additions and 40 deletions

View File

@ -144,9 +144,12 @@ M.defaults = {
M.ns = vim.api.nvim_create_namespace("lazy") M.ns = vim.api.nvim_create_namespace("lazy")
---@type string|LazySpec Should be either a string pointing to a module, or a spec ---@type LazySpec
M.spec = nil M.spec = nil
---@type LazySpecLoader
M.parsed = nil
---@type table<string, LazyPlugin> ---@type table<string, LazyPlugin>
M.plugins = {} M.plugins = {}
@ -167,7 +170,7 @@ M.headless = #vim.api.nvim_list_uis() == 0
---@param spec LazySpec ---@param spec LazySpec
---@param opts? LazyConfig ---@param opts? LazyConfig
function M.setup(spec, opts) function M.setup(spec, opts)
M.spec = spec M.spec = type(spec) == "string" and { import = spec } or spec
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {}) M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
M.options.performance.cache = require("lazy.core.cache") M.options.performance.cache = require("lazy.core.cache")
table.insert(M.options.install.colorscheme, "habamax") table.insert(M.options.install.colorscheme, "habamax")

View File

@ -8,6 +8,7 @@ local M = {}
---@class LazySpecLoader ---@class LazySpecLoader
---@field plugins table<string, LazyPlugin> ---@field plugins table<string, LazyPlugin>
---@field modules string[]
---@field errors string[] ---@field errors string[]
---@field opts LazySpecOptions ---@field opts LazySpecOptions
local Spec = {} local Spec = {}
@ -21,6 +22,7 @@ function Spec.new(spec, opts)
local self = setmetatable({}, { __index = Spec }) local self = setmetatable({}, { __index = Spec })
self.opts = opts or {} self.opts = opts or {}
self.plugins = {} self.plugins = {}
self.modules = {}
self.errors = {} self.errors = {}
if spec then if spec then
self:normalize(spec) self:normalize(spec)
@ -88,7 +90,7 @@ function Spec:error(error)
end end
end end
---@param spec LazySpec ---@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, is_dep)
@ -105,22 +107,49 @@ function Spec:normalize(spec, results, is_dep)
for _, s in ipairs(spec) do for _, s in ipairs(spec) do
self:normalize(s, results, is_dep) self:normalize(s, results, is_dep)
end end
elseif spec.enabled == nil or spec.enabled == true or (type(spec.enabled) == "function" and spec.enabled()) then elseif spec.import then
local plugin ---@cast spec LazySpecImport
-- check if we already processed this spec. Can happen when a user uses the same instance of a spec in multiple specs self:import(spec)
-- see https://github.com/folke/lazy.nvim/issues/45 else
if spec._ then ---@cast spec LazyPluginSpec
plugin = spec if spec.enabled == nil or spec.enabled == true or (type(spec.enabled) == "function" and spec.enabled()) then
else local plugin
---@cast spec LazyPlugin -- check if we already processed this spec. Can happen when a user uses the same instance of a spec in multiple specs
plugin = self:add(spec, is_dep) -- see https://github.com/folke/lazy.nvim/issues/45
plugin.dependencies = plugin.dependencies and self:normalize(plugin.dependencies, {}, true) or nil if spec._ then
plugin = spec
else
---@cast spec LazyPlugin
plugin = self:add(spec, is_dep)
plugin.dependencies = plugin.dependencies and self:normalize(plugin.dependencies, {}, true) or nil
end
table.insert(results, plugin.name)
end end
table.insert(results, plugin.name)
end end
return results return results
end end
---@param spec LazySpecImport
function Spec:import(spec)
if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then
return
end
Util.lsmod(spec.import, function(modname)
-- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil
Util.try(function()
self:normalize(Cache.require(modname))
self.modules[#self.modules + 1] = modname
end, {
msg = "Failed to load `" .. modname .. "`",
on_error = function(msg)
self:error(msg)
end,
})
end)
end
---@param old LazyPlugin ---@param old LazyPlugin
---@param new LazyPlugin ---@param new LazyPlugin
---@return LazyPlugin ---@return LazyPlugin
@ -194,35 +223,14 @@ end
---@param opts? LazySpecOptions ---@param opts? LazySpecOptions
function M.spec(opts) function M.spec(opts)
local spec = Spec.new(nil, opts) return Spec.new(vim.deepcopy(Config.spec), opts)
if type(Config.spec) == "string" then
-- spec is a module
local function _load(modname)
-- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil
Util.try(function()
spec:normalize(Cache.require(modname))
end, {
msg = "Failed to load `" .. modname .. "`",
on_error = function(msg)
spec:error(msg)
end,
})
end
Util.lsmod(Config.spec --[[@as string]], _load)
else
-- spec is a spec
spec:normalize(vim.deepcopy(Config.spec))
end
return spec
end end
function M.load() function M.load()
-- load specs -- load specs
Util.track("spec") Util.track("spec")
local spec = M.spec() local spec = M.spec()
Config.parsed = spec
-- add ourselves -- add ourselves
spec:add({ "folke/lazy.nvim" }) spec:add({ "folke/lazy.nvim" })

View File

@ -17,7 +17,7 @@ function M.enable()
if M.timer then if M.timer then
M.timer:stop() M.timer:stop()
end end
if type(Config.spec) == "string" then if #Config.parsed.modules > 0 then
M.timer = vim.loop.new_timer() M.timer = vim.loop.new_timer()
M.root = vim.fn.stdpath("config") .. "/lua" M.root = vim.fn.stdpath("config") .. "/lua"
M.check(true) M.check(true)
@ -55,7 +55,9 @@ function M.check(start)
end end
end end
Util.lsmod(Config.spec --[[@as string]], check) for _, modname in ipairs(Config.parsed.modules) do
Util.lsmod(modname, check)
end
for file in pairs(M.files) do for file in pairs(M.files) do
if not checked[file] then if not checked[file] then

View File

@ -58,4 +58,8 @@
---@class LazyPluginSpec: LazyPluginBase,LazyPluginSpecHandlers,LazyPluginHooks,LazyPluginRef ---@class LazyPluginSpec: LazyPluginBase,LazyPluginSpecHandlers,LazyPluginHooks,LazyPluginRef
---@field dependencies? string|string[]|LazyPluginSpec[] ---@field dependencies? string|string[]|LazyPluginSpec[]
---@alias LazySpec string|string[]|LazyPluginSpec[]|LazyPluginSpec[][] ---@alias LazySpec string|LazyPluginSpec|LazySpecImport|LazySpec[]
---@class LazySpecImport
---@field import string spec module to import
---@field enabled? boolean|(fun():boolean)