Skip to content

Commit

Permalink
feat(loader): incrementally install missing plugins and rebuild spec,…
Browse files Browse the repository at this point in the history
… so imported specs from plugins work as expected
  • Loading branch information
folke committed Jan 2, 2023
1 parent 919b7f5 commit 2d06faa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
44 changes: 34 additions & 10 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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()
Expand Down
13 changes: 9 additions & 4 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions lua/lazy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 2d06faa

Please sign in to comment.