2022-11-23 04:28:08 +08:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Config = require("lazy.core.config")
|
2022-12-04 06:15:50 +08:00
|
|
|
local Handler = require("lazy.core.handler")
|
2022-11-21 05:33:47 +08:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2022-11-23 23:07:57 +08:00
|
|
|
---@type LazyPlugin[]
|
|
|
|
M.loading = {}
|
2022-12-02 16:22:15 +08:00
|
|
|
M.init_done = false
|
2022-12-15 04:03:53 +08:00
|
|
|
---@type table<string,true>
|
2022-12-18 00:35:45 +08:00
|
|
|
M.disabled_rtp_plugins = { packer_compiled = true }
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-12-01 18:06:44 +08:00
|
|
|
function M.setup()
|
2022-12-01 20:33:55 +08:00
|
|
|
-- setup handlers
|
|
|
|
Util.track("handlers")
|
2022-12-01 18:06:44 +08:00
|
|
|
Handler.setup()
|
2022-12-01 20:33:55 +08:00
|
|
|
Util.track()
|
2022-12-02 16:22:15 +08:00
|
|
|
|
2022-12-15 04:03:53 +08:00
|
|
|
for _, file in ipairs(Config.options.performance.rtp.disabled_plugins) do
|
|
|
|
M.disabled_rtp_plugins[file] = true
|
|
|
|
end
|
|
|
|
|
2022-12-22 20:49:00 +08:00
|
|
|
vim.api.nvim_create_autocmd("ColorSchemePre", {
|
|
|
|
callback = function(event)
|
|
|
|
M.colorscheme(event.match)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2022-12-02 16:22:15 +08:00
|
|
|
-- autoload opt plugins
|
|
|
|
table.insert(package.loaders, M.autoload)
|
2022-12-23 14:35:44 +08: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
|
|
|
|
for _, colorscheme in ipairs(Config.options.install.colorscheme) do
|
|
|
|
if pcall(vim.cmd.colorscheme, colorscheme) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
require("lazy.manage").install({ wait = true })
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
end
|
2022-12-01 18:06:44 +08:00
|
|
|
end
|
|
|
|
|
2022-12-15 04:03:53 +08:00
|
|
|
-- Startup sequence
|
|
|
|
-- 1. load any start plugins and do init
|
|
|
|
function M.startup()
|
|
|
|
Util.track({ start = "startup" })
|
|
|
|
|
2022-12-21 21:34:31 +08:00
|
|
|
-- load filetype.lua first since plugins might depend on that
|
2022-12-21 06:35:06 +08:00
|
|
|
M.source(vim.env.VIMRUNTIME .. "/filetype.lua")
|
2022-12-21 03:32:37 +08:00
|
|
|
|
2022-12-21 21:34:31 +08:00
|
|
|
-- backup original rtp
|
|
|
|
local rtp = vim.opt.rtp:get()
|
2022-12-15 04:03:53 +08:00
|
|
|
|
2022-12-23 01:46:43 +08:00
|
|
|
-- 1. run plugin init
|
|
|
|
Util.track({ start = "init" })
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if plugin.init then
|
|
|
|
Util.track({ plugin = plugin.name, init = "init" })
|
2022-12-27 05:59:02 +08:00
|
|
|
Util.try(function()
|
|
|
|
plugin.init(plugin)
|
|
|
|
end, "Failed to run `init` for **" .. plugin.name .. "**")
|
2022-12-23 01:46:43 +08:00
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
|
|
|
-- 2. load start plugin
|
2022-12-15 04:03:53 +08:00
|
|
|
Util.track({ start = "start" })
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-12-23 01:46:43 +08:00
|
|
|
if plugin.lazy == false and not plugin._.loaded then
|
2022-12-15 04:03:53 +08:00
|
|
|
M.load(plugin, { start = "start" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-23 01:46:43 +08:00
|
|
|
-- 3. load plugins from rtp, excluding after
|
2022-12-21 21:34:31 +08:00
|
|
|
Util.track({ start = "rtp plugins" })
|
|
|
|
for _, path in ipairs(rtp) do
|
|
|
|
if not path:find("after/?$") then
|
|
|
|
M.packadd(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-23 01:46:43 +08:00
|
|
|
-- 4. load after plugins
|
2022-12-16 06:23:18 +08:00
|
|
|
Util.track({ start = "after" })
|
2022-12-21 06:35:06 +08:00
|
|
|
for _, path in ipairs(vim.opt.rtp:get()) do
|
2022-12-16 06:23:18 +08:00
|
|
|
if path:find("after/?$") then
|
|
|
|
M.source_runtime(path, "plugin")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-16 20:06:52 +08:00
|
|
|
M.init_done = true
|
|
|
|
|
2022-12-15 04:03:53 +08:00
|
|
|
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-12-01 18:06:44 +08:00
|
|
|
function M.load(plugins, reason)
|
2022-11-27 02:33:38 +08:00
|
|
|
---@diagnostic disable-next-line: cast-local-type
|
2022-12-02 16:22:15 +08:00
|
|
|
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-12-05 21:45:50 +08:00
|
|
|
for _, plugin in pairs(plugins) do
|
2022-12-25 21:06:41 +08:00
|
|
|
local try_load = true
|
|
|
|
|
|
|
|
if type(plugin) == "string" then
|
|
|
|
if not Config.plugins[plugin] then
|
|
|
|
Util.error("Plugin " .. plugin .. " not found")
|
|
|
|
try_load = false
|
|
|
|
else
|
|
|
|
plugin = Config.plugins[plugin]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-26 16:35:19 +08:00
|
|
|
if try_load and plugin.cond then
|
|
|
|
try_load = plugin.cond == true or (type(plugin.cond) == "function" and plugin.cond()) or false
|
|
|
|
plugin._.cond = try_load
|
|
|
|
end
|
|
|
|
|
2022-11-27 02:33:38 +08:00
|
|
|
---@cast plugin LazyPlugin
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-12-25 21:06:41 +08:00
|
|
|
if try_load and 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-12-02 16:22:15 +08:00
|
|
|
elseif reason.require then
|
|
|
|
plugin._.loaded.source = Util.get_source()
|
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-12-05 21:45:50 +08:00
|
|
|
Handler.disable(plugin)
|
2022-12-02 16:22:15 +08:00
|
|
|
|
|
|
|
vim.opt.runtimepath:prepend(plugin.dir)
|
2022-12-21 06:35:06 +08:00
|
|
|
local after = plugin.dir .. "/after"
|
|
|
|
if vim.loop.fs_stat(after) then
|
|
|
|
vim.opt.runtimepath:append(after)
|
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-11-29 21:27:04 +08:00
|
|
|
if plugin.dependencies then
|
2022-12-25 21:06:41 +08:00
|
|
|
Util.try(function()
|
|
|
|
M.load(plugin.dependencies, {})
|
|
|
|
end, "Failed to load deps for " .. plugin.name)
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-12-15 04:03:53 +08:00
|
|
|
M.packadd(plugin.dir)
|
2022-11-21 05:34:55 +08:00
|
|
|
if plugin.config then
|
2022-12-23 05:41:44 +08:00
|
|
|
M.config(plugin)
|
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
|
|
|
|
|
2022-12-23 05:41:44 +08:00
|
|
|
--- runs plugin config
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.config(plugin)
|
|
|
|
local fn
|
|
|
|
if type(plugin.config) == "function" then
|
2022-12-27 05:59:02 +08:00
|
|
|
fn = function()
|
|
|
|
plugin.config(plugin)
|
|
|
|
end
|
2022-12-23 05:41:44 +08:00
|
|
|
else
|
|
|
|
local normname = Util.normname(plugin.name)
|
|
|
|
---@type table<string, string>
|
|
|
|
local mods = {}
|
|
|
|
Util.ls(plugin.dir .. "/lua", function(_, modname)
|
|
|
|
modname = modname:gsub("%.lua$", "")
|
|
|
|
mods[modname] = modname
|
|
|
|
local modnorm = Util.normname(modname)
|
|
|
|
-- if we found an exact match, then use that
|
|
|
|
if modnorm == normname then
|
|
|
|
mods = { modname }
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
mods = vim.tbl_values(mods)
|
|
|
|
if #mods == 1 then
|
|
|
|
fn = function()
|
|
|
|
require(mods[1]).setup(plugin.config == true and {} or plugin.config)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return Util.error(
|
|
|
|
"Lua module not found for config of " .. plugin.name .. ". Please use a `config()` function instead"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.try(fn, "Failed to run `config` for " .. plugin.name)
|
|
|
|
end
|
|
|
|
|
2022-12-15 04:03:53 +08:00
|
|
|
---@param path string
|
|
|
|
function M.packadd(path)
|
|
|
|
M.source_runtime(path, "plugin")
|
|
|
|
M.ftdetect(path)
|
2022-12-04 00:43:55 +08:00
|
|
|
if M.init_done then
|
2022-12-15 04:03:53 +08:00
|
|
|
M.source_runtime(path, "after/plugin")
|
2022-12-02 16:22:15 +08:00
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-12-15 04:03:53 +08:00
|
|
|
---@param path string
|
|
|
|
function M.ftdetect(path)
|
2022-12-05 21:45:50 +08:00
|
|
|
vim.cmd("augroup filetypedetect")
|
2022-12-15 04:03:53 +08:00
|
|
|
M.source_runtime(path, "ftdetect")
|
2022-12-05 21:45:50 +08:00
|
|
|
vim.cmd("augroup END")
|
|
|
|
end
|
|
|
|
|
2022-12-04 00:43:55 +08:00
|
|
|
---@param ... string
|
|
|
|
function M.source_runtime(...)
|
|
|
|
local dir = table.concat({ ... }, "/")
|
2022-12-17 20:03:34 +08:00
|
|
|
---@type string[]
|
|
|
|
local files = {}
|
2022-12-15 04:03:53 +08:00
|
|
|
Util.walk(dir, function(path, name, t)
|
|
|
|
local ext = name:sub(-3)
|
|
|
|
name = name:sub(1, -5)
|
2022-12-21 23:27:56 +08:00
|
|
|
if (t == "file" or t == "link") and (ext == "lua" or ext == "vim") and not M.disabled_rtp_plugins[name] then
|
2022-12-17 20:03:34 +08:00
|
|
|
files[#files + 1] = path
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-11-25 05:04:23 +08:00
|
|
|
end)
|
2022-12-17 20:03:34 +08:00
|
|
|
-- plugin files are sourced alphabetically per directory
|
|
|
|
table.sort(files)
|
|
|
|
for _, path in ipairs(files) do
|
2022-12-21 06:35:06 +08:00
|
|
|
M.source(path)
|
2022-12-17 20:03:34 +08:00
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
end
|
|
|
|
|
2022-12-21 06:35:06 +08:00
|
|
|
function M.source(path)
|
|
|
|
Util.track({ runtime = path })
|
|
|
|
Util.try(function()
|
|
|
|
vim.cmd("silent source " .. path)
|
|
|
|
end, "Failed to source `" .. path .. "`")
|
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
|
2022-12-22 20:49:00 +08:00
|
|
|
function M.colorscheme(name)
|
|
|
|
if vim.tbl_contains(vim.fn.getcompletion("", "color"), name) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if not plugin._.loaded then
|
|
|
|
for _, ext in ipairs({ "lua", "vim" }) do
|
|
|
|
local path = plugin.dir .. "/colors/" .. name .. "." .. ext
|
|
|
|
if vim.loop.fs_stat(path) then
|
|
|
|
return M.load(plugin, { colorscheme = name })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-02 16:22:15 +08: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 20:06:52 +08:00
|
|
|
function M.autoload(modname)
|
2022-12-02 16:22:15 +08:00
|
|
|
-- check if a lazy plugin should be loaded
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-12-19 21:56:43 +08:00
|
|
|
if not (plugin._.loaded or plugin.module == false) then
|
2022-12-02 16:22:15 +08:00
|
|
|
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 20:06:52 +08: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 16:22:15 +08:00
|
|
|
local chunk, err = loadfile(path)
|
|
|
|
return chunk or error(err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-16 20:06:52 +08:00
|
|
|
return modname .. " not found in lazy plugins"
|
|
|
|
end
|
|
|
|
|
2022-11-21 05:33:47 +08:00
|
|
|
return M
|