2022-11-22 20:28:08 +00:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Config = require("lazy.core.config")
|
2022-11-20 21:33:47 +00:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2022-11-23 15:07:57 +00:00
|
|
|
---@type LazyPlugin[]
|
|
|
|
M.loading = {}
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-12-01 10:06:44 +00:00
|
|
|
function M.setup()
|
2022-12-01 12:33:55 +00:00
|
|
|
-- install missing plugins
|
|
|
|
if Config.options.install_missing then
|
|
|
|
Util.track("install")
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if not plugin._.installed then
|
|
|
|
vim.cmd("do User LazyInstallPre")
|
|
|
|
require("lazy.manage").install({ wait = true })
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- setup handlers
|
|
|
|
Util.track("handlers")
|
2022-12-01 10:06:44 +00:00
|
|
|
local Handler = require("lazy.core.handler")
|
|
|
|
Handler.setup()
|
2022-12-01 12:33:55 +00:00
|
|
|
Util.track()
|
2022-12-01 10:06:44 +00:00
|
|
|
end
|
|
|
|
|
2022-11-20 21:33:47 +00:00
|
|
|
function M.init_plugins()
|
2022-12-01 15:27:52 +00:00
|
|
|
Util.track({ start = "init" })
|
2022-11-26 20:29:40 +00:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-11-20 21:34:55 +00:00
|
|
|
if plugin.init then
|
2022-12-01 15:27:52 +00:00
|
|
|
Util.track({ plugin = plugin.name, init = "init" })
|
2022-11-25 21:50:17 +00:00
|
|
|
Util.try(plugin.init, "Failed to run `init` for **" .. plugin.name .. "**")
|
2022-11-20 21:34:55 +00:00
|
|
|
Util.track()
|
|
|
|
end
|
2022-12-01 10:06:44 +00:00
|
|
|
if plugin.lazy == false then
|
2022-12-01 15:27:52 +00:00
|
|
|
M.load(plugin, { start = "startup" })
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-11-26 20:29:40 +00:00
|
|
|
---@class Loader
|
2022-11-20 21:33:47 +00:00
|
|
|
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
|
2022-11-23 15:07:57 +00:00
|
|
|
---@param reason {[string]:string}
|
2022-12-01 10:06:44 +00:00
|
|
|
function M.load(plugins, reason)
|
2022-11-26 18:33:38 +00:00
|
|
|
---@diagnostic disable-next-line: cast-local-type
|
|
|
|
plugins = type(plugins) == "string" or plugins.name and { plugins } or plugins
|
2022-11-22 20:12:33 +00:00
|
|
|
---@cast plugins (string|LazyPlugin)[]
|
2022-11-26 18:33:38 +00:00
|
|
|
|
2022-11-20 21:34:55 +00:00
|
|
|
for _, plugin in ipairs(plugins) do
|
2022-11-26 18:33:38 +00:00
|
|
|
plugin = type(plugin) == "string" and Config.plugins[plugin] or plugin
|
|
|
|
---@cast plugin LazyPlugin
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-28 10:19:50 +00:00
|
|
|
if not plugin._.loaded then
|
2022-11-24 21:04:23 +00:00
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch
|
2022-11-28 10:19:50 +00:00
|
|
|
plugin._.loaded = {}
|
2022-11-23 20:54:56 +00:00
|
|
|
for k, v in pairs(reason) do
|
2022-11-28 10:19:50 +00:00
|
|
|
plugin._.loaded[k] = v
|
2022-11-23 20:54:56 +00:00
|
|
|
end
|
2022-11-23 15:07:57 +00:00
|
|
|
if #M.loading > 0 then
|
2022-11-28 10:19:50 +00:00
|
|
|
plugin._.loaded.plugin = M.loading[#M.loading].name
|
2022-11-23 15:07:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(M.loading, plugin)
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-29 11:02:25 +00:00
|
|
|
Util.track({ plugin = plugin.name, start = reason.start })
|
2022-12-01 10:06:44 +00:00
|
|
|
M.packadd(plugin)
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-29 13:27:04 +00:00
|
|
|
if plugin.dependencies then
|
|
|
|
M.load(plugin.dependencies, {})
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-20 21:34:55 +00:00
|
|
|
if plugin.config then
|
2022-11-25 21:50:17 +00:00
|
|
|
Util.try(plugin.config, "Failed to run `config` for " .. plugin.name)
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-11-28 10:19:50 +00:00
|
|
|
plugin._.loaded.time = Util.track().time
|
2022-11-23 15:07:57 +00:00
|
|
|
table.remove(M.loading)
|
2022-11-22 20:12:33 +00:00
|
|
|
vim.schedule(function()
|
|
|
|
vim.cmd("do User LazyRender")
|
|
|
|
end)
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2022-12-01 10:06:44 +00:00
|
|
|
function M.packadd(plugin)
|
|
|
|
vim.cmd.packadd(plugin.name)
|
|
|
|
M.source_runtime(plugin, "/after/plugin")
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2022-11-30 13:41:20 +00:00
|
|
|
---@param dir? string
|
|
|
|
function M.source_runtime(plugin, dir)
|
|
|
|
Util.walk(plugin.dir .. dir, function(path, _, t)
|
2022-11-24 21:04:23 +00:00
|
|
|
local ext = path:sub(-3)
|
|
|
|
if t == "file" and (ext == "lua" or ext == "vim") then
|
|
|
|
vim.cmd("silent source " .. path)
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
end)
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|