mirror of
https://github.com/folke/lazy.nvim.git
synced 2025-04-20 05:16:45 +00:00
refactor: move handlers to its own file
This commit is contained in:
parent
870d8924f7
commit
05a0da532b
5 changed files with 169 additions and 180 deletions
|
@ -3,148 +3,30 @@ local Config = require("lazy.core.config")
|
|||
|
||||
local M = {}
|
||||
|
||||
---@alias LoaderType "event"|"ft"|"module"|"keys"|"cmd"|"init"
|
||||
---@type LoaderType[]
|
||||
M.types = {
|
||||
"event",
|
||||
"ft",
|
||||
"module",
|
||||
"keys",
|
||||
"cmd",
|
||||
}
|
||||
|
||||
---@type table<LoaderType, table<string, string[]>>|{init: string[]}
|
||||
M.loaders = nil
|
||||
---@type LazyPlugin[]
|
||||
M.loading = {}
|
||||
|
||||
function M.get_loaders()
|
||||
---@type table<LoaderType, table<string, string[]>>|{init: string[]}
|
||||
local loaders = { init = {} }
|
||||
for _, lt in ipairs(M.types) do
|
||||
loaders[lt] = {}
|
||||
end
|
||||
for _, plugin in pairs(Config.plugins) do
|
||||
if plugin.init or (plugin.opt == false) then
|
||||
table.insert(loaders.init, plugin.name)
|
||||
end
|
||||
for _, lt in ipairs(M.types) do
|
||||
if plugin[lt] then
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
for _, loader in ipairs(type(plugin[lt]) == "table" and plugin[lt] or { plugin[lt] }) do
|
||||
loaders[lt][loader] = loaders[lt][loader] or {}
|
||||
table.insert(loaders[lt][loader], plugin.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return loaders
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
M.loaders = M.loaders or M.get_loaders()
|
||||
|
||||
local group = vim.api.nvim_create_augroup("lazy_loader", {
|
||||
clear = true,
|
||||
})
|
||||
|
||||
-- modules
|
||||
table.insert(package.loaders, 2, M.module)
|
||||
|
||||
-- events
|
||||
Util.track("loader_events")
|
||||
for event, plugins in pairs(M.loaders.event) do
|
||||
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
|
||||
M.load(plugins, { event = event })
|
||||
else
|
||||
local user_event = event:match("User (.*)")
|
||||
vim.api.nvim_create_autocmd(user_event and "User" or event, {
|
||||
once = true,
|
||||
group = group,
|
||||
pattern = user_event,
|
||||
callback = function()
|
||||
Util.track("event: " .. (user_event or event))
|
||||
M.load(plugins, { event = event })
|
||||
Util.track()
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
Util.track()
|
||||
|
||||
-- filetypes
|
||||
Util.track("loader_filetypes")
|
||||
for ft, plugins in pairs(M.loaders.ft) do
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
once = true,
|
||||
pattern = ft,
|
||||
group = group,
|
||||
callback = function()
|
||||
Util.track("filetype: " .. ft)
|
||||
M.load(plugins, { ft = ft })
|
||||
Util.track()
|
||||
end,
|
||||
})
|
||||
end
|
||||
Util.track()
|
||||
|
||||
-- keys
|
||||
Util.track("loader_keys")
|
||||
for keys, plugins in pairs(M.loaders.keys or {}) do
|
||||
vim.keymap.set("n", keys, function()
|
||||
vim.keymap.del("n", keys)
|
||||
Util.track("keys: " .. keys)
|
||||
M.load(plugins, { keys = keys })
|
||||
vim.api.nvim_input(keys)
|
||||
Util.track()
|
||||
end)
|
||||
end
|
||||
Util.track()
|
||||
|
||||
-- commands
|
||||
Util.track("loader_commands")
|
||||
for cmd, plugins in pairs(M.loaders.cmd or {}) do
|
||||
local function _load(complete)
|
||||
vim.api.nvim_del_user_command(cmd)
|
||||
if complete then
|
||||
Util.track("cmd-complete: " .. cmd)
|
||||
else
|
||||
Util.track("cmd: " .. cmd)
|
||||
local Handler = require("lazy.core.handler")
|
||||
for t, handler in pairs(Handler.handlers) do
|
||||
Util.track(t)
|
||||
---@type LazyPlugin[]
|
||||
local plugins = {}
|
||||
for _, plugin in pairs(Config.plugins) do
|
||||
if plugin[t] ~= nil then
|
||||
table.insert(plugins, plugin)
|
||||
end
|
||||
M.load(plugins, { cmd = cmd })
|
||||
Util.track()
|
||||
end
|
||||
vim.api.nvim_create_user_command(cmd, function(event)
|
||||
_load()
|
||||
vim.cmd(
|
||||
("%s %s%s%s %s"):format(
|
||||
event.mods or "",
|
||||
event.line1 == event.line2 and "" or event.line1 .. "," .. event.line2,
|
||||
cmd,
|
||||
event.bang and "!" or "",
|
||||
event.args or ""
|
||||
)
|
||||
)
|
||||
end, {
|
||||
bang = true,
|
||||
nargs = "*",
|
||||
complete = function()
|
||||
_load(true)
|
||||
-- HACK: trick Neovim to show the newly loaded command completion
|
||||
vim.api.nvim_input("<space><bs><tab>")
|
||||
end,
|
||||
})
|
||||
if #plugins > 0 then
|
||||
handler(plugins)
|
||||
end
|
||||
Util.track()
|
||||
end
|
||||
Util.track()
|
||||
end
|
||||
|
||||
function M.init_plugins()
|
||||
Util.track("plugin_init")
|
||||
for _, name in ipairs(M.loaders.init) do
|
||||
local plugin = Config.plugins[name]
|
||||
if not plugin then
|
||||
error(name)
|
||||
end
|
||||
for _, plugin in pairs(Config.plugins) do
|
||||
if plugin.init then
|
||||
Util.track(plugin.name)
|
||||
Util.try(plugin.init, "Failed to run `init` for **" .. plugin.name .. "**")
|
||||
|
@ -157,34 +39,7 @@ function M.init_plugins()
|
|||
Util.track()
|
||||
end
|
||||
|
||||
---@param modname string
|
||||
function M.module(modname)
|
||||
local idx = modname:find(".", 1, true) or #modname + 1
|
||||
while idx do
|
||||
local name = modname:sub(1, idx - 1)
|
||||
local plugins = M.loaders.module[name]
|
||||
if plugins then
|
||||
M.loaders.module[name] = nil
|
||||
local reason = { require = modname }
|
||||
if #M.loading == 0 then
|
||||
local f = 3
|
||||
while not reason.source do
|
||||
local info = debug.getinfo(f, "S")
|
||||
if not info then
|
||||
break
|
||||
end
|
||||
if info.what ~= "C" then
|
||||
reason.source = info.source:sub(2)
|
||||
end
|
||||
f = f + 1
|
||||
end
|
||||
end
|
||||
M.load(plugins, reason)
|
||||
end
|
||||
idx = modname:find(".", idx + 1, true)
|
||||
end
|
||||
end
|
||||
|
||||
---@class Loader
|
||||
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
|
||||
---@param reason {[string]:string}
|
||||
---@param opts? {load_start: boolean}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue