fix(event): better dealing with even handlers. Fixes #788

This commit is contained in:
Folke Lemaitre 2023-10-06 15:39:18 +02:00
parent 6b6f0a4512
commit ef2a5d0bd1
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 44 additions and 34 deletions

View File

@ -7,10 +7,6 @@ local Loader = require("lazy.core.loader")
---@field group number ---@field group number
local M = {} local M = {}
M.trigger_events = {
BufRead = { "BufReadPre", "BufRead" },
BufReadPost = { "BufReadPre", "BufRead", "BufReadPost" },
}
M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true }) M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
---@param value string ---@param value string
@ -28,11 +24,17 @@ function M:_add(value)
return return
end end
Util.track({ [self.type] = value }) Util.track({ [self.type] = value })
local groups = M.get_augroups(event, pattern) local groups = M.get_augroups(ev.event, pattern)
-- load the plugins -- load the plugins
Loader.load(self.active[value], { [self.type] = value }) Loader.load(self.active[value], { [self.type] = value })
-- check if any plugin created an event handler for this event and fire the group -- check if any plugin created an event handler for this event and fire the group
self:trigger(event, pattern, groups, ev.data) M.trigger({
event = ev.event,
pattern = pattern,
exclude = groups,
data = ev.data,
buf = ev.buf,
})
Util.track() Util.track()
end, end,
}) })
@ -40,49 +42,57 @@ end
---@param value string ---@param value string
function M:_event(value) function M:_event(value)
return value == "VeryLazy" and "User VeryLazy" or value if value == "VeryLazy" then
return "User VeryLazy"
elseif value == "BufRead" then
return "BufReadPost"
end
return value
end end
-- Get all augroups for the events -- Get all augroups for the events
---@param event string ---@param event string
---@param pattern? string ---@param pattern? string
function M.get_augroups(event, pattern) function M.get_augroups(event, pattern)
local events = M.trigger_events[event] or { event } local groups = {} ---@type number[]
---@type table<string,true> for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event, pattern = pattern })) do
local groups = {}
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = events, pattern = pattern })) do
if autocmd.group then if autocmd.group then
groups[autocmd.group] = true table.insert(groups, autocmd.group)
end end
end end
return groups return groups
end end
---@param event string|string[] ---@param opts {event:string, pattern?:string, exclude?:string[], data?:any, buf?:number}
---@param pattern? string function M.trigger(opts)
---@param groups table<string,true> local done = {} ---@type table<string,true>
function M:trigger(event, pattern, groups, data) for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event, pattern = opts.pattern })) do
local events = M.trigger_events[event] or { event } local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string
---@cast events string[] local skip = done[id] or (opts.exclude and vim.list_contains(opts.exclude, autocmd.group))
for _, e in ipairs(events) do done[id] = true
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = e, pattern = pattern })) do if autocmd.group and not skip then
if autocmd.event == e and autocmd.group and not groups[autocmd.group] then
if Config.options.debug then if Config.options.debug then
Util.info({ Util.info({
"# Firing Events", "# Firing Events",
" - **group:** `" .. autocmd.group_name .. "`", " - **group:** `" .. autocmd.group_name .. "`",
" - **event:** " .. autocmd.event, " - **event:** " .. autocmd.event,
pattern and (" - **pattern:** " .. pattern), opts.pattern and (" - **pattern:** " .. opts.pattern),
opts.buf and (" - **buf:** " .. opts.buf),
}) })
end end
Util.track({ event = autocmd.group_name }) Util.track({ event = autocmd.group_name })
Util.try(function() Util.try(function()
vim.api.nvim_exec_autocmds(autocmd.event, { group = autocmd.group, modeline = false, data = data }) vim.api.nvim_exec_autocmds(autocmd.event, {
-- pattern = opts.pattern,
buffer = opts.buf,
group = autocmd.group,
modeline = false,
data = opts.data,
})
Util.track() Util.track()
end) end)
end end
end end
end end
end
return M return M