2022-11-26 20:29:40 +00:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Loader = require("lazy.core.loader")
|
2022-11-29 23:18:59 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-26 20:29:40 +00:00
|
|
|
|
2022-11-28 06:36:32 +00:00
|
|
|
---@class LazyPluginHandlers
|
|
|
|
---@field event? string|string[]
|
|
|
|
---@field cmd? string|string[]
|
|
|
|
---@field ft? string|string[]
|
|
|
|
---@field keys? string|string[]
|
|
|
|
|
2022-11-26 20:29:40 +00:00
|
|
|
local M = {}
|
|
|
|
|
2022-11-26 21:04:32 +00:00
|
|
|
---@alias LazyHandler fun(grouped:table<string, string[]>)
|
|
|
|
|
2022-11-29 23:18:59 +00:00
|
|
|
function M.setup()
|
|
|
|
for key, handler in pairs(M.handlers) do
|
|
|
|
---@type table<string, string[]>
|
|
|
|
local group = {}
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if plugin[key] then
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
|
|
|
|
group[value] = group[value] or {}
|
|
|
|
table.insert(group[value], plugin.name)
|
2022-11-26 21:04:32 +00:00
|
|
|
end
|
|
|
|
end
|
2022-11-26 20:29:40 +00:00
|
|
|
end
|
2022-11-29 23:18:59 +00:00
|
|
|
handler(group)
|
2022-11-26 20:29:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-02 16:01:05 +00:00
|
|
|
---@param events string|string[]
|
|
|
|
---@param pattern? string
|
|
|
|
function M.get_augroups(events, pattern)
|
|
|
|
-- Check for all autocmd groups listening for the events
|
|
|
|
---@type table<string,true>
|
|
|
|
local groups = {}
|
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = events, pattern = pattern })) do
|
|
|
|
if autocmd.group then
|
|
|
|
groups[autocmd.group] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return groups
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param groups table<string,true>
|
|
|
|
---@param events string|string[]
|
|
|
|
---@param pattern? string
|
|
|
|
function M.trigger(groups, events, pattern)
|
|
|
|
events = type(events) == "string" and { events } or events
|
|
|
|
---@cast events string[]
|
|
|
|
for _, event in ipairs(events) do
|
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event, pattern = pattern })) do
|
|
|
|
if autocmd.event == event and autocmd.group and not groups[autocmd.group] then
|
|
|
|
if Config.options.debug then
|
|
|
|
local lines = {
|
|
|
|
"# Firing Events",
|
|
|
|
" - **event:** " .. autocmd.event,
|
|
|
|
" - **group:** `" .. autocmd.group_name .. "`",
|
|
|
|
}
|
|
|
|
if pattern then
|
|
|
|
table.insert(lines, 2, "- **pattern:** " .. pattern)
|
|
|
|
end
|
|
|
|
Util.info(lines)
|
|
|
|
end
|
|
|
|
vim.api.nvim_exec_autocmds(autocmd.event, { group = autocmd.group, modeline = false })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-26 20:29:40 +00:00
|
|
|
---@type table<string, LazyHandler>
|
|
|
|
M.handlers = {}
|
2022-11-26 21:04:32 +00:00
|
|
|
function M.handlers.event(grouped)
|
2022-11-26 20:29:40 +00:00
|
|
|
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
|
2022-12-02 16:01:05 +00:00
|
|
|
for event_spec, plugins in pairs(grouped) do
|
|
|
|
if event_spec == "VeryLazy" then
|
|
|
|
event_spec = "User VeryLazy"
|
|
|
|
end
|
|
|
|
if event_spec == "VimEnter" and vim.v.vim_did_enter == 1 then
|
|
|
|
Loader.load(plugins, { event = event_spec })
|
2022-11-26 20:29:40 +00:00
|
|
|
else
|
2022-12-02 16:01:05 +00:00
|
|
|
local event, pattern = event_spec:match("^(%w+)%s+(.*)$")
|
|
|
|
event = event or event_spec
|
|
|
|
vim.api.nvim_create_autocmd(event, {
|
2022-11-26 20:29:40 +00:00
|
|
|
group = group,
|
|
|
|
once = true,
|
|
|
|
pattern = pattern,
|
|
|
|
callback = function()
|
2022-12-02 16:01:05 +00:00
|
|
|
Util.track({ event = event_spec })
|
|
|
|
local events = { event }
|
|
|
|
if event == "BufRead" then
|
|
|
|
events = { "BufReadPre", "BufRead" }
|
|
|
|
elseif event == "BufReadPost" then
|
|
|
|
events = { "BufReadPre", "BufRead", "BufReadPost" }
|
|
|
|
end
|
|
|
|
|
|
|
|
local groups = M.get_augroups(events, pattern)
|
|
|
|
|
|
|
|
-- load the plugins
|
|
|
|
Loader.load(plugins, { event = event_spec })
|
|
|
|
|
|
|
|
-- check if any plugin created an event handler for this event and fire the group
|
|
|
|
M.trigger(groups, events, pattern)
|
2022-11-26 20:29:40 +00:00
|
|
|
Util.track()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-26 21:04:32 +00:00
|
|
|
function M.handlers.keys(grouped)
|
|
|
|
for keys, plugins in pairs(grouped) do
|
2022-11-26 20:29:40 +00:00
|
|
|
vim.keymap.set("n", keys, function()
|
|
|
|
vim.keymap.del("n", keys)
|
2022-11-29 11:02:25 +00:00
|
|
|
Util.track({ keys = keys })
|
2022-11-26 20:29:40 +00:00
|
|
|
Loader.load(plugins, { keys = keys })
|
|
|
|
vim.api.nvim_input(keys)
|
|
|
|
Util.track()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-26 21:04:32 +00:00
|
|
|
function M.handlers.ft(grouped)
|
2022-11-26 20:29:40 +00:00
|
|
|
local group = vim.api.nvim_create_augroup("lazy_handler_ft", { clear = true })
|
2022-11-26 21:04:32 +00:00
|
|
|
for ft, plugins in pairs(grouped) do
|
2022-11-26 20:29:40 +00:00
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
|
|
|
once = true,
|
|
|
|
pattern = ft,
|
|
|
|
group = group,
|
|
|
|
callback = function()
|
2022-11-29 11:02:25 +00:00
|
|
|
Util.track({ ft = ft })
|
2022-12-02 16:01:05 +00:00
|
|
|
local groups = M.get_augroups("FileType", ft)
|
2022-11-26 20:29:40 +00:00
|
|
|
Loader.load(plugins, { ft = ft })
|
2022-12-02 16:01:05 +00:00
|
|
|
M.trigger(groups, "FileType", ft)
|
2022-11-26 20:29:40 +00:00
|
|
|
Util.track()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-26 21:04:32 +00:00
|
|
|
function M.handlers.cmd(grouped)
|
|
|
|
for cmd, plugins in pairs(grouped) do
|
2022-11-29 11:02:25 +00:00
|
|
|
local function _load()
|
2022-11-26 20:29:40 +00:00
|
|
|
vim.api.nvim_del_user_command(cmd)
|
2022-11-29 11:02:25 +00:00
|
|
|
Util.track({ cmd = cmd })
|
2022-11-26 20:29:40 +00:00
|
|
|
Loader.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()
|
2022-11-29 11:02:25 +00:00
|
|
|
_load()
|
2022-11-26 20:29:40 +00:00
|
|
|
-- HACK: trick Neovim to show the newly loaded command completion
|
|
|
|
vim.api.nvim_input("<space><bs><tab>")
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|