From 919b7f5de3ba78d2030be617b64ada17bddd47da Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 2 Jan 2023 09:44:09 +0100 Subject: [PATCH] feat(spec): added `import` to import other plugin modules --- lua/lazy/core/cache.lua | 2 +- lua/lazy/core/config.lua | 24 ++++++++------- lua/lazy/core/plugin.lua | 59 ++++++++++++++++++++++-------------- lua/lazy/health.lua | 16 ++++++---- lua/lazy/init.lua | 14 +++++++-- lua/lazy/manage/reloader.lua | 4 +-- lua/lazy/view/colors.lua | 1 + 7 files changed, 74 insertions(+), 46 deletions(-) diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index 474779c2..35e38526 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -331,7 +331,7 @@ function M.find(modname, opts) ---@type LazyCoreConfig local Config = package.loaded["lazy.core.config"] 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 updated = M._index(plugin.dir) or updated end diff --git a/lua/lazy/core/config.lua b/lua/lazy/core/config.lua index 68daf7dd..d2b3146c 100644 --- a/lua/lazy/core/config.lua +++ b/lua/lazy/core/config.lua @@ -11,6 +11,8 @@ M.defaults = { version = nil, -- 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. concurrency = nil, ---@type number limit the maximum amount of concurrent tasks git = { @@ -38,20 +40,21 @@ M.defaults = { -- The border to use for the UI window. Accepts same border values as |nvim_open_win()|. border = "none", icons = { - loaded = "●", - not_loaded = "○", cmd = " ", config = "", event = "", ft = " ", init = " ", + import = " ", keys = " ", + lazy = "鈴 ", + loaded = "●", + not_loaded = "○", plugin = " ", runtime = " ", source = " ", start = "", task = "✔ ", - lazy = "鈴 ", list = { "●", "➜", @@ -144,11 +147,8 @@ M.defaults = { M.ns = vim.api.nvim_create_namespace("lazy") ----@type LazySpec -M.spec = nil - ---@type LazySpecLoader -M.parsed = nil +M.spec = nil ---@type table M.plugins = {} @@ -167,12 +167,14 @@ M.mapleader = nil M.headless = #vim.api.nvim_list_uis() == 0 ----@param spec LazySpec ---@param opts? LazyConfig -function M.setup(spec, opts) - M.spec = type(spec) == "string" and { import = spec } or spec +function M.setup(opts) 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") M.options.root = Util.norm(M.options.root) diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index 3c99b36a..1a2c153d 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -9,21 +9,17 @@ local M = {} ---@class LazySpecLoader ---@field plugins table ---@field modules string[] ----@field errors string[] ----@field opts LazySpecOptions +---@field notifs {msg:string, level:number, file?:string}[] +---@field importing? string local Spec = {} M.Spec = Spec ----@alias LazySpecOptions {show_errors: boolean} - ---@param spec? LazySpec ----@param opts? LazySpecOptions -function Spec.new(spec, opts) +function Spec.new(spec) local self = setmetatable({}, { __index = Spec }) - self.opts = opts or {} self.plugins = {} self.modules = {} - self.errors = {} + self.notifs = {} if spec then self:normalize(spec) end @@ -83,11 +79,19 @@ function Spec:add(plugin, is_dep) return self.plugins[plugin.name] end -function Spec:error(error) - self.errors[#self.errors + 1] = error - if self.opts.show_errors ~= false then - Util.error(error) - end +function Spec:error(msg) + self:notify(msg, vim.log.levels.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 ---@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 return end + + Cache.indexed_unloaded = false + + local imported = 0 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 ---@diagnostic disable-next-line: no-unknown package.loaded[modname] = nil Util.try(function() self:normalize(Cache.require(modname)) self.modules[#self.modules + 1] = modname + self.importing = nil + Util.track() end, { msg = "Failed to load `" .. modname .. "`", on_error = function(msg) self:error(msg) + self.importing = nil + Util.track() end, }) end) + if imported == 0 then + self:error("No specs found for module " .. spec.import) + end end ---@param old LazyPlugin @@ -230,21 +248,16 @@ function M.update_state() end end ----@param opts? LazySpecOptions -function M.spec(opts) - return Spec.new(vim.deepcopy(Config.spec), opts) -end - function M.load() -- load specs Util.track("spec") - local spec = M.spec() - Config.parsed = spec + Config.spec = Spec.new() + Config.spec:normalize(vim.deepcopy(Config.options.spec)) -- add ourselves - spec:add({ "folke/lazy.nvim" }) + Config.spec:add({ "folke/lazy.nvim" }) -- override some lazy props - local lazy = spec.plugins["lazy.nvim"] + local lazy = Config.spec.plugins["lazy.nvim"] lazy.lazy = true lazy.dir = Config.me lazy.config = function() @@ -253,7 +266,7 @@ function M.load() lazy._.loaded = {} local existing = Config.plugins - Config.plugins = spec.plugins + Config.plugins = Config.spec.plugins -- copy state. This wont do anything during startup for name, plugin in pairs(existing) do if Config.plugins[name] then diff --git a/lua/lazy/health.lua b/lua/lazy/health.lua index e218603b..a498dcf3 100644 --- a/lua/lazy/health.lua +++ b/lua/lazy/health.lua @@ -48,7 +48,7 @@ function M.check() "cond", "_", } - local spec = Config.parsed + local spec = Config.spec for _, plugin in pairs(spec.plugins) do for key in pairs(plugin) do if not vim.tbl_contains(valid, key) then @@ -58,12 +58,16 @@ function M.check() end end end - if #spec.errors > 0 then - vim.health.report_error("Errors were reported when loading your specs:") - for _, error in ipairs(spec.errors) do - local lines = vim.split(error, "\n") + if #spec.notifs > 0 then + vim.health.report_error("Issues were reported when loading your specs:") + for _, notif in ipairs(spec.notifs) do + local lines = vim.split(notif.msg, "\n") 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 diff --git a/lua/lazy/init.lua b/lua/lazy/init.lua index b08b5b58..2214761b 100644 --- a/lua/lazy/init.lua +++ b/lua/lazy/init.lua @@ -2,9 +2,17 @@ local M = {} M._start = 0 ----@param spec LazySpec Should be a module name to load, or a plugin spec ----@param opts? LazyConfig +---@overload fun(opts: LazyConfig) +---@overload fun(spec:LazySpec, opts: LazyConfig) 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 if vim.g.lazy_did_setup then return vim.notify( @@ -38,7 +46,7 @@ function M.setup(spec, opts) -- load config Util.track("config") - Config.setup(spec, opts) + Config.setup(opts) Util.track() -- load the plugins diff --git a/lua/lazy/manage/reloader.lua b/lua/lazy/manage/reloader.lua index a0d42659..d3387753 100644 --- a/lua/lazy/manage/reloader.lua +++ b/lua/lazy/manage/reloader.lua @@ -17,7 +17,7 @@ function M.enable() if M.timer then M.timer:stop() end - if #Config.parsed.modules > 0 then + if #Config.spec.modules > 0 then M.timer = vim.loop.new_timer() M.root = vim.fn.stdpath("config") .. "/lua" M.check(true) @@ -55,7 +55,7 @@ function M.check(start) end end - for _, modname in ipairs(Config.parsed.modules) do + for _, modname in ipairs(Config.spec.modules) do Util.lsmod(modname, check) end diff --git a/lua/lazy/view/colors.lua b/lua/lazy/view/colors.lua index 0ec8b2b8..550ed09e 100644 --- a/lua/lazy/view/colors.lua +++ b/lua/lazy/view/colors.lua @@ -23,6 +23,7 @@ M.colors = { ReasonSource = "Character", ReasonFt = "Character", ReasonCmd = "Operator", + ReasonImport = "Identifier", Button = "CursorLine", ButtonActive = "Visual", TaskOutput = "MsgArea", -- task output