fix: recalculate loaders on config file change

This commit is contained in:
Folke Lemaitre 2022-11-26 19:33:38 +01:00
parent 38e2711cdb
commit 870d8924f7
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 32 additions and 48 deletions

View File

@ -18,38 +18,31 @@ M.loaders = nil
---@type LazyPlugin[] ---@type LazyPlugin[]
M.loading = {} M.loading = {}
---@param plugin LazyPlugin function M.get_loaders()
function M.add(plugin) ---@type table<LoaderType, table<string, string[]>>|{init: string[]}
local loaders = { init = {} }
for _, lt in ipairs(M.types) do
loaders[lt] = {}
end
for _, plugin in pairs(Config.plugins) do
if plugin.init or (plugin.opt == false) then if plugin.init or (plugin.opt == false) then
table.insert(M.loaders.init, plugin.name) table.insert(loaders.init, plugin.name)
end end
for _, lt in ipairs(M.types) do
for _, loader_type in ipairs(M.types) do if plugin[lt] then
---@type (string|string[])? ---@diagnostic disable-next-line: no-unknown
local loaders = plugin[loader_type] for _, loader in ipairs(type(plugin[lt]) == "table" and plugin[lt] or { plugin[lt] }) do
if plugin[loader_type] then loaders[lt][loader] = loaders[lt][loader] or {}
loaders = type(loaders) == "table" and loaders or { loaders } table.insert(loaders[lt][loader], plugin.name)
---@cast loaders string[]
for _, loader in ipairs(loaders) do
if not M.loaders[loader_type][loader] then
M.loaders[loader_type][loader] = {}
end
table.insert(M.loaders[loader_type][loader], plugin.name)
end end
end end
end end
end end
return loaders
end
function M.setup() function M.setup()
if not M.loaders then M.loaders = M.loaders or M.get_loaders()
M.loaders = { init = {} }
for _, type in ipairs(M.types) do
M.loaders[type] = {}
end
for _, plugin in pairs(Config.plugins) do
M.add(plugin)
end
end
local group = vim.api.nvim_create_augroup("lazy_loader", { local group = vim.api.nvim_create_augroup("lazy_loader", {
clear = true, clear = true,
@ -167,53 +160,42 @@ end
---@param modname string ---@param modname string
function M.module(modname) function M.module(modname)
local idx = modname:find(".", 1, true) or #modname + 1 local idx = modname:find(".", 1, true) or #modname + 1
while idx do while idx do
local name = modname:sub(1, idx - 1) local name = modname:sub(1, idx - 1)
local plugins = M.loaders.module[name] local plugins = M.loaders.module[name]
if plugins then if plugins then
M.loaders.module[name] = nil
local reason = { require = modname } local reason = { require = modname }
if #M.loading == 0 then if #M.loading == 0 then
local f = 3 local f = 3
while not reason.source do while not reason.source do
local info = debug.getinfo(f, "S") local info = debug.getinfo(f, "S")
f = f + 1
if not info then if not info then
break break
end end
if info.what ~= "C" then if info.what ~= "C" then
reason.source = info.source:sub(2) reason.source = info.source:sub(2)
end end
f = f + 1
end end
end end
M.load(plugins, reason) M.load(plugins, reason)
end end
idx = modname:find(".", idx + 1, true) idx = modname:find(".", idx + 1, true)
end end
---@diagnostic disable-next-line: no-unknown
local mod = package.loaded[modname]
if type(mod) == "table" then
return function()
return mod
end
end
end end
---@param plugins string|LazyPlugin|string[]|LazyPlugin[] ---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
---@param reason {[string]:string} ---@param reason {[string]:string}
---@param opts? {load_start: boolean} ---@param opts? {load_start: boolean}
function M.load(plugins, reason, opts) function M.load(plugins, reason, opts)
if type(plugins) == "string" or plugins.name then ---@diagnostic disable-next-line: cast-local-type
---@diagnostic disable-next-line: assign-type-mismatch plugins = type(plugins) == "string" or plugins.name and { plugins } or plugins
plugins = { plugins }
end
---@cast plugins (string|LazyPlugin)[] ---@cast plugins (string|LazyPlugin)[]
for _, plugin in ipairs(plugins) do for _, plugin in ipairs(plugins) do
if type(plugin) == "string" then plugin = type(plugin) == "string" and Config.plugins[plugin] or plugin
plugin = Config.plugins[plugin] ---@cast plugin LazyPlugin
end
if not plugin.loaded then if not plugin.loaded then
---@diagnostic disable-next-line: assign-type-mismatch ---@diagnostic disable-next-line: assign-type-mismatch

View File

@ -9,6 +9,8 @@ local M = {}
local skip = { installed = true, loaded = true, tasks = true, dirty = true, dir = true } local skip = { installed = true, loaded = true, tasks = true, dirty = true, dir = true }
local funs = { config = true, init = true, run = true } local funs = { config = true, init = true, run = true }
M.dirty = false
---@class LazyPlugin ---@class LazyPlugin
---@field [1] string ---@field [1] string
---@field name string display name and name used for plugin config files ---@field name string display name and name used for plugin config files
@ -46,7 +48,9 @@ function Spec.load(modname, modpath)
self.plugins = {} self.plugins = {}
self.modname = modname self.modname = modname
self.modpath = modpath self.modpath = modpath
self:normalize(assert(Module.load(modname, modpath))) local mod, cached = Module.load(modname, modpath)
M.dirty = M.dirty or not cached
self:normalize(assert(mod))
if modname == Config.options.plugins and not self.plugins["lazy.nvim"] then if modname == Config.options.plugins and not self.plugins["lazy.nvim"] then
self:add({ "folke/lazy.nvim", opt = false }) self:add({ "folke/lazy.nvim", opt = false })
end end
@ -176,12 +180,10 @@ function M.specs(cache)
end end
function M.load() function M.load()
local dirty = false
---@type boolean, LazyState? ---@type boolean, LazyState?
local ok, state = pcall(vim.json.decode, Cache.get("cache.state")) local ok, state = pcall(vim.json.decode, Cache.get("cache.state"))
if not (ok and state and vim.deep_equal(Config.options, state.config)) then if not (ok and state and vim.deep_equal(Config.options, state.config)) then
dirty = true M.dirty = true
state = nil state = nil
end end
@ -203,7 +205,7 @@ function M.load()
M.update_state() M.update_state()
Util.track() Util.track()
if dirty then if M.dirty then
Cache.dirty = true Cache.dirty = true
elseif state then elseif state then
require("lazy.core.loader").loaders = state.loaders require("lazy.core.loader").loaders = state.loaders
@ -215,7 +217,7 @@ function M.save()
local state = { local state = {
---@type table<string, LazySpec> ---@type table<string, LazySpec>
specs = {}, specs = {},
loaders = require("lazy.core.loader").loaders, loaders = require("lazy.core.loader").get_loaders(),
config = Config.options, config = Config.options,
} }