feat(spec): added `import` to import other plugin modules

This commit is contained in:
Folke Lemaitre 2023-01-02 09:44:09 +01:00
parent 313015fdb4
commit 919b7f5de3
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
7 changed files with 74 additions and 46 deletions

View File

@ -331,7 +331,7 @@ function M.find(modname, opts)
---@type LazyCoreConfig ---@type LazyCoreConfig
local Config = package.loaded["lazy.core.config"] local Config = package.loaded["lazy.core.config"]
if Config then if Config then
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.spec.plugins) do
if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then
updated = M._index(plugin.dir) or updated updated = M._index(plugin.dir) or updated
end end

View File

@ -11,6 +11,8 @@ M.defaults = {
version = nil, version = nil,
-- version = "*", -- enable this to try installing the latest stable versions of plugins -- version = "*", -- enable this to try installing the latest stable versions of plugins
}, },
-- leave nil when passing the spec as the first argument to setup()
spec = nil, ---@type LazySpec
lockfile = vim.fn.stdpath("config") .. "/lazy-lock.json", -- lockfile generated after running update. lockfile = vim.fn.stdpath("config") .. "/lazy-lock.json", -- lockfile generated after running update.
concurrency = nil, ---@type number limit the maximum amount of concurrent tasks concurrency = nil, ---@type number limit the maximum amount of concurrent tasks
git = { git = {
@ -38,20 +40,21 @@ M.defaults = {
-- The border to use for the UI window. Accepts same border values as |nvim_open_win()|. -- The border to use for the UI window. Accepts same border values as |nvim_open_win()|.
border = "none", border = "none",
icons = { icons = {
loaded = "",
not_loaded = "",
cmd = "", cmd = "",
config = "", config = "",
event = "", event = "",
ft = "", ft = "",
init = "", init = "",
import = "",
keys = "", keys = "",
lazy = "",
loaded = "",
not_loaded = "",
plugin = "", plugin = "",
runtime = "", runtime = "",
source = "", source = "",
start = "", start = "",
task = "", task = "",
lazy = "",
list = { list = {
"", "",
"", "",
@ -144,11 +147,8 @@ M.defaults = {
M.ns = vim.api.nvim_create_namespace("lazy") M.ns = vim.api.nvim_create_namespace("lazy")
---@type LazySpec
M.spec = nil
---@type LazySpecLoader ---@type LazySpecLoader
M.parsed = nil M.spec = nil
---@type table<string, LazyPlugin> ---@type table<string, LazyPlugin>
M.plugins = {} M.plugins = {}
@ -167,12 +167,14 @@ M.mapleader = nil
M.headless = #vim.api.nvim_list_uis() == 0 M.headless = #vim.api.nvim_list_uis() == 0
---@param spec LazySpec
---@param opts? LazyConfig ---@param opts? LazyConfig
function M.setup(spec, opts) function M.setup(opts)
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")
if type(M.options.spec) == "string" then
M.options.spec = { import = M.options.spec }
end
M.options.performance.cache = require("lazy.core.cache").config
table.insert(M.options.install.colorscheme, "habamax") table.insert(M.options.install.colorscheme, "habamax")
M.options.root = Util.norm(M.options.root) M.options.root = Util.norm(M.options.root)

View File

@ -9,21 +9,17 @@ local M = {}
---@class LazySpecLoader ---@class LazySpecLoader
---@field plugins table<string, LazyPlugin> ---@field plugins table<string, LazyPlugin>
---@field modules string[] ---@field modules string[]
---@field errors string[] ---@field notifs {msg:string, level:number, file?:string}[]
---@field opts LazySpecOptions ---@field importing? string
local Spec = {} local Spec = {}
M.Spec = Spec M.Spec = Spec
---@alias LazySpecOptions {show_errors: boolean}
---@param spec? LazySpec ---@param spec? LazySpec
---@param opts? LazySpecOptions function Spec.new(spec)
function Spec.new(spec, opts)
local self = setmetatable({}, { __index = Spec }) local self = setmetatable({}, { __index = Spec })
self.opts = opts or {}
self.plugins = {} self.plugins = {}
self.modules = {} self.modules = {}
self.errors = {} self.notifs = {}
if spec then if spec then
self:normalize(spec) self:normalize(spec)
end end
@ -83,11 +79,19 @@ function Spec:add(plugin, is_dep)
return self.plugins[plugin.name] return self.plugins[plugin.name]
end end
function Spec:error(error) function Spec:error(msg)
self.errors[#self.errors + 1] = error self:notify(msg, vim.log.levels.ERROR)
if self.opts.show_errors ~= false then end
Util.error(error)
end function Spec:warn(msg)
self:notify(msg, vim.log.levels.WARN)
end
---@param msg string
---@param level number
function Spec:notify(msg, level)
self.notifs[#self.notifs + 1] = { msg = msg, level = level, file = self.importing }
Util.notify(msg, level)
end end
---@param spec LazySpec|LazySpecImport ---@param spec LazySpec|LazySpecImport
@ -134,20 +138,34 @@ function Spec:import(spec)
if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then
return return
end end
Cache.indexed_unloaded = false
local imported = 0
Util.lsmod(spec.import, function(modname) Util.lsmod(spec.import, function(modname)
imported = imported + 1
Util.track({ import = modname })
self.importing = modname
-- unload the module so we get a clean slate -- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil package.loaded[modname] = nil
Util.try(function() Util.try(function()
self:normalize(Cache.require(modname)) self:normalize(Cache.require(modname))
self.modules[#self.modules + 1] = modname self.modules[#self.modules + 1] = modname
self.importing = nil
Util.track()
end, { end, {
msg = "Failed to load `" .. modname .. "`", msg = "Failed to load `" .. modname .. "`",
on_error = function(msg) on_error = function(msg)
self:error(msg) self:error(msg)
self.importing = nil
Util.track()
end, end,
}) })
end) end)
if imported == 0 then
self:error("No specs found for module " .. spec.import)
end
end end
---@param old LazyPlugin ---@param old LazyPlugin
@ -230,21 +248,16 @@ function M.update_state()
end end
end end
---@param opts? LazySpecOptions
function M.spec(opts)
return Spec.new(vim.deepcopy(Config.spec), opts)
end
function M.load() function M.load()
-- load specs -- load specs
Util.track("spec") Util.track("spec")
local spec = M.spec() Config.spec = Spec.new()
Config.parsed = spec Config.spec:normalize(vim.deepcopy(Config.options.spec))
-- add ourselves -- add ourselves
spec:add({ "folke/lazy.nvim" }) Config.spec:add({ "folke/lazy.nvim" })
-- override some lazy props -- override some lazy props
local lazy = spec.plugins["lazy.nvim"] local lazy = Config.spec.plugins["lazy.nvim"]
lazy.lazy = true lazy.lazy = true
lazy.dir = Config.me lazy.dir = Config.me
lazy.config = function() lazy.config = function()
@ -253,7 +266,7 @@ function M.load()
lazy._.loaded = {} lazy._.loaded = {}
local existing = Config.plugins local existing = Config.plugins
Config.plugins = spec.plugins Config.plugins = Config.spec.plugins
-- copy state. This wont do anything during startup -- copy state. This wont do anything during startup
for name, plugin in pairs(existing) do for name, plugin in pairs(existing) do
if Config.plugins[name] then if Config.plugins[name] then

View File

@ -48,7 +48,7 @@ function M.check()
"cond", "cond",
"_", "_",
} }
local spec = Config.parsed local spec = Config.spec
for _, plugin in pairs(spec.plugins) do for _, plugin in pairs(spec.plugins) do
for key in pairs(plugin) do for key in pairs(plugin) do
if not vim.tbl_contains(valid, key) then if not vim.tbl_contains(valid, key) then
@ -58,12 +58,16 @@ function M.check()
end end
end end
end end
if #spec.errors > 0 then if #spec.notifs > 0 then
vim.health.report_error("Errors were reported when loading your specs:") vim.health.report_error("Issues were reported when loading your specs:")
for _, error in ipairs(spec.errors) do for _, notif in ipairs(spec.notifs) do
local lines = vim.split(error, "\n") local lines = vim.split(notif.msg, "\n")
for _, line in ipairs(lines) do for _, line in ipairs(lines) do
vim.health.report_error(line) if notif.level == vim.log.levels.ERROR then
vim.health.report_error(line)
else
vim.health.report_warn(line)
end
end end
end end
end end

View File

@ -2,9 +2,17 @@
local M = {} local M = {}
M._start = 0 M._start = 0
---@param spec LazySpec Should be a module name to load, or a plugin spec ---@overload fun(opts: LazyConfig)
---@param opts? LazyConfig ---@overload fun(spec:LazySpec, opts: LazyConfig)
function M.setup(spec, opts) function M.setup(spec, opts)
if type(spec) == "table" and spec.spec then
---@cast spec LazyConfig
opts = spec
else
opts = opts or {}
opts.spec = spec
end
M._start = M._start == 0 and vim.loop.hrtime() or M._start M._start = M._start == 0 and vim.loop.hrtime() or M._start
if vim.g.lazy_did_setup then if vim.g.lazy_did_setup then
return vim.notify( return vim.notify(
@ -38,7 +46,7 @@ function M.setup(spec, opts)
-- load config -- load config
Util.track("config") Util.track("config")
Config.setup(spec, opts) Config.setup(opts)
Util.track() Util.track()
-- load the plugins -- load the plugins

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 #Config.parsed.modules > 0 then if #Config.spec.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,7 @@ function M.check(start)
end end
end end
for _, modname in ipairs(Config.parsed.modules) do for _, modname in ipairs(Config.spec.modules) do
Util.lsmod(modname, check) Util.lsmod(modname, check)
end end

View File

@ -23,6 +23,7 @@ M.colors = {
ReasonSource = "Character", ReasonSource = "Character",
ReasonFt = "Character", ReasonFt = "Character",
ReasonCmd = "Operator", ReasonCmd = "Operator",
ReasonImport = "Identifier",
Button = "CursorLine", Button = "CursorLine",
ButtonActive = "Visual", ButtonActive = "Visual",
TaskOutput = "MsgArea", -- task output TaskOutput = "MsgArea", -- task output