diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index 57212b49..596294ec 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -18,38 +18,31 @@ M.loaders = nil ---@type LazyPlugin[] M.loading = {} ----@param plugin LazyPlugin -function M.add(plugin) - if plugin.init or (plugin.opt == false) then - table.insert(M.loaders.init, plugin.name) +function M.get_loaders() + ---@type table>|{init: string[]} + local loaders = { init = {} } + for _, lt in ipairs(M.types) do + loaders[lt] = {} end - - for _, loader_type in ipairs(M.types) do - ---@type (string|string[])? - local loaders = plugin[loader_type] - if plugin[loader_type] then - loaders = type(loaders) == "table" and loaders or { loaders } - ---@cast loaders string[] - for _, loader in ipairs(loaders) do - if not M.loaders[loader_type][loader] then - M.loaders[loader_type][loader] = {} + for _, plugin in pairs(Config.plugins) do + if plugin.init or (plugin.opt == false) then + table.insert(loaders.init, plugin.name) + end + for _, lt in ipairs(M.types) do + if plugin[lt] then + ---@diagnostic disable-next-line: no-unknown + for _, loader in ipairs(type(plugin[lt]) == "table" and plugin[lt] or { plugin[lt] }) do + loaders[lt][loader] = loaders[lt][loader] or {} + table.insert(loaders[lt][loader], plugin.name) end - table.insert(M.loaders[loader_type][loader], plugin.name) end end end + return loaders end function M.setup() - if not M.loaders then - 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 + M.loaders = M.loaders or M.get_loaders() local group = vim.api.nvim_create_augroup("lazy_loader", { clear = true, @@ -167,53 +160,42 @@ end ---@param modname string function M.module(modname) local idx = modname:find(".", 1, true) or #modname + 1 - while idx do local name = modname:sub(1, idx - 1) local plugins = M.loaders.module[name] if plugins then + M.loaders.module[name] = nil local reason = { require = modname } if #M.loading == 0 then local f = 3 while not reason.source do local info = debug.getinfo(f, "S") - f = f + 1 if not info then break end if info.what ~= "C" then reason.source = info.source:sub(2) end + f = f + 1 end end M.load(plugins, reason) end idx = modname:find(".", idx + 1, true) end - - ---@diagnostic disable-next-line: no-unknown - local mod = package.loaded[modname] - if type(mod) == "table" then - return function() - return mod - end - end end ---@param plugins string|LazyPlugin|string[]|LazyPlugin[] ---@param reason {[string]:string} ---@param opts? {load_start: boolean} function M.load(plugins, reason, opts) - if type(plugins) == "string" or plugins.name then - ---@diagnostic disable-next-line: assign-type-mismatch - plugins = { plugins } - end - + ---@diagnostic disable-next-line: cast-local-type + plugins = type(plugins) == "string" or plugins.name and { plugins } or plugins ---@cast plugins (string|LazyPlugin)[] + for _, plugin in ipairs(plugins) do - if type(plugin) == "string" then - plugin = Config.plugins[plugin] - end + plugin = type(plugin) == "string" and Config.plugins[plugin] or plugin + ---@cast plugin LazyPlugin if not plugin.loaded then ---@diagnostic disable-next-line: assign-type-mismatch diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index f9903f7b..bb45233b 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -9,6 +9,8 @@ local M = {} local skip = { installed = true, loaded = true, tasks = true, dirty = true, dir = true } local funs = { config = true, init = true, run = true } +M.dirty = false + ---@class LazyPlugin ---@field [1] string ---@field name string display name and name used for plugin config files @@ -46,7 +48,9 @@ function Spec.load(modname, modpath) self.plugins = {} self.modname = modname 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 self:add({ "folke/lazy.nvim", opt = false }) end @@ -176,12 +180,10 @@ function M.specs(cache) end function M.load() - local dirty = false - ---@type boolean, LazyState? 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 - dirty = true + M.dirty = true state = nil end @@ -203,7 +205,7 @@ function M.load() M.update_state() Util.track() - if dirty then + if M.dirty then Cache.dirty = true elseif state then require("lazy.core.loader").loaders = state.loaders @@ -215,7 +217,7 @@ function M.save() local state = { ---@type table specs = {}, - loaders = require("lazy.core.loader").loaders, + loaders = require("lazy.core.loader").get_loaders(), config = Config.options, }