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")
|
|
|
|
|
2023-10-07 08:48:53 +01:00
|
|
|
---@class LazyEventOpts
|
|
|
|
---@field event string
|
|
|
|
---@field pattern? string
|
2023-10-07 10:15:12 +01:00
|
|
|
---@field group? string
|
2023-10-07 08:48:53 +01:00
|
|
|
---@field exclude? string[]
|
|
|
|
---@field data? any
|
2023-10-07 10:15:12 +01:00
|
|
|
---@field buffer? number
|
2023-10-07 08:48:53 +01:00
|
|
|
|
2022-12-05 13:45:50 +00:00
|
|
|
---@class LazyEventHandler:LazyHandler
|
|
|
|
---@field events table<string,true>
|
|
|
|
---@field group number
|
|
|
|
local M = {}
|
|
|
|
|
2023-10-07 10:15:12 +01:00
|
|
|
-- Event dependencies
|
|
|
|
M.triggers = {
|
|
|
|
FileType = "BufReadPost",
|
|
|
|
BufReadPost = "BufReadPre",
|
|
|
|
}
|
|
|
|
|
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
|
2023-10-06 18:42:19 +01:00
|
|
|
local done = false
|
2022-12-05 13:45:50 +00:00
|
|
|
vim.api.nvim_create_autocmd(event, {
|
|
|
|
group = self.group,
|
|
|
|
once = true,
|
|
|
|
pattern = pattern,
|
2023-07-09 08:44:08 +01:00
|
|
|
callback = function(ev)
|
2023-10-06 18:42:19 +01:00
|
|
|
if done or not self.active[value] then
|
2022-12-05 13:45:50 +00:00
|
|
|
return
|
|
|
|
end
|
2023-10-06 18:42:19 +01:00
|
|
|
done = true
|
2022-12-16 08:13:08 +00:00
|
|
|
Util.track({ [self.type] = value })
|
2023-10-07 10:15:12 +01:00
|
|
|
|
|
|
|
local state = M.get_state(ev.event, pattern, ev.buf, ev.data)
|
|
|
|
|
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 })
|
2023-10-07 10:15:12 +01:00
|
|
|
|
2022-12-29 16:26:38 +00:00
|
|
|
-- check if any plugin created an event handler for this event and fire the group
|
2023-10-07 10:15:12 +01:00
|
|
|
for _, s in ipairs(state) do
|
|
|
|
M.trigger(s)
|
|
|
|
end
|
2022-12-05 13:45:50 +00:00
|
|
|
Util.track()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-10-07 10:15:12 +01:00
|
|
|
-- Get the current state of the event and all the events that will be fired
|
|
|
|
---@param event string
|
|
|
|
---@param pattern? string
|
|
|
|
---@param buf number
|
|
|
|
---@param data any
|
|
|
|
function M.get_state(event, pattern, buf, data)
|
|
|
|
local state = {} ---@type LazyEventOpts[]
|
|
|
|
while event do
|
|
|
|
table.insert(state, 1, {
|
|
|
|
event = event,
|
|
|
|
pattern = pattern,
|
|
|
|
exclude = event ~= "FileType" and M.get_augroups(event) or nil,
|
|
|
|
buffer = buf,
|
|
|
|
data = data,
|
|
|
|
})
|
|
|
|
data = nil -- only pass the data to the first event
|
|
|
|
if event == "FileType" then
|
|
|
|
pattern = nil -- only use the pattern for the first event
|
|
|
|
end
|
|
|
|
event = M.triggers[event]
|
|
|
|
end
|
|
|
|
return state
|
|
|
|
end
|
|
|
|
|
2022-12-16 08:13:08 +00:00
|
|
|
---@param value string
|
|
|
|
function M:_event(value)
|
2023-10-06 14:39:18 +01:00
|
|
|
if value == "VeryLazy" then
|
|
|
|
return "User VeryLazy"
|
|
|
|
elseif value == "BufRead" then
|
|
|
|
return "BufReadPost"
|
|
|
|
end
|
|
|
|
return value
|
2022-12-16 08:13:08 +00:00
|
|
|
end
|
|
|
|
|
2022-12-05 13:45:50 +00:00
|
|
|
-- Get all augroups for the events
|
|
|
|
---@param event string
|
2023-10-07 10:15:12 +01:00
|
|
|
function M.get_augroups(event)
|
2023-10-07 08:48:53 +01:00
|
|
|
local groups = {} ---@type string[]
|
2023-10-07 10:15:12 +01:00
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event })) do
|
2023-10-07 08:48:53 +01:00
|
|
|
if autocmd.group_name then
|
|
|
|
table.insert(groups, autocmd.group_name)
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return groups
|
|
|
|
end
|
|
|
|
|
2023-10-07 10:15:12 +01:00
|
|
|
-- Trigger an event. When a group is given, only the events in that group will be triggered.
|
|
|
|
-- When exclude is set, the events in those groups will be skipped.
|
2023-10-07 08:48:53 +01:00
|
|
|
---@param opts LazyEventOpts
|
2023-10-06 14:39:18 +01:00
|
|
|
function M.trigger(opts)
|
2023-10-07 10:15:12 +01:00
|
|
|
if opts.group or opts.exclude == nil then
|
|
|
|
return M._trigger(opts)
|
|
|
|
end
|
2023-10-06 14:39:18 +01:00
|
|
|
local done = {} ---@type table<string,true>
|
2023-10-07 10:15:12 +01:00
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event })) do
|
2023-10-06 14:39:18 +01:00
|
|
|
local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string
|
2023-10-07 08:48:53 +01:00
|
|
|
local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name))
|
2023-10-06 14:39:18 +01:00
|
|
|
done[id] = true
|
|
|
|
if autocmd.group and not skip then
|
2023-10-07 10:15:12 +01:00
|
|
|
opts.group = autocmd.group_name
|
|
|
|
M._trigger(opts)
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-07 10:15:12 +01:00
|
|
|
-- Trigger an event
|
|
|
|
---@param opts LazyEventOpts
|
|
|
|
function M._trigger(opts)
|
|
|
|
if Config.options.debug then
|
|
|
|
Util.info({
|
|
|
|
"# Firing Events",
|
|
|
|
" - **event:** " .. opts.event,
|
|
|
|
opts.pattern and (" - **pattern:** " .. opts.pattern),
|
|
|
|
opts.group and (" - **group:** " .. opts.group),
|
|
|
|
opts.buffer and (" - **buffer:** " .. opts.buffer),
|
|
|
|
})
|
|
|
|
end
|
|
|
|
Util.track({ event = opts.group or opts.event })
|
|
|
|
Util.try(function()
|
|
|
|
vim.api.nvim_exec_autocmds(opts.event, {
|
|
|
|
-- pattern = opts.pattern,
|
|
|
|
buffer = opts.buffer,
|
|
|
|
group = opts.group,
|
|
|
|
modeline = false,
|
|
|
|
data = opts.data,
|
|
|
|
})
|
|
|
|
Util.track()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-12-05 13:45:50 +00:00
|
|
|
return M
|