local M = {} M.loaded = {} M.waiting = {} ---nice utility wrapper around vim.pack.add ---@param plugin string|table function M.load(plugin) if plugin == nil then error("value passed to load is nil") end if type(plugin) == "table" then if type(plugin[1]) == "table" then for _, i in ipairs(plugin) do M.load(i) end elseif type(plugin[1]) == "string" then local name = plugin[1] if plugin.source == nil then name = "https://github.com/" .. name elseif type(plugin.source) == "string" then name = plugin.source .. name else error("plugin source must be either string or nil") end if plugin.load == nil then vim.pack.add({ name }) else vim.pack.add({ name }, { load = plugin.load }) end else error("table passed to load is invalid") end elseif type(plugin) == "string" then vim.pack.add({ "https://github.com/" .. plugin }) else error("value passed to load must be string or table [of tables]* of strings") end end ---get plugin, expect it to be in @VIMRC/lua/plugins/name.lua ---@param plugin string ---@return Plugin function M.get(plugin) return require("plugins." .. plugin) end ---@param pkg Plugin ---@return boolean local isDoneWaiting = function(pkg) local after = pkg.after ---@cast after table for _, i in pairs(after) do if not M.loaded[i] then return false end end return true end ---@class Plugin ---@field configure fun(load: fun(string)): nil ---@field after? string|table ---@field configures? string ---@field onUpdate? fun(ev): nil ---parse a plugin in the util.Plugin format ---@param plugin Plugin function M.parse(plugin) local load = false if plugin.after == nil then load = true elseif type(plugin.after) == "string" then load = M.loaded[plugin.after] else load = isDoneWaiting(plugin) end if load then plugin.configure(M.load) if plugin.configures ~= nil then M.loaded[plugin.configures] = true if M.waiting[plugin.configures] ~= nil then for _, i in pairs(M.waiting[plugin.configures]) do M.parse(i) end end M.waiting[plugin.configures] = nil if plugin.onUpdate ~= nil then vim.api.nvim_create_autocmd("PackChanged", { callback = function(ev) local name, kind = ev.data.spec.name, ev.data.kind if name == plugin.configures and kind == "update" then if not ev.data.active then vim.cmd.packadd(plugin.configures) end plugin.onUpdate(ev) end end, }) end end else if type(plugin.after == "string") then if M.waiting[plugin.after] == nil then M.waiting[plugin.after] = {} end table.insert(M.waiting[plugin.after], plugin) else local after = plugin.after ---@cast after table for _, i in pairs(after) do if M.waiting[i] == nil then M.waiting[i] = {} end table.insert(M.waiting[i], plugin) end end end end return M