From 2d06faa941998f76f0348b7b69c5ecdcb5f3db2a Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 2 Jan 2023 10:08:45 +0100 Subject: [PATCH] feat(loader): incrementally install missing plugins and rebuild spec, so imported specs from plugins work as expected --- lua/lazy/core/loader.lua | 44 +++++++++++++++++++++++++++++++--------- lua/lazy/core/plugin.lua | 13 ++++++++---- lua/lazy/init.lua | 3 --- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index 5a666a9..f9f2620 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -2,6 +2,7 @@ local Util = require("lazy.core.util") local Config = require("lazy.core.config") local Handler = require("lazy.core.handler") local Cache = require("lazy.core.cache") +local Plugin = require("lazy.core.plugin") local M = {} @@ -26,29 +27,52 @@ function M.setup() end, }) + -- load the plugins + Plugin.load() + -- install missing plugins if Config.options.install.missing then Util.track("install") - for _, plugin in pairs(Config.plugins) do - if not plugin._.installed then - for _, colorscheme in ipairs(Config.options.install.colorscheme) do - if pcall(vim.cmd.colorscheme, colorscheme) then - break - end - end - require("lazy.manage").install({ wait = true, lockfile = true }) - break - end + while M.install_missing() do end Util.track() end + -- report any warnings & errors + Config.spec:report() + -- setup handlers Util.track("handlers") Handler.setup() Util.track() end +-- this will incrementally install missing plugins +-- multiple rounds can happen when importing a spec from a missing plugin +function M.install_missing() + for _, plugin in pairs(Config.plugins) do + if not plugin._.installed then + for _, colorscheme in ipairs(Config.options.install.colorscheme) do + if pcall(vim.cmd.colorscheme, colorscheme) then + break + end + end + require("lazy.manage").install({ wait = true, lockfile = true }) + -- remove and installed plugins from indexed, so cache will index again + for _, p in pairs(Config.plugins) do + if p._.installed then + Cache.indexed[p.dir] = nil + end + end + -- clear plugins. no need to merge in this stage + Config.plugins = {} + -- reload plugins + Plugin.load() + return true + end + end +end + -- Startup sequence -- 1. load any start plugins and do init function M.startup() diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index 1a2c153..f6bfaf2 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -80,18 +80,23 @@ function Spec:add(plugin, is_dep) end function Spec:error(msg) - self:notify(msg, vim.log.levels.ERROR) + self:log(msg, vim.log.levels.ERROR) end function Spec:warn(msg) - self:notify(msg, vim.log.levels.WARN) + self:log(msg, vim.log.levels.WARN) end ---@param msg string ---@param level number -function Spec:notify(msg, level) +function Spec:log(msg, level) self.notifs[#self.notifs + 1] = { msg = msg, level = level, file = self.importing } - Util.notify(msg, level) +end + +function Spec:report() + for _, notif in ipairs(self.notifs) do + Util.notify(notif.msg, notif.level) + end end ---@param spec LazySpec|LazySpecImport diff --git a/lua/lazy/init.lua b/lua/lazy/init.lua index 2214761..318f63c 100644 --- a/lua/lazy/init.lua +++ b/lua/lazy/init.lua @@ -49,9 +49,6 @@ function M.setup(spec, opts) Config.setup(opts) Util.track() - -- load the plugins - Plugin.load() - -- setup loader and handlers Loader.setup()