2022-11-23 04:28:08 +08:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Config = require("lazy.core.config")
|
2022-11-21 05:33:47 +08:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2022-11-23 23:07:57 +08:00
|
|
|
---@type LazyPlugin[]
|
|
|
|
M.loading = {}
|
2022-11-21 05:33:47 +08:00
|
|
|
|
|
|
|
function M.init_plugins()
|
2022-11-23 04:12:33 +08:00
|
|
|
Util.track("plugin_init")
|
2022-11-27 04:29:40 +08:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-11-21 05:34:55 +08:00
|
|
|
if plugin.init then
|
2022-11-29 19:02:25 +08:00
|
|
|
Util.track({ plugin = plugin.name, start = "init" })
|
2022-11-26 05:50:17 +08:00
|
|
|
Util.try(plugin.init, "Failed to run `init` for **" .. plugin.name .. "**")
|
2022-11-21 05:34:55 +08:00
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
if plugin.opt == false then
|
2022-11-24 04:54:56 +08:00
|
|
|
M.load(plugin, { start = "start" })
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-11-27 04:29:40 +08:00
|
|
|
---@class Loader
|
2022-11-21 05:33:47 +08:00
|
|
|
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
|
2022-11-23 23:07:57 +08:00
|
|
|
---@param reason {[string]:string}
|
2022-11-25 05:04:23 +08:00
|
|
|
---@param opts? {load_start: boolean}
|
|
|
|
function M.load(plugins, reason, opts)
|
2022-11-27 02:33:38 +08:00
|
|
|
---@diagnostic disable-next-line: cast-local-type
|
|
|
|
plugins = type(plugins) == "string" or plugins.name and { plugins } or plugins
|
2022-11-23 04:12:33 +08:00
|
|
|
---@cast plugins (string|LazyPlugin)[]
|
2022-11-27 02:33:38 +08:00
|
|
|
|
2022-11-21 05:34:55 +08:00
|
|
|
for _, plugin in ipairs(plugins) do
|
2022-11-27 02:33:38 +08:00
|
|
|
plugin = type(plugin) == "string" and Config.plugins[plugin] or plugin
|
|
|
|
---@cast plugin LazyPlugin
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-11-28 18:19:50 +08:00
|
|
|
if not plugin._.loaded then
|
2022-11-25 05:04:23 +08:00
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch
|
2022-11-28 18:19:50 +08:00
|
|
|
plugin._.loaded = {}
|
2022-11-24 04:54:56 +08:00
|
|
|
for k, v in pairs(reason) do
|
2022-11-28 18:19:50 +08:00
|
|
|
plugin._.loaded[k] = v
|
2022-11-24 04:54:56 +08:00
|
|
|
end
|
2022-11-23 23:07:57 +08:00
|
|
|
if #M.loading > 0 then
|
2022-11-28 18:19:50 +08:00
|
|
|
plugin._.loaded.plugin = M.loading[#M.loading].name
|
2022-11-23 23:07:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(M.loading, plugin)
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-11-29 19:02:25 +08:00
|
|
|
Util.track({ plugin = plugin.name, start = reason.start })
|
2022-11-25 05:04:23 +08:00
|
|
|
M.packadd(plugin, opts and opts.load_start)
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-11-29 21:27:04 +08:00
|
|
|
if plugin.dependencies then
|
|
|
|
M.load(plugin.dependencies, {})
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-11-21 05:34:55 +08:00
|
|
|
if plugin.config then
|
2022-11-26 05:50:17 +08:00
|
|
|
Util.try(plugin.config, "Failed to run `config` for " .. plugin.name)
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-11-28 18:19:50 +08:00
|
|
|
plugin._.loaded.time = Util.track().time
|
2022-11-23 23:07:57 +08:00
|
|
|
table.remove(M.loading)
|
2022-11-23 04:12:33 +08:00
|
|
|
vim.schedule(function()
|
|
|
|
vim.cmd("do User LazyRender")
|
|
|
|
end)
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2022-11-25 05:04:23 +08:00
|
|
|
function M.packadd(plugin, load_start)
|
2022-11-21 05:34:55 +08:00
|
|
|
if plugin.opt then
|
2022-11-25 22:35:40 +08:00
|
|
|
vim.cmd.packadd(plugin.name)
|
2022-11-21 05:34:55 +08:00
|
|
|
M.source_plugin_files(plugin, true)
|
2022-11-25 05:04:23 +08:00
|
|
|
elseif load_start then
|
2022-11-21 05:34:55 +08:00
|
|
|
vim.opt.runtimepath:append(plugin.dir)
|
|
|
|
M.source_plugin_files(plugin)
|
|
|
|
M.source_plugin_files(plugin, true)
|
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param after? boolean
|
|
|
|
function M.source_plugin_files(plugin, after)
|
2022-11-25 05:04:23 +08:00
|
|
|
Util.walk(plugin.dir .. (after and "/after" or "") .. "/plugin", function(path, _, t)
|
|
|
|
local ext = path:sub(-3)
|
|
|
|
if t == "file" and (ext == "lua" or ext == "vim") then
|
|
|
|
vim.cmd("silent source " .. path)
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-25 05:04:23 +08:00
|
|
|
end)
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|