2022-12-05 13:45:50 +00:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Loader = require("lazy.core.loader")
|
|
|
|
|
|
|
|
---@class LazyEventHandler:LazyHandler
|
|
|
|
---@field events table<string,true>
|
|
|
|
---@field group number
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.trigger_events = {
|
|
|
|
BufRead = { "BufReadPre", "BufRead" },
|
|
|
|
BufReadPost = { "BufReadPre", "BufRead", "BufReadPost" },
|
|
|
|
}
|
2022-12-16 08:13:08 +00:00
|
|
|
M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
|
2022-12-05 13:45:50 +00:00
|
|
|
|
|
|
|
---@param value string
|
2022-12-16 08:13:08 +00:00
|
|
|
function M:_add(value)
|
|
|
|
local event_spec = self:_event(value)
|
2022-12-05 13:45:50 +00:00
|
|
|
---@type string?, string?
|
|
|
|
local event, pattern = event_spec:match("^(%w+)%s+(.*)$")
|
|
|
|
event = event or event_spec
|
|
|
|
vim.api.nvim_create_autocmd(event, {
|
|
|
|
group = self.group,
|
|
|
|
once = true,
|
|
|
|
pattern = pattern,
|
2023-07-09 08:44:08 +01:00
|
|
|
callback = function(ev)
|
2022-12-16 08:13:08 +00:00
|
|
|
if not self.active[value] then
|
2022-12-05 13:45:50 +00:00
|
|
|
return
|
|
|
|
end
|
2022-12-16 08:13:08 +00:00
|
|
|
Util.track({ [self.type] = value })
|
2023-06-06 14:05:51 +01:00
|
|
|
local groups = M.get_augroups(event, pattern)
|
2022-12-05 13:45:50 +00:00
|
|
|
-- load the plugins
|
2022-12-16 08:13:08 +00:00
|
|
|
Loader.load(self.active[value], { [self.type] = value })
|
2022-12-29 16:26:38 +00:00
|
|
|
-- check if any plugin created an event handler for this event and fire the group
|
2023-07-09 08:44:08 +01:00
|
|
|
self:trigger(event, pattern, groups, ev.data)
|
2022-12-05 13:45:50 +00:00
|
|
|
Util.track()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-12-16 08:13:08 +00:00
|
|
|
---@param value string
|
|
|
|
function M:_event(value)
|
|
|
|
return value == "VeryLazy" and "User VeryLazy" or value
|
|
|
|
end
|
|
|
|
|
2022-12-05 13:45:50 +00:00
|
|
|
-- Get all augroups for the events
|
|
|
|
---@param event string
|
2023-06-06 14:05:51 +01:00
|
|
|
---@param pattern? string
|
|
|
|
function M.get_augroups(event, pattern)
|
2022-12-05 13:45:50 +00:00
|
|
|
local events = M.trigger_events[event] or { event }
|
|
|
|
---@type table<string,true>
|
|
|
|
local groups = {}
|
2023-06-06 14:05:51 +01:00
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = events, pattern = pattern })) do
|
2022-12-05 13:45:50 +00:00
|
|
|
if autocmd.group then
|
|
|
|
groups[autocmd.group] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return groups
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param event string|string[]
|
|
|
|
---@param pattern? string
|
|
|
|
---@param groups table<string,true>
|
2023-07-09 08:44:08 +01:00
|
|
|
function M:trigger(event, pattern, groups, data)
|
2022-12-05 13:45:50 +00:00
|
|
|
local events = M.trigger_events[event] or { event }
|
|
|
|
---@cast events string[]
|
|
|
|
for _, e in ipairs(events) do
|
2023-06-06 14:05:51 +01:00
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = e, pattern = pattern })) do
|
2022-12-05 13:45:50 +00:00
|
|
|
if autocmd.event == e and autocmd.group and not groups[autocmd.group] then
|
|
|
|
if Config.options.debug then
|
|
|
|
Util.info({
|
|
|
|
"# Firing Events",
|
|
|
|
" - **group:** `" .. autocmd.group_name .. "`",
|
|
|
|
" - **event:** " .. autocmd.event,
|
2023-06-06 14:05:51 +01:00
|
|
|
pattern and (" - **pattern:** " .. pattern),
|
2022-12-05 13:45:50 +00:00
|
|
|
})
|
|
|
|
end
|
2023-01-02 10:26:54 +00:00
|
|
|
Util.track({ event = autocmd.group_name })
|
2022-12-05 13:45:50 +00:00
|
|
|
Util.try(function()
|
2023-07-09 08:44:08 +01:00
|
|
|
vim.api.nvim_exec_autocmds(autocmd.event, { group = autocmd.group, modeline = false, data = data })
|
2023-01-02 10:26:54 +00:00
|
|
|
Util.track()
|
2022-12-05 13:45:50 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|