2022-11-22 20:28:08 +00:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Config = require("lazy.core.config")
|
2022-12-03 22:15:50 +00:00
|
|
|
local Handler = require("lazy.core.handler")
|
2022-11-20 21:33:47 +00:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2022-11-23 15:07:57 +00:00
|
|
|
---@type LazyPlugin[]
|
|
|
|
M.loading = {}
|
2022-12-02 08:22:15 +00:00
|
|
|
M.init_done = false
|
2022-12-14 20:03:53 +00:00
|
|
|
---@type table<string,true>
|
|
|
|
M.disabled_rtp_plugins = {}
|
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
|
2022-12-02 18:18:10 +00:00
|
|
|
if Config.options.install.missing then
|
2022-12-01 12:33:55 +00:00
|
|
|
Util.track("install")
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if not plugin._.installed then
|
2022-12-02 21:45:33 +00:00
|
|
|
for _, colorscheme in ipairs(Config.options.install.colorscheme) do
|
|
|
|
if pcall(vim.cmd.colorscheme, colorscheme) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-12-01 12:33:55 +00:00
|
|
|
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
|
|
|
Handler.setup()
|
2022-12-01 12:33:55 +00:00
|
|
|
Util.track()
|
2022-12-02 08:22:15 +00:00
|
|
|
|
2022-12-14 20:03:53 +00:00
|
|
|
for _, file in ipairs(Config.options.performance.rtp.disabled_plugins) do
|
|
|
|
M.disabled_rtp_plugins[file] = true
|
|
|
|
end
|
|
|
|
|
2022-12-02 08:22:15 +00:00
|
|
|
-- autoload opt plugins
|
|
|
|
table.insert(package.loaders, M.autoload)
|
2022-12-01 10:06:44 +00:00
|
|
|
end
|
|
|
|
|
2022-12-14 20:03:53 +00:00
|
|
|
-- Startup sequence
|
|
|
|
-- 1. load any start plugins and do init
|
|
|
|
function M.startup()
|
|
|
|
Util.track({ start = "startup" })
|
|
|
|
|
2022-12-15 22:23:18 +00:00
|
|
|
local rtp = vim.opt.rtp:get()
|
|
|
|
|
|
|
|
-- load plugins from rtp, excluding after
|
2022-12-14 20:03:53 +00:00
|
|
|
Util.track({ start = "rtp plugins" })
|
2022-12-15 22:23:18 +00:00
|
|
|
for _, path in ipairs(rtp) do
|
2022-12-14 20:03:53 +00:00
|
|
|
if not path:find("after/?$") then
|
2022-12-16 12:06:52 +00:00
|
|
|
M.packadd(path)
|
2022-12-14 20:03:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-15 22:23:18 +00:00
|
|
|
-- load start plugin
|
2022-12-14 20:03:53 +00:00
|
|
|
Util.track({ start = "start" })
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if plugin.lazy == false then
|
|
|
|
M.load(plugin, { start = "start" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-15 22:23:18 +00:00
|
|
|
-- load after files
|
|
|
|
Util.track({ start = "after" })
|
|
|
|
-- load after files from plugins
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if plugin._.loaded then
|
|
|
|
M.source_runtime(plugin.dir, "after/plugin")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- load after files from rtp plugins
|
|
|
|
for _, path in ipairs(rtp) do
|
|
|
|
if path:find("after/?$") then
|
|
|
|
M.source_runtime(path, "plugin")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-16 12:06:52 +00:00
|
|
|
M.init_done = true
|
|
|
|
|
2022-12-15 22:23:18 +00:00
|
|
|
-- run plugin init
|
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-14 20:03:53 +00:00
|
|
|
end
|
|
|
|
Util.track()
|
2022-12-03 16:43:55 +00:00
|
|
|
|
2022-12-14 20:03:53 +00:00
|
|
|
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
|
2022-12-02 08:22:15 +00:00
|
|
|
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-12-05 13:45:50 +00:00
|
|
|
for _, plugin in pairs(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-12-02 08:22:15 +00:00
|
|
|
elseif reason.require then
|
|
|
|
plugin._.loaded.source = Util.get_source()
|
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-05 13:45:50 +00:00
|
|
|
Handler.disable(plugin)
|
2022-12-02 08:22:15 +00:00
|
|
|
|
|
|
|
vim.opt.runtimepath:prepend(plugin.dir)
|
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-12-14 20:03:53 +00:00
|
|
|
M.packadd(plugin.dir)
|
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
|
|
|
|
|
2022-12-14 20:03:53 +00:00
|
|
|
---@param path string
|
|
|
|
function M.packadd(path)
|
|
|
|
M.source_runtime(path, "plugin")
|
|
|
|
M.ftdetect(path)
|
2022-12-03 16:43:55 +00:00
|
|
|
if M.init_done then
|
2022-12-14 20:03:53 +00:00
|
|
|
M.source_runtime(path, "after/plugin")
|
2022-12-02 08:22:15 +00:00
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-12-14 20:03:53 +00:00
|
|
|
---@param path string
|
|
|
|
function M.ftdetect(path)
|
2022-12-05 13:45:50 +00:00
|
|
|
vim.cmd("augroup filetypedetect")
|
2022-12-14 20:03:53 +00:00
|
|
|
M.source_runtime(path, "ftdetect")
|
2022-12-05 13:45:50 +00:00
|
|
|
vim.cmd("augroup END")
|
|
|
|
end
|
|
|
|
|
2022-12-03 16:43:55 +00:00
|
|
|
---@param ... string
|
|
|
|
function M.source_runtime(...)
|
|
|
|
local dir = table.concat({ ... }, "/")
|
2022-12-17 12:03:34 +00:00
|
|
|
---@type string[]
|
|
|
|
local files = {}
|
2022-12-14 20:03:53 +00:00
|
|
|
Util.walk(dir, function(path, name, t)
|
|
|
|
local ext = name:sub(-3)
|
|
|
|
name = name:sub(1, -5)
|
|
|
|
if t == "file" and (ext == "lua" or ext == "vim") and not M.disabled_rtp_plugins[name] then
|
2022-12-17 12:03:34 +00:00
|
|
|
files[#files + 1] = path
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
end)
|
2022-12-17 12:03:34 +00:00
|
|
|
-- plugin files are sourced alphabetically per directory
|
|
|
|
table.sort(files)
|
|
|
|
for _, path in ipairs(files) do
|
|
|
|
Util.track({ runtime = path })
|
|
|
|
Util.try(function()
|
|
|
|
vim.cmd("silent source " .. path)
|
|
|
|
end, "Failed to source `" .. path .. "`")
|
|
|
|
Util.track()
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 08:22:15 +00:00
|
|
|
-- This loader is added as the very last one.
|
|
|
|
-- This only hits when the modname is not cached and
|
|
|
|
-- even then only once per plugin. So pretty much never.
|
|
|
|
---@param modname string
|
2022-12-16 12:06:52 +00:00
|
|
|
function M.autoload(modname)
|
2022-12-02 08:22:15 +00:00
|
|
|
-- check if a lazy plugin should be loaded
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if not plugin._.loaded then
|
|
|
|
for _, pattern in ipairs({ ".lua", "/init.lua" }) do
|
|
|
|
local path = plugin.dir .. "/lua/" .. modname:gsub("%.", "/") .. pattern
|
|
|
|
if vim.loop.fs_stat(path) then
|
|
|
|
M.load(plugin, { require = modname })
|
2022-12-16 12:06:52 +00:00
|
|
|
-- check if the module has been loaded in the meantime
|
|
|
|
if type(package.loaded[modname]) == "table" then
|
|
|
|
local mod = package.loaded[modname]
|
|
|
|
return function()
|
|
|
|
return mod
|
|
|
|
end
|
|
|
|
end
|
2022-12-02 08:22:15 +00:00
|
|
|
local chunk, err = loadfile(path)
|
|
|
|
return chunk or error(err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-16 12:06:52 +00:00
|
|
|
return modname .. " not found in lazy plugins"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- lazy.cache will call this when loading a cached file with modpath set.
|
|
|
|
---@param modname string
|
|
|
|
---@param modpath string
|
|
|
|
function M.check_load(modname, modpath)
|
|
|
|
-- no need to check anything before init
|
|
|
|
if M.init_done then
|
|
|
|
local plugin = require("lazy.core.plugin").find(modpath)
|
|
|
|
if plugin and not plugin._.loaded then
|
|
|
|
M.load(plugin, { require = modname })
|
|
|
|
end
|
|
|
|
end
|
2022-12-02 08:22:15 +00:00
|
|
|
end
|
|
|
|
|
2022-11-20 21:33:47 +00:00
|
|
|
return M
|