2023-10-09 10:25:42 +01:00
|
|
|
local Cache = require("lazy.core.cache")
|
2022-11-22 20:28:08 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2022-12-03 22:15:50 +00:00
|
|
|
local Handler = require("lazy.core.handler")
|
2023-01-02 09:08:45 +00:00
|
|
|
local Plugin = require("lazy.core.plugin")
|
2023-10-09 10:25:42 +01:00
|
|
|
local Util = require("lazy.core.util")
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
---@class LazyCoreLoader
|
2022-11-20 21:33:47 +00:00
|
|
|
local M = {}
|
|
|
|
|
2022-12-27 16:24:40 +00:00
|
|
|
local DEFAULT_PRIORITY = 50
|
|
|
|
|
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>
|
2022-12-17 16:35:45 +00:00
|
|
|
M.disabled_rtp_plugins = { packer_compiled = true }
|
2022-12-29 21:40:15 +00:00
|
|
|
---@type table<string,string>
|
|
|
|
M.did_ftdetect = {}
|
2023-10-17 16:44:14 +01:00
|
|
|
M.did_handlers = false
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2023-01-04 11:40:00 +00:00
|
|
|
function M.disable_rtp_plugin(plugin)
|
|
|
|
M.disabled_rtp_plugins[plugin] = true
|
|
|
|
end
|
|
|
|
|
2022-12-01 10:06:44 +00:00
|
|
|
function M.setup()
|
2022-12-14 20:03:53 +00:00
|
|
|
for _, file in ipairs(Config.options.performance.rtp.disabled_plugins) do
|
2023-01-04 11:40:00 +00:00
|
|
|
M.disable_rtp_plugin(file)
|
2022-12-14 20:03:53 +00:00
|
|
|
end
|
|
|
|
|
2022-12-22 12:49:00 +00:00
|
|
|
vim.api.nvim_create_autocmd("ColorSchemePre", {
|
|
|
|
callback = function(event)
|
|
|
|
M.colorscheme(event.match)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2023-01-02 09:08:45 +00:00
|
|
|
-- load the plugins
|
|
|
|
Plugin.load()
|
2023-09-27 11:39:39 +01:00
|
|
|
Handler.init()
|
2023-01-02 09:08:45 +00:00
|
|
|
|
2022-12-23 06:35:44 +00:00
|
|
|
-- install missing plugins
|
|
|
|
if Config.options.install.missing then
|
|
|
|
Util.track("install")
|
2023-01-03 21:50:14 +00:00
|
|
|
local count = 0
|
2023-01-02 09:08:45 +00:00
|
|
|
while M.install_missing() do
|
2023-01-03 21:50:14 +00:00
|
|
|
count = count + 1
|
|
|
|
if count > 5 then
|
2024-06-18 18:39:47 +01:00
|
|
|
Util.error("Too many rounds of missing plugins")
|
2023-01-03 21:50:14 +00:00
|
|
|
break
|
|
|
|
end
|
2022-12-23 06:35:44 +00:00
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
end
|
2023-01-04 08:07:30 +00:00
|
|
|
Config.mapleader = vim.g.mapleader
|
2024-03-07 16:32:07 +00:00
|
|
|
Config.maplocalleader = vim.g.maplocalleader
|
2023-01-01 17:45:20 +00:00
|
|
|
|
2023-01-02 09:08:45 +00:00
|
|
|
-- report any warnings & errors
|
|
|
|
Config.spec:report()
|
|
|
|
|
2023-01-01 17:45:20 +00:00
|
|
|
-- setup handlers
|
|
|
|
Util.track("handlers")
|
|
|
|
Handler.setup()
|
2023-10-17 16:44:14 +01:00
|
|
|
M.did_handlers = true
|
2023-01-01 17:45:20 +00:00
|
|
|
Util.track()
|
2022-12-01 10:06:44 +00:00
|
|
|
end
|
|
|
|
|
2023-01-02 09:08:45 +00: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-18 18:39:47 +01:00
|
|
|
local installed = plugin._.installed
|
|
|
|
local has_errors = Plugin.has_errors(plugin)
|
|
|
|
|
2024-06-24 13:14:41 +01:00
|
|
|
if not has_errors and not (installed and not plugin._.build) then
|
2023-01-02 09:08:45 +00:00
|
|
|
for _, colorscheme in ipairs(Config.options.install.colorscheme) do
|
2024-01-23 20:51:00 +00:00
|
|
|
if colorscheme == "default" then
|
|
|
|
break
|
|
|
|
end
|
2023-02-07 21:59:05 +00:00
|
|
|
M.colorscheme(colorscheme)
|
|
|
|
if vim.g.colors_name or pcall(vim.cmd.colorscheme, colorscheme) then
|
2023-01-02 09:08:45 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-05-17 18:36:00 +01:00
|
|
|
Cache.reset()
|
2023-05-17 18:35:16 +01:00
|
|
|
require("lazy.manage").install({ wait = true, lockfile = true, clear = false })
|
2023-04-24 06:54:21 +01:00
|
|
|
-- remove any installed plugins from indexed, so cache will index again
|
2023-01-02 09:08:45 +00:00
|
|
|
for _, p in pairs(Config.plugins) do
|
|
|
|
if p._.installed then
|
2023-03-15 08:01:00 +00:00
|
|
|
Cache.reset(p.dir)
|
2023-01-02 09:08:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- reload plugins
|
|
|
|
Plugin.load()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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-21 13:34:31 +00:00
|
|
|
-- load filetype.lua first since plugins might depend on that
|
2022-12-20 22:35:06 +00:00
|
|
|
M.source(vim.env.VIMRUNTIME .. "/filetype.lua")
|
2022-12-20 19:32:37 +00:00
|
|
|
|
2022-12-21 13:34:31 +00:00
|
|
|
-- backup original rtp
|
2024-06-22 21:18:26 +01:00
|
|
|
local rtp = vim.opt.rtp:get() --[[@as string[] ]]
|
2022-12-14 20:03:53 +00:00
|
|
|
|
2022-12-22 17:46:43 +00: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-26 21:59:02 +00:00
|
|
|
Util.try(function()
|
|
|
|
plugin.init(plugin)
|
|
|
|
end, "Failed to run `init` for **" .. plugin.name .. "**")
|
2022-12-22 17:46:43 +00:00
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
|
|
|
-- 2. load start plugin
|
2022-12-14 20:03:53 +00:00
|
|
|
Util.track({ start = "start" })
|
2022-12-27 16:24:40 +00: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-14 20:03:53 +00:00
|
|
|
M.load(plugin, { start = "start" })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-29 15:03:23 +00:00
|
|
|
-- 3. load plugins from the original rtp, excluding after
|
2022-12-21 13:34:31 +00:00
|
|
|
Util.track({ start = "rtp plugins" })
|
|
|
|
for _, path in ipairs(rtp) do
|
|
|
|
if not path:find("after/?$") then
|
2023-05-28 10:02:35 +01:00
|
|
|
-- these paths don't will already have their ftdetect ran,
|
|
|
|
-- by sourcing filetype.lua above, so skip them
|
2024-06-22 21:18:26 +01:00
|
|
|
M.did_ftdetect[path] = path
|
2022-12-21 13:34:31 +00:00
|
|
|
M.packadd(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Util.track()
|
|
|
|
|
2022-12-22 17:46:43 +00:00
|
|
|
-- 4. load after plugins
|
2022-12-15 22:23:18 +00:00
|
|
|
Util.track({ start = "after" })
|
2024-06-22 21:18:26 +01:00
|
|
|
for _, path in
|
|
|
|
ipairs(vim.opt.rtp:get() --[[@as string[] ]])
|
|
|
|
do
|
2022-12-15 22:23:18 +00:00
|
|
|
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-14 20:03:53 +00:00
|
|
|
Util.track()
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-12-27 16:24:40 +00: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-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}
|
2023-01-06 06:11:50 +00:00
|
|
|
---@param opts? {force:boolean} when force is true, we skip the cond check
|
|
|
|
function M.load(plugins, reason, opts)
|
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-12-25 13:06:41 +00:00
|
|
|
if type(plugin) == "string" then
|
2022-12-28 16:56:20 +00:00
|
|
|
if Config.plugins[plugin] then
|
2022-12-25 13:06:41 +00:00
|
|
|
plugin = Config.plugins[plugin]
|
2023-01-04 10:02:29 +00:00
|
|
|
elseif Config.spec.disabled[plugin] then
|
|
|
|
plugin = nil
|
2022-12-28 16:56:20 +00:00
|
|
|
else
|
|
|
|
Util.error("Plugin " .. plugin .. " not found")
|
|
|
|
plugin = nil
|
2022-12-25 13:06:41 +00:00
|
|
|
end
|
|
|
|
end
|
2022-12-28 16:56:20 +00:00
|
|
|
if plugin and not plugin._.loaded then
|
2023-01-06 06:11:50 +00:00
|
|
|
M._load(plugin, reason, opts)
|
2022-12-26 08:35:19 +00:00
|
|
|
end
|
2022-12-28 16:56:20 +00:00
|
|
|
end
|
|
|
|
end
|
2022-12-26 08:35:19 +00:00
|
|
|
|
2023-02-07 22:52:02 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.deactivate(plugin)
|
2023-02-10 16:47:25 +00:00
|
|
|
if not plugin._.loaded then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-02-07 22:52:02 +00: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-10 16:47:25 +00:00
|
|
|
if plugin.deactivate then
|
2023-02-07 22:52:02 +00:00
|
|
|
Util.try(function()
|
|
|
|
plugin.deactivate(plugin)
|
|
|
|
end, "Failed to deactivate plugin " .. plugin.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable handlers
|
|
|
|
Handler.disable(plugin)
|
|
|
|
|
2023-10-25 19:00:50 +01:00
|
|
|
-- clear plugin properties cache
|
|
|
|
plugin._.cache = nil
|
|
|
|
|
2023-02-07 22:52:02 +00: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 14:19:50 +01:00
|
|
|
---@param plugin LazyPlugin|string
|
2023-02-07 22:52:02 +00:00
|
|
|
function M.reload(plugin)
|
2023-05-27 14:19:50 +01: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-07 22:52:02 +00: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 07:36:09 +01: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-07 22:52:02 +00:00
|
|
|
load = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-27 14:19:11 +01:00
|
|
|
-- reload any vimscript files for this plugin
|
2023-07-22 07:24:24 +01:00
|
|
|
local scripts = vim.fn.getscriptinfo()
|
2023-05-27 14:19:11 +01: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-07 22:52:02 +00:00
|
|
|
if load then
|
|
|
|
M.load(plugin, { start = "reload" })
|
2023-05-27 14:19:11 +01:00
|
|
|
for _, s in ipairs(loaded_scripts) do
|
|
|
|
M.source(s)
|
|
|
|
end
|
2023-02-07 22:52:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-28 16:56:20 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param reason {[string]:string}
|
2023-01-06 06:11:50 +00:00
|
|
|
---@param opts? {force:boolean} when force is true, we skip the cond check
|
|
|
|
function M._load(plugin, reason, opts)
|
2022-12-28 16:56:20 +00:00
|
|
|
if not plugin._.installed then
|
|
|
|
return Util.error("Plugin " .. plugin.name .. " is not installed")
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2023-07-06 14:30:01 +01:00
|
|
|
if plugin._.cond == false and not (opts and opts.force) then
|
|
|
|
return
|
2022-12-28 16:56:20 +00:00
|
|
|
end
|
2022-11-23 15:07:57 +00:00
|
|
|
|
2023-09-27 11:39:39 +01:00
|
|
|
if not Handler.did_setup then
|
|
|
|
Util.try(function()
|
|
|
|
Handler.enable(plugin)
|
|
|
|
end, "Failed to setup handlers for " .. plugin.name)
|
|
|
|
end
|
|
|
|
|
2022-12-28 16:56:20 +00: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-20 21:33:47 +00:00
|
|
|
|
2022-12-28 16:56:20 +00:00
|
|
|
table.insert(M.loading, plugin)
|
2022-12-02 08:22:15 +00:00
|
|
|
|
2022-12-28 16:56:20 +00:00
|
|
|
Util.track({ plugin = plugin.name, start = reason.start })
|
|
|
|
Handler.disable(plugin)
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2022-12-29 15:03:23 +00:00
|
|
|
M.add_to_rtp(plugin)
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2024-06-24 13:14:41 +01:00
|
|
|
if plugin._.pkg and plugin._.pkg.source == "rockspec" then
|
|
|
|
M.add_to_luapath(plugin)
|
|
|
|
end
|
|
|
|
|
2022-12-28 16:56:20 +00:00
|
|
|
if plugin.dependencies then
|
|
|
|
Util.try(function()
|
|
|
|
M.load(plugin.dependencies, {})
|
|
|
|
end, "Failed to load deps for " .. plugin.name)
|
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
|
2023-01-05 18:43:47 +00:00
|
|
|
M.packadd(plugin.dir)
|
2023-01-08 14:01:49 +00:00
|
|
|
if plugin.config or plugin.opts then
|
2022-12-28 16:56:20 +00:00
|
|
|
M.config(plugin)
|
2022-11-20 21:34:55 +00:00
|
|
|
end
|
2022-12-28 16:56:20 +00:00
|
|
|
|
|
|
|
plugin._.loaded.time = Util.track().time
|
|
|
|
table.remove(M.loading)
|
|
|
|
vim.schedule(function()
|
2023-07-12 16:21:32 +01:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyLoad", modeline = false, data = plugin.name })
|
2023-01-13 08:00:15 +00:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyRender", modeline = false })
|
2022-12-28 16:56:20 +00:00
|
|
|
end)
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-12-22 21:41:44 +00:00
|
|
|
--- runs plugin config
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.config(plugin)
|
|
|
|
local fn
|
|
|
|
if type(plugin.config) == "function" then
|
2022-12-26 21:59:02 +00:00
|
|
|
fn = function()
|
2023-01-15 19:00:07 +00:00
|
|
|
local opts = Plugin.values(plugin, "opts", false)
|
2023-01-12 12:07:51 +00:00
|
|
|
plugin.config(plugin, opts)
|
2022-12-26 21:59:02 +00:00
|
|
|
end
|
2022-12-22 21:41:44 +00:00
|
|
|
else
|
2023-02-07 22:52:02 +00:00
|
|
|
local main = M.get_main(plugin)
|
|
|
|
if main then
|
2022-12-22 21:41:44 +00:00
|
|
|
fn = function()
|
2023-01-15 19:00:07 +00:00
|
|
|
local opts = Plugin.values(plugin, "opts", false)
|
2023-02-07 22:52:02 +00:00
|
|
|
require(main).setup(opts)
|
2022-12-22 21:41:44 +00: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-07 22:52:02 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.get_main(plugin)
|
|
|
|
if plugin.main then
|
|
|
|
return plugin.main
|
|
|
|
end
|
2023-05-13 15:09:48 +01:00
|
|
|
if plugin.name ~= "mini.nvim" and plugin.name:match("^mini%..*$") then
|
|
|
|
return plugin.name
|
|
|
|
end
|
2023-02-07 22:52:02 +00:00
|
|
|
local normname = Util.normname(plugin.name)
|
|
|
|
---@type string[]
|
|
|
|
local mods = {}
|
2023-03-15 14:10:44 +00:00
|
|
|
for _, mod in ipairs(Cache.find("*", { all = true, rtp = false, paths = { plugin.dir } })) do
|
|
|
|
local modname = mod.modname
|
2023-02-07 22:52:02 +00: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 15:09:48 +01:00
|
|
|
|
2023-02-07 22:52:02 +00:00
|
|
|
return #mods == 1 and mods[1] or nil
|
|
|
|
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-29 21:40:15 +00: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 13:45:50 +00:00
|
|
|
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)
|
2022-12-21 15:27:56 +00:00
|
|
|
if (t == "file" or t == "link") 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
|
2022-12-20 22:35:06 +00:00
|
|
|
M.source(path)
|
2022-12-17 12:03:34 +00:00
|
|
|
end
|
2022-11-20 21:33:47 +00:00
|
|
|
end
|
|
|
|
|
2022-12-29 15:03:23 +00: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 12:23:25 +01:00
|
|
|
if Util.is_win then
|
2022-12-29 22:06:37 +00:00
|
|
|
path = Util.norm(path)
|
|
|
|
end
|
2022-12-29 15:03:23 +00: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 07:58:36 +00:00
|
|
|
if vim.uv.fs_stat(after) then
|
2022-12-29 15:03:23 +00:00
|
|
|
table.insert(rtp, idx_after or (#rtp + 1), after)
|
|
|
|
end
|
|
|
|
|
2024-06-04 05:58:19 +01:00
|
|
|
---@type vim.Option
|
2022-12-29 15:03:23 +00:00
|
|
|
vim.opt.rtp = rtp
|
|
|
|
end
|
|
|
|
|
2024-06-24 13:14:41 +01: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-20 22:35:06 +00:00
|
|
|
function M.source(path)
|
|
|
|
Util.track({ runtime = path })
|
|
|
|
Util.try(function()
|
2023-01-06 18:16:39 +00:00
|
|
|
vim.cmd("source " .. path)
|
2022-12-20 22:35:06 +00:00
|
|
|
end, "Failed to source `" .. path .. "`")
|
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
|
2022-12-22 12:49:00 +00: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 07:58:36 +00:00
|
|
|
if vim.uv.fs_stat(path) then
|
2022-12-22 12:49:00 +00:00
|
|
|
return M.load(plugin, { colorscheme = name })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
function M.auto_load(modname, modpath)
|
2024-06-24 20:56:43 +01:00
|
|
|
local plugin = Plugin.find(modpath, { fast = not M.did_handlers })
|
2024-06-24 22:42:51 +01:00
|
|
|
if plugin then
|
2023-10-15 07:51:54 +01:00
|
|
|
plugin._.rtp_loaded = true
|
2023-10-17 16:44:14 +01: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 10:00:56 +00: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 10:17:54 +00:00
|
|
|
if plugin._.cond == false then
|
|
|
|
error("You're trying to load `" .. plugin.name .. "` for which `cond==false`")
|
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param modname string
|
|
|
|
function M.loader(modname)
|
2024-06-24 20:56:43 +01:00
|
|
|
local paths, cached = Util.get_unloaded_rtp(modname, { cache = true })
|
2023-03-15 14:10:44 +00:00
|
|
|
local ret = Cache.find(modname, { rtp = false, paths = paths })[1]
|
2024-06-24 20:56:43 +01: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 14:10:44 +00:00
|
|
|
if ret then
|
2023-02-14 10:00:56 +00:00
|
|
|
local mod = package.loaded[modname]
|
2024-07-21 11:41:54 +01:00
|
|
|
if type(mod) ~= "table" then
|
|
|
|
mod = loadfile(ret.modpath, nil, nil, ret.stat)()
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
2024-07-21 11:41:54 +01:00
|
|
|
package.loaded[modname] = mod
|
|
|
|
M.auto_load(modname, ret.modpath)
|
2023-03-15 14:10:44 +00:00
|
|
|
-- selene: allow(incorrect_standard_library_use)
|
2024-07-21 11:41:54 +01:00
|
|
|
return function()
|
|
|
|
return mod
|
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-20 21:33:47 +00:00
|
|
|
return M
|