2023-10-09 17:25:42 +08:00
|
|
|
local Cache = require("lazy.core.cache")
|
2022-11-23 04:28:08 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2022-12-04 06:15:50 +08:00
|
|
|
local Handler = require("lazy.core.handler")
|
2023-01-02 17:08:45 +08:00
|
|
|
local Plugin = require("lazy.core.plugin")
|
2023-10-09 17:25:42 +08:00
|
|
|
local Util = require("lazy.core.util")
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2023-02-14 18:00:56 +08:00
|
|
|
---@class LazyCoreLoader
|
2022-11-21 05:33:47 +08:00
|
|
|
local M = {}
|
|
|
|
|
2022-12-28 00:24:40 +08:00
|
|
|
local DEFAULT_PRIORITY = 50
|
|
|
|
|
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-12-30 05:40:15 +08:00
|
|
|
---@type table<string,string>
|
|
|
|
M.did_ftdetect = {}
|
2023-10-17 23:44:14 +08:00
|
|
|
M.did_handlers = false
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2023-01-04 19:40:00 +08:00
|
|
|
function M.disable_rtp_plugin(plugin)
|
|
|
|
M.disabled_rtp_plugins[plugin] = true
|
|
|
|
end
|
|
|
|
|
2022-12-01 18:06:44 +08:00
|
|
|
function M.setup()
|
2022-12-15 04:03:53 +08:00
|
|
|
for _, file in ipairs(Config.options.performance.rtp.disabled_plugins) do
|
2023-01-04 19:40:00 +08:00
|
|
|
M.disable_rtp_plugin(file)
|
2022-12-15 04:03:53 +08:00
|
|
|
end
|
|
|
|
|
2022-12-22 20:49:00 +08:00
|
|
|
vim.api.nvim_create_autocmd("ColorSchemePre", {
|
|
|
|
callback = function(event)
|
|
|
|
M.colorscheme(event.match)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2023-01-02 17:08:45 +08:00
|
|
|
-- load the plugins
|
|
|
|
Plugin.load()
|
2023-09-27 18:39:39 +08:00
|
|
|
Handler.init()
|
2023-01-02 17:08:45 +08:00
|
|
|
|
2022-12-23 14:35:44 +08:00
|
|
|
-- install missing plugins
|
|
|
|
if Config.options.install.missing then
|
|
|
|
Util.track("install")
|
2023-01-04 05:50:14 +08:00
|
|
|
local count = 0
|
2023-01-02 17:08:45 +08:00
|
|
|
while M.install_missing() do
|
2023-01-04 05:50:14 +08:00
|
|
|
count = count + 1
|
|
|
|
if count > 5 then
|
2024-06-19 01:39:47 +08:00
|
|
|
Util.error("Too many rounds of missing plugins")
|
2023-01-04 05:50:14 +08:00
|
|
|
break
|
|
|
|
end
|
2022-12-23 14:35:44 +08:00
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
end
|
2023-01-04 16:07:30 +08:00
|
|
|
Config.mapleader = vim.g.mapleader
|
2024-03-08 00:32:07 +08:00
|
|
|
Config.maplocalleader = vim.g.maplocalleader
|
2023-01-02 01:45:20 +08:00
|
|
|
|
2023-01-02 17:08:45 +08:00
|
|
|
-- report any warnings & errors
|
|
|
|
Config.spec:report()
|
|
|
|
|
2023-01-02 01:45:20 +08:00
|
|
|
-- setup handlers
|
|
|
|
Util.track("handlers")
|
|
|
|
Handler.setup()
|
2023-10-17 23:44:14 +08:00
|
|
|
M.did_handlers = true
|
2023-01-02 01:45:20 +08:00
|
|
|
Util.track()
|
2022-12-01 18:06:44 +08:00
|
|
|
end
|
|
|
|
|
2023-01-02 17:08:45 +08:00
|
|
|
-- this will incrementally install missing plugins
|
|
|
|
-- multiple rounds can happen when importing a spec from a missing plugin
|
|
|
|
function M.install_missing()
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2024-06-19 01:39:47 +08:00
|
|
|
local installed = plugin._.installed
|
|
|
|
local has_errors = Plugin.has_errors(plugin)
|
|
|
|
|
2024-06-24 20:14:41 +08:00
|
|
|
if not has_errors and not (installed and not plugin._.build) then
|
2023-01-02 17:08:45 +08:00
|
|
|
for _, colorscheme in ipairs(Config.options.install.colorscheme) do
|
2024-01-24 04:51:00 +08:00
|
|
|
if colorscheme == "default" then
|
|
|
|
break
|
|
|
|
end
|
2023-02-08 05:59:05 +08:00
|
|
|
M.colorscheme(colorscheme)
|
|
|
|
if vim.g.colors_name or pcall(vim.cmd.colorscheme, colorscheme) then
|
2023-01-02 17:08:45 +08:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-05-18 01:36:00 +08:00
|
|
|
Cache.reset()
|
2023-05-18 01:35:16 +08:00
|
|
|
require("lazy.manage").install({ wait = true, lockfile = true, clear = false })
|
2023-04-24 13:54:21 +08:00
|
|
|
-- remove any installed plugins from indexed, so cache will index again
|
2023-01-02 17:08:45 +08:00
|
|
|
for _, p in pairs(Config.plugins) do
|
|
|
|
if p._.installed then
|
2023-03-15 16:01:00 +08:00
|
|
|
Cache.reset(p.dir)
|
2023-01-02 17:08:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- reload plugins
|
|
|
|
Plugin.load()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2024-06-23 04:18:26 +08:00
|
|
|
local rtp = vim.opt.rtp:get() --[[@as string[] ]]
|
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" })
|
2022-12-28 00:24:40 +08:00
|
|
|
for _, plugin in ipairs(M.get_start_plugins()) do
|
|
|
|
-- plugin may be loaded by another plugin in the meantime
|
|
|
|
if not plugin._.loaded then
|
2022-12-15 04:03:53 +08:00
|
|
|
M.load(plugin, { start = "start" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-29 23:03:23 +08:00
|
|
|
-- 3. load plugins from the original 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
|
2023-05-28 17:02:35 +08:00
|
|
|
-- these paths don't will already have their ftdetect ran,
|
|
|
|
-- by sourcing filetype.lua above, so skip them
|
2024-06-23 04:18:26 +08:00
|
|
|
M.did_ftdetect[path] = path
|
2022-12-21 21:34:31 +08:00
|
|
|
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" })
|
2024-06-23 04:18:26 +08:00
|
|
|
for _, path in
|
|
|
|
ipairs(vim.opt.rtp:get() --[[@as string[] ]])
|
|
|
|
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-12-28 00:24:40 +08:00
|
|
|
function M.get_start_plugins()
|
|
|
|
---@type LazyPlugin[]
|
|
|
|
local start = {}
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if plugin.lazy == false and not plugin._.loaded then
|
|
|
|
start[#start + 1] = plugin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(start, function(a, b)
|
|
|
|
local ap = a.priority or DEFAULT_PRIORITY
|
|
|
|
local bp = b.priority or DEFAULT_PRIORITY
|
|
|
|
return ap > bp
|
|
|
|
end)
|
|
|
|
return start
|
|
|
|
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}
|
2023-01-06 14:11:50 +08:00
|
|
|
---@param opts? {force:boolean} when force is true, we skip the cond check
|
|
|
|
function M.load(plugins, reason, opts)
|
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
|
|
|
if type(plugin) == "string" then
|
2022-12-29 00:56:20 +08:00
|
|
|
if Config.plugins[plugin] then
|
2022-12-25 21:06:41 +08:00
|
|
|
plugin = Config.plugins[plugin]
|
2023-01-04 18:02:29 +08:00
|
|
|
elseif Config.spec.disabled[plugin] then
|
|
|
|
plugin = nil
|
2022-12-29 00:56:20 +08:00
|
|
|
else
|
|
|
|
Util.error("Plugin " .. plugin .. " not found")
|
|
|
|
plugin = nil
|
2022-12-25 21:06:41 +08:00
|
|
|
end
|
|
|
|
end
|
2022-12-29 00:56:20 +08:00
|
|
|
if plugin and not plugin._.loaded then
|
2023-01-06 14:11:50 +08:00
|
|
|
M._load(plugin, reason, opts)
|
2022-12-26 16:35:19 +08:00
|
|
|
end
|
2022-12-29 00:56:20 +08:00
|
|
|
end
|
|
|
|
end
|
2022-12-26 16:35:19 +08:00
|
|
|
|
2023-02-08 06:52:02 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.deactivate(plugin)
|
2023-02-11 00:47:25 +08:00
|
|
|
if not plugin._.loaded then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-02-08 06:52:02 +08:00
|
|
|
local main = M.get_main(plugin)
|
|
|
|
|
|
|
|
if main then
|
|
|
|
Util.try(function()
|
|
|
|
local mod = require(main)
|
|
|
|
if mod.deactivate then
|
|
|
|
mod.deactivate(plugin)
|
|
|
|
end
|
|
|
|
end, "Failed to deactivate plugin " .. plugin.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- execute deactivate when needed
|
2023-02-11 00:47:25 +08:00
|
|
|
if plugin.deactivate then
|
2023-02-08 06:52:02 +08:00
|
|
|
Util.try(function()
|
|
|
|
plugin.deactivate(plugin)
|
|
|
|
end, "Failed to deactivate plugin " .. plugin.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable handlers
|
|
|
|
Handler.disable(plugin)
|
|
|
|
|
2023-10-26 02:00:50 +08:00
|
|
|
-- clear plugin properties cache
|
|
|
|
plugin._.cache = nil
|
|
|
|
|
2023-02-08 06:52:02 +08:00
|
|
|
-- remove loaded lua modules
|
|
|
|
Util.walkmods(plugin.dir .. "/lua", function(modname)
|
|
|
|
package.loaded[modname] = nil
|
|
|
|
package.preload[modname] = nil
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- clear vim.g.loaded_ for plugins
|
|
|
|
Util.ls(plugin.dir .. "/plugin", function(_, name, type)
|
|
|
|
if type == "file" then
|
|
|
|
vim.g["loaded_" .. name:gsub("%..*", "")] = nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
-- set as not loaded
|
|
|
|
plugin._.loaded = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--- reload a plugin
|
2023-05-27 21:19:50 +08:00
|
|
|
---@param plugin LazyPlugin|string
|
2023-02-08 06:52:02 +08:00
|
|
|
function M.reload(plugin)
|
2023-05-27 21:19:50 +08:00
|
|
|
if type(plugin) == "string" then
|
|
|
|
plugin = Config.plugins[plugin]
|
|
|
|
end
|
|
|
|
|
|
|
|
if not plugin then
|
|
|
|
error("Plugin not found")
|
|
|
|
end
|
|
|
|
|
|
|
|
local load = plugin._.loaded ~= nil
|
2023-02-08 06:52:02 +08:00
|
|
|
M.deactivate(plugin)
|
|
|
|
|
|
|
|
-- enable handlers
|
|
|
|
Handler.enable(plugin)
|
|
|
|
|
|
|
|
-- run init
|
|
|
|
if plugin.init then
|
|
|
|
Util.try(function()
|
|
|
|
plugin.init(plugin)
|
|
|
|
end, "Failed to run `init` for **" .. plugin.name .. "**")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if this is a start plugin, load it now
|
|
|
|
if plugin.lazy == false then
|
|
|
|
load = true
|
|
|
|
end
|
|
|
|
|
2023-10-17 14:36:09 +08:00
|
|
|
local events = plugin._.handlers and plugin._.handlers.event and plugin._.handlers.event or {}
|
|
|
|
|
|
|
|
for _, event in pairs(events) do
|
|
|
|
if event.id:find("VimEnter") or event.id:find("UIEnter") or event.id:find("VeryLazy") then
|
2023-02-08 06:52:02 +08:00
|
|
|
load = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-27 21:19:11 +08:00
|
|
|
-- reload any vimscript files for this plugin
|
2023-07-22 14:24:24 +08:00
|
|
|
local scripts = vim.fn.getscriptinfo()
|
2023-05-27 21:19:11 +08:00
|
|
|
local loaded_scripts = {}
|
|
|
|
for _, s in ipairs(scripts) do
|
|
|
|
---@type string
|
|
|
|
local path = s.name
|
|
|
|
if
|
|
|
|
path:sub(-4) == ".vim"
|
|
|
|
and path:find(plugin.dir, 1, true) == 1
|
|
|
|
and not path:find("/plugin/", 1, true)
|
|
|
|
and not path:find("/ftplugin/", 1, true)
|
|
|
|
then
|
|
|
|
loaded_scripts[#loaded_scripts + 1] = path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-08 06:52:02 +08:00
|
|
|
if load then
|
|
|
|
M.load(plugin, { start = "reload" })
|
2023-05-27 21:19:11 +08:00
|
|
|
for _, s in ipairs(loaded_scripts) do
|
|
|
|
M.source(s)
|
|
|
|
end
|
2023-02-08 06:52:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 00:56:20 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param reason {[string]:string}
|
2023-01-06 14:11:50 +08:00
|
|
|
---@param opts? {force:boolean} when force is true, we skip the cond check
|
|
|
|
function M._load(plugin, reason, opts)
|
2022-12-29 00:56:20 +08:00
|
|
|
if not plugin._.installed then
|
|
|
|
return Util.error("Plugin " .. plugin.name .. " is not installed")
|
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2023-07-06 21:30:01 +08:00
|
|
|
if plugin._.cond == false and not (opts and opts.force) then
|
|
|
|
return
|
2022-12-29 00:56:20 +08:00
|
|
|
end
|
2022-11-23 23:07:57 +08:00
|
|
|
|
2023-09-27 18:39:39 +08:00
|
|
|
if not Handler.did_setup then
|
|
|
|
Util.try(function()
|
|
|
|
Handler.enable(plugin)
|
|
|
|
end, "Failed to setup handlers for " .. plugin.name)
|
|
|
|
end
|
|
|
|
|
2022-12-29 00:56:20 +08:00
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch
|
|
|
|
plugin._.loaded = {}
|
|
|
|
for k, v in pairs(reason) do
|
|
|
|
plugin._.loaded[k] = v
|
|
|
|
end
|
|
|
|
if #M.loading > 0 then
|
|
|
|
plugin._.loaded.plugin = M.loading[#M.loading].name
|
|
|
|
elseif reason.require then
|
|
|
|
plugin._.loaded.source = Util.get_source()
|
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-12-29 00:56:20 +08:00
|
|
|
table.insert(M.loading, plugin)
|
2022-12-02 16:22:15 +08:00
|
|
|
|
2022-12-29 00:56:20 +08:00
|
|
|
Util.track({ plugin = plugin.name, start = reason.start })
|
|
|
|
Handler.disable(plugin)
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2022-12-29 23:03:23 +08:00
|
|
|
M.add_to_rtp(plugin)
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2024-06-24 20:14:41 +08:00
|
|
|
if plugin._.pkg and plugin._.pkg.source == "rockspec" then
|
|
|
|
M.add_to_luapath(plugin)
|
|
|
|
end
|
|
|
|
|
2022-12-29 00:56:20 +08:00
|
|
|
if plugin.dependencies then
|
|
|
|
Util.try(function()
|
|
|
|
M.load(plugin.dependencies, {})
|
|
|
|
end, "Failed to load deps for " .. plugin.name)
|
|
|
|
end
|
2022-11-21 05:33:47 +08:00
|
|
|
|
2023-01-06 02:43:47 +08:00
|
|
|
M.packadd(plugin.dir)
|
2023-01-08 22:01:49 +08:00
|
|
|
if plugin.config or plugin.opts then
|
2022-12-29 00:56:20 +08:00
|
|
|
M.config(plugin)
|
2022-11-21 05:34:55 +08:00
|
|
|
end
|
2022-12-29 00:56:20 +08:00
|
|
|
|
|
|
|
plugin._.loaded.time = Util.track().time
|
|
|
|
table.remove(M.loading)
|
|
|
|
vim.schedule(function()
|
2023-07-12 23:21:32 +08:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyLoad", modeline = false, data = plugin.name })
|
2023-01-13 16:00:15 +08:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
|
2022-12-29 00:56:20 +08:00
|
|
|
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()
|
2023-01-16 03:00:07 +08:00
|
|
|
local opts = Plugin.values(plugin, "opts", false)
|
2023-01-12 20:07:51 +08:00
|
|
|
plugin.config(plugin, opts)
|
2022-12-27 05:59:02 +08:00
|
|
|
end
|
2022-12-23 05:41:44 +08:00
|
|
|
else
|
2023-02-08 06:52:02 +08:00
|
|
|
local main = M.get_main(plugin)
|
|
|
|
if main then
|
2022-12-23 05:41:44 +08:00
|
|
|
fn = function()
|
2023-01-16 03:00:07 +08:00
|
|
|
local opts = Plugin.values(plugin, "opts", false)
|
2023-02-08 06:52:02 +08:00
|
|
|
require(main).setup(opts)
|
2022-12-23 05:41:44 +08:00
|
|
|
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
|
|
|
|
|
2023-02-08 06:52:02 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.get_main(plugin)
|
|
|
|
if plugin.main then
|
|
|
|
return plugin.main
|
|
|
|
end
|
2023-05-13 22:09:48 +08:00
|
|
|
if plugin.name ~= "mini.nvim" and plugin.name:match("^mini%..*$") then
|
|
|
|
return plugin.name
|
|
|
|
end
|
2023-02-08 06:52:02 +08:00
|
|
|
local normname = Util.normname(plugin.name)
|
|
|
|
---@type string[]
|
|
|
|
local mods = {}
|
2023-03-15 22:10:44 +08:00
|
|
|
for _, mod in ipairs(Cache.find("*", { all = true, rtp = false, paths = { plugin.dir } })) do
|
|
|
|
local modname = mod.modname
|
2023-02-08 06:52:02 +08:00
|
|
|
mods[#mods + 1] = modname
|
|
|
|
local modnorm = Util.normname(modname)
|
|
|
|
-- if we found an exact match, then use that
|
|
|
|
if modnorm == normname then
|
|
|
|
mods = { modname }
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-05-13 22:09:48 +08:00
|
|
|
|
2023-02-08 06:52:02 +08:00
|
|
|
return #mods == 1 and mods[1] or nil
|
|
|
|
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-30 05:40:15 +08:00
|
|
|
if not M.did_ftdetect[path] then
|
|
|
|
M.did_ftdetect[path] = path
|
|
|
|
vim.cmd("augroup filetypedetect")
|
|
|
|
M.source_runtime(path, "ftdetect")
|
|
|
|
vim.cmd("augroup END")
|
|
|
|
end
|
2022-12-05 21:45:50 +08:00
|
|
|
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-29 23:03:23 +08:00
|
|
|
-- This does the same as runtime.c:add_pack_dir_to_rtp
|
|
|
|
-- * find first after
|
|
|
|
-- * find lazy pack path
|
|
|
|
-- * insert right after lazy pack path or right before first after or at the end
|
|
|
|
-- * insert after dir right before first after or append to the end
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.add_to_rtp(plugin)
|
|
|
|
local rtp = vim.api.nvim_get_runtime_file("", true)
|
|
|
|
local idx_dir, idx_after
|
|
|
|
|
|
|
|
for i, path in ipairs(rtp) do
|
2024-06-25 19:23:25 +08:00
|
|
|
if Util.is_win then
|
2022-12-30 06:06:37 +08:00
|
|
|
path = Util.norm(path)
|
|
|
|
end
|
2022-12-29 23:03:23 +08:00
|
|
|
if path == Config.me then
|
|
|
|
idx_dir = i + 1
|
|
|
|
elseif not idx_after and path:sub(-6, -1) == "/after" then
|
|
|
|
idx_after = i + 1 -- +1 to offset the insert of the plugin dir
|
|
|
|
idx_dir = idx_dir or i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(rtp, idx_dir or (#rtp + 1), plugin.dir)
|
|
|
|
|
|
|
|
local after = plugin.dir .. "/after"
|
2024-03-22 15:58:36 +08:00
|
|
|
if vim.uv.fs_stat(after) then
|
2022-12-29 23:03:23 +08:00
|
|
|
table.insert(rtp, idx_after or (#rtp + 1), after)
|
|
|
|
end
|
|
|
|
|
2024-06-04 12:58:19 +08:00
|
|
|
---@type vim.Option
|
2022-12-29 23:03:23 +08:00
|
|
|
vim.opt.rtp = rtp
|
|
|
|
end
|
|
|
|
|
2024-06-24 20:14:41 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.add_to_luapath(plugin)
|
|
|
|
local root = Config.options.rocks.root .. "/" .. plugin.name
|
|
|
|
local path = root .. "/share/lua/5.1"
|
|
|
|
local cpath = root .. "/lib/lua/5.1"
|
|
|
|
package.path = package.path .. ";" .. path .. "/?.lua;" .. path .. "/?/init.lua;"
|
|
|
|
package.cpath = package.cpath .. ";" .. cpath .. "/?." .. (jit.os:find("Windows") and "dll" or "so") .. ";"
|
|
|
|
end
|
|
|
|
|
2022-12-21 06:35:06 +08:00
|
|
|
function M.source(path)
|
|
|
|
Util.track({ runtime = path })
|
|
|
|
Util.try(function()
|
2023-01-07 02:16:39 +08:00
|
|
|
vim.cmd("source " .. path)
|
2022-12-21 06:35:06 +08:00
|
|
|
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
|
2024-03-22 15:58:36 +08:00
|
|
|
if vim.uv.fs_stat(path) then
|
2022-12-22 20:49:00 +08:00
|
|
|
return M.load(plugin, { colorscheme = name })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-14 18:00:56 +08:00
|
|
|
function M.auto_load(modname, modpath)
|
2024-06-25 03:56:43 +08:00
|
|
|
local plugin = Plugin.find(modpath, { fast = not M.did_handlers })
|
2024-06-25 05:42:51 +08:00
|
|
|
if plugin then
|
2023-10-15 14:51:54 +08:00
|
|
|
plugin._.rtp_loaded = true
|
2023-10-17 23:44:14 +08:00
|
|
|
-- don't load if:
|
|
|
|
-- * handlers haven't been setup yet
|
|
|
|
-- * we're loading specs
|
|
|
|
-- * the plugin is already loaded
|
|
|
|
if M.did_handlers and not (Plugin.loading or plugin._.loaded) then
|
2023-02-14 18:00:56 +08:00
|
|
|
if plugin.module == false then
|
|
|
|
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
|
|
|
|
end
|
|
|
|
M.load(plugin, { require = modname })
|
2023-03-14 18:17:54 +08:00
|
|
|
if plugin._.cond == false then
|
|
|
|
error("You're trying to load `" .. plugin.name .. "` for which `cond==false`")
|
|
|
|
end
|
2023-02-14 18:00:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
---@param modname string
|
|
|
|
function M.loader(modname)
|
2024-06-25 03:56:43 +08:00
|
|
|
local paths, cached = Util.get_unloaded_rtp(modname, { cache = true })
|
2023-03-15 22:10:44 +08:00
|
|
|
local ret = Cache.find(modname, { rtp = false, paths = paths })[1]
|
2024-06-25 03:56:43 +08:00
|
|
|
|
|
|
|
if not ret and cached then
|
|
|
|
paths = Util.get_unloaded_rtp(modname)
|
|
|
|
ret = Cache.find(modname, { rtp = false, paths = paths })[1]
|
|
|
|
end
|
|
|
|
|
2023-03-15 22:10:44 +08:00
|
|
|
if ret then
|
|
|
|
M.auto_load(modname, ret.modpath)
|
2023-02-14 18:00:56 +08:00
|
|
|
local mod = package.loaded[modname]
|
|
|
|
if type(mod) == "table" then
|
|
|
|
return function()
|
|
|
|
return mod
|
2023-02-13 19:01:56 +08:00
|
|
|
end
|
|
|
|
end
|
2023-03-15 22:10:44 +08:00
|
|
|
-- selene: allow(incorrect_standard_library_use)
|
|
|
|
return loadfile(ret.modpath, nil, nil, ret.stat)
|
2023-02-13 19:01:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-21 05:33:47 +08:00
|
|
|
return M
|