2022-12-05 21:45:50 +08:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Loader = require("lazy.core.loader")
|
2023-10-09 17:25:42 +08:00
|
|
|
local Util = require("lazy.core.util")
|
2022-12-05 21:45:50 +08:00
|
|
|
|
2023-10-07 15:48:53 +08:00
|
|
|
---@class LazyEventOpts
|
|
|
|
---@field event string
|
2023-10-07 17:15:12 +08:00
|
|
|
---@field group? string
|
2023-10-07 15:48:53 +08:00
|
|
|
---@field exclude? string[]
|
|
|
|
---@field data? any
|
2023-10-07 17:15:12 +08:00
|
|
|
---@field buffer? number
|
2023-10-07 15:48:53 +08:00
|
|
|
|
2023-10-11 20:24:18 +08:00
|
|
|
---@alias LazyEvent {id:string, event:string[]|string, pattern?:string[]|string}
|
|
|
|
---@alias LazyEventSpec string|{event?:string|string[], pattern?:string|string[]}|string[]
|
|
|
|
|
2022-12-05 21:45:50 +08:00
|
|
|
---@class LazyEventHandler:LazyHandler
|
|
|
|
---@field events table<string,true>
|
|
|
|
---@field group number
|
|
|
|
local M = {}
|
|
|
|
|
2023-10-07 17:15:12 +08:00
|
|
|
-- Event dependencies
|
|
|
|
M.triggers = {
|
|
|
|
FileType = "BufReadPost",
|
|
|
|
BufReadPost = "BufReadPre",
|
|
|
|
}
|
|
|
|
|
2023-10-11 20:24:40 +08:00
|
|
|
-- A table of mappings for custom events
|
|
|
|
-- Can be used by distros to add custom events (see usage in LazyVim)
|
|
|
|
---@type table<string, LazyEvent>
|
|
|
|
M.mappings = {
|
|
|
|
VeryLazy = { id = "VeryLazy", event = "User", pattern = "VeryLazy" },
|
|
|
|
-- Example:
|
|
|
|
-- LazyFile = { id = "LazyFile", event = { "BufReadPost", "BufNewFile", "BufWritePre" } },
|
|
|
|
}
|
|
|
|
M.mappings["User VeryLazy"] = M.mappings.VeryLazy
|
|
|
|
|
2022-12-16 16:13:08 +08:00
|
|
|
M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
|
2022-12-05 21:45:50 +08:00
|
|
|
|
2023-10-11 20:24:18 +08:00
|
|
|
---@param spec LazyEventSpec
|
|
|
|
---@return LazyEvent
|
2023-10-17 04:34:44 +08:00
|
|
|
function M:_parse(spec)
|
2023-10-11 20:24:18 +08:00
|
|
|
local ret = M.mappings[spec] --[[@as LazyEvent?]]
|
|
|
|
if ret then
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
if type(spec) == "string" then
|
|
|
|
local event, pattern = spec:match("^(%w+)%s+(.*)$")
|
|
|
|
event = event or spec
|
|
|
|
return { id = spec, event = event, pattern = pattern }
|
|
|
|
elseif Util.is_list(spec) then
|
|
|
|
ret = { id = table.concat(spec, "|"), event = spec }
|
|
|
|
else
|
|
|
|
ret = spec --[[@as LazyEvent]]
|
|
|
|
if not ret.id then
|
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
|
|
|
|
ret.id = type(ret.event) == "string" and ret.event or table.concat(ret.event, "|")
|
|
|
|
if ret.pattern then
|
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
|
|
|
|
ret.id = ret.id .. " " .. (type(ret.pattern) == "string" and ret.pattern or table.concat(ret.pattern, ", "))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param event LazyEvent
|
|
|
|
function M:_add(event)
|
2023-10-07 01:42:19 +08:00
|
|
|
local done = false
|
2023-10-11 20:24:18 +08:00
|
|
|
vim.api.nvim_create_autocmd(event.event, {
|
2022-12-05 21:45:50 +08:00
|
|
|
group = self.group,
|
|
|
|
once = true,
|
2023-10-11 20:24:18 +08:00
|
|
|
pattern = event.pattern,
|
2023-07-09 15:44:08 +08:00
|
|
|
callback = function(ev)
|
2023-10-11 20:24:18 +08:00
|
|
|
if done or not self.active[event.id] then
|
2022-12-05 21:45:50 +08:00
|
|
|
return
|
|
|
|
end
|
2023-10-11 20:24:18 +08:00
|
|
|
-- HACK: work-around for https://github.com/neovim/neovim/issues/25526
|
2023-10-07 01:42:19 +08:00
|
|
|
done = true
|
2024-07-04 21:17:02 +08:00
|
|
|
if event.id ~= "VeryLazy" then
|
|
|
|
Util.track({ [self.type] = event.id })
|
|
|
|
end
|
2023-10-07 17:15:12 +08:00
|
|
|
|
2023-10-11 20:24:18 +08:00
|
|
|
local state = M.get_state(ev.event, ev.buf, ev.data)
|
2023-10-07 17:15:12 +08:00
|
|
|
|
2022-12-05 21:45:50 +08:00
|
|
|
-- load the plugins
|
2023-10-11 20:24:18 +08:00
|
|
|
Loader.load(self.active[event.id], { [self.type] = event.id })
|
2023-10-07 17:15:12 +08:00
|
|
|
|
2022-12-30 00:26:38 +08:00
|
|
|
-- check if any plugin created an event handler for this event and fire the group
|
2023-10-07 17:15:12 +08:00
|
|
|
for _, s in ipairs(state) do
|
|
|
|
M.trigger(s)
|
|
|
|
end
|
2024-07-04 21:17:02 +08:00
|
|
|
if event.id ~= "VeryLazy" then
|
|
|
|
Util.track()
|
|
|
|
end
|
2022-12-05 21:45:50 +08:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-10-07 17:15:12 +08:00
|
|
|
-- Get the current state of the event and all the events that will be fired
|
|
|
|
---@param event string
|
|
|
|
---@param buf number
|
|
|
|
---@param data any
|
2023-10-11 20:24:18 +08:00
|
|
|
function M.get_state(event, buf, data)
|
2023-10-07 17:15:12 +08:00
|
|
|
local state = {} ---@type LazyEventOpts[]
|
|
|
|
while event do
|
|
|
|
table.insert(state, 1, {
|
|
|
|
event = event,
|
|
|
|
exclude = event ~= "FileType" and M.get_augroups(event) or nil,
|
|
|
|
buffer = buf,
|
|
|
|
data = data,
|
|
|
|
})
|
|
|
|
data = nil -- only pass the data to the first event
|
|
|
|
event = M.triggers[event]
|
|
|
|
end
|
|
|
|
return state
|
|
|
|
end
|
|
|
|
|
2022-12-05 21:45:50 +08:00
|
|
|
-- Get all augroups for the events
|
|
|
|
---@param event string
|
2023-10-07 17:15:12 +08:00
|
|
|
function M.get_augroups(event)
|
2023-10-07 15:48:53 +08:00
|
|
|
local groups = {} ---@type string[]
|
2023-10-07 17:15:12 +08:00
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event })) do
|
2023-10-07 15:48:53 +08:00
|
|
|
if autocmd.group_name then
|
|
|
|
table.insert(groups, autocmd.group_name)
|
2022-12-05 21:45:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return groups
|
|
|
|
end
|
|
|
|
|
2023-10-07 17:15:12 +08: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 15:48:53 +08:00
|
|
|
---@param opts LazyEventOpts
|
2023-10-06 21:39:18 +08:00
|
|
|
function M.trigger(opts)
|
2023-10-07 17:15:12 +08:00
|
|
|
if opts.group or opts.exclude == nil then
|
|
|
|
return M._trigger(opts)
|
|
|
|
end
|
2023-10-06 21:39:18 +08:00
|
|
|
local done = {} ---@type table<string,true>
|
2023-10-07 17:15:12 +08:00
|
|
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event })) do
|
2023-10-06 21:39:18 +08:00
|
|
|
local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string
|
2023-10-07 15:48:53 +08:00
|
|
|
local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name))
|
2023-10-06 21:39:18 +08:00
|
|
|
done[id] = true
|
|
|
|
if autocmd.group and not skip then
|
2023-10-07 17:15:12 +08:00
|
|
|
opts.group = autocmd.group_name
|
|
|
|
M._trigger(opts)
|
2022-12-05 21:45:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-07 17:15:12 +08:00
|
|
|
-- Trigger an event
|
|
|
|
---@param opts LazyEventOpts
|
|
|
|
function M._trigger(opts)
|
|
|
|
if Config.options.debug then
|
|
|
|
Util.info({
|
|
|
|
"# Firing Events",
|
|
|
|
" - **event:** " .. opts.event,
|
|
|
|
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, {
|
|
|
|
buffer = opts.buffer,
|
|
|
|
group = opts.group,
|
|
|
|
modeline = false,
|
|
|
|
data = opts.data,
|
|
|
|
})
|
|
|
|
Util.track()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-12-05 21:45:50 +08:00
|
|
|
return M
|