From ef2a5d0bd1a4995539b93be69fc760be7d9f62be Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 6 Oct 2023 15:39:18 +0200 Subject: [PATCH] fix(event): better dealing with even handlers. Fixes #788 --- lua/lazy/core/handler/event.lua | 78 +++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/lua/lazy/core/handler/event.lua b/lua/lazy/core/handler/event.lua index 0e26c68..050c3f6 100644 --- a/lua/lazy/core/handler/event.lua +++ b/lua/lazy/core/handler/event.lua @@ -7,10 +7,6 @@ local Loader = require("lazy.core.loader") ---@field group number local M = {} -M.trigger_events = { - BufRead = { "BufReadPre", "BufRead" }, - BufReadPost = { "BufReadPre", "BufRead", "BufReadPost" }, -} M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true }) ---@param value string @@ -28,11 +24,17 @@ function M:_add(value) return end Util.track({ [self.type] = value }) - local groups = M.get_augroups(event, pattern) + local groups = M.get_augroups(ev.event, pattern) -- load the plugins Loader.load(self.active[value], { [self.type] = value }) -- 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() end, }) @@ -40,47 +42,55 @@ end ---@param value string 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 -- Get all augroups for the events ---@param event string ---@param pattern? string function M.get_augroups(event, pattern) - local events = M.trigger_events[event] or { event } - ---@type table - local groups = {} - for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = events, pattern = pattern })) do + local groups = {} ---@type number[] + for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event, pattern = pattern })) do if autocmd.group then - groups[autocmd.group] = true + table.insert(groups, autocmd.group) end end return groups end ----@param event string|string[] ----@param pattern? string ----@param groups table -function M:trigger(event, pattern, groups, data) - local events = M.trigger_events[event] or { event } - ---@cast events string[] - for _, e in ipairs(events) do - for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = e, pattern = pattern })) do - 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, - pattern and (" - **pattern:** " .. pattern), - }) - end - Util.track({ event = autocmd.group_name }) - Util.try(function() - vim.api.nvim_exec_autocmds(autocmd.event, { group = autocmd.group, modeline = false, data = data }) - Util.track() - end) +---@param opts {event:string, pattern?:string, exclude?:string[], data?:any, buf?:number} +function M.trigger(opts) + local done = {} ---@type table + for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event, pattern = opts.pattern })) do + local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string + local skip = done[id] or (opts.exclude and vim.list_contains(opts.exclude, autocmd.group)) + done[id] = true + if autocmd.group and not skip then + if Config.options.debug then + Util.info({ + "# Firing Events", + " - **group:** `" .. autocmd.group_name .. "`", + " - **event:** " .. autocmd.event, + opts.pattern and (" - **pattern:** " .. opts.pattern), + opts.buf and (" - **buf:** " .. opts.buf), + }) end + Util.track({ event = autocmd.group_name }) + Util.try(function() + vim.api.nvim_exec_autocmds(autocmd.event, { + -- pattern = opts.pattern, + buffer = opts.buf, + group = autocmd.group, + modeline = false, + data = opts.data, + }) + Util.track() + end) end end end