|
| 1 | +local Util = require("lazy.core.util") |
| 2 | +local Config = require("lazy.core.config") |
| 3 | +local Loader = require("lazy.core.loader") |
| 4 | + |
| 5 | +---@class LazyEventHandler:LazyHandler |
| 6 | +---@field events table<string,true> |
| 7 | +---@field group number |
| 8 | +local M = {} |
| 9 | + |
| 10 | +M.trigger_events = { |
| 11 | + BufRead = { "BufReadPre", "BufRead" }, |
| 12 | + BufReadPost = { "BufReadPre", "BufRead", "BufReadPost" }, |
| 13 | +} |
| 14 | + |
| 15 | +function M:init() |
| 16 | + self.group = vim.api.nvim_create_augroup("lazy_handler_" .. self.type, { clear = true }) |
| 17 | + self.events = {} |
| 18 | +end |
| 19 | + |
| 20 | +---@param event_spec string |
| 21 | +function M:_add(_, event_spec) |
| 22 | + if not self.events[event_spec] then |
| 23 | + self:listen(event_spec) |
| 24 | + end |
| 25 | +end |
| 26 | + |
| 27 | +---@param value string |
| 28 | +function M:_value(value) |
| 29 | + return value == "VeryLazy" and "User VeryLazy" or value |
| 30 | +end |
| 31 | + |
| 32 | +function M:listen(event_spec) |
| 33 | + self.events[event_spec] = true |
| 34 | + ---@type string?, string? |
| 35 | + local event, pattern = event_spec:match("^(%w+)%s+(.*)$") |
| 36 | + event = event or event_spec |
| 37 | + vim.api.nvim_create_autocmd(event, { |
| 38 | + group = self.group, |
| 39 | + once = true, |
| 40 | + pattern = pattern, |
| 41 | + callback = function() |
| 42 | + if not self.active[event_spec] then |
| 43 | + return |
| 44 | + end |
| 45 | + Util.track({ [self.type] = event_spec }) |
| 46 | + local groups = M.get_augroups(event, pattern) |
| 47 | + -- load the plugins |
| 48 | + Loader.load(self.active[event_spec], { [self.type] = event_spec }) |
| 49 | + self.events[event_spec] = nil |
| 50 | + -- check if any plugin created an event handler for this event and fire the group |
| 51 | + M.trigger(event, pattern, groups) |
| 52 | + Util.track() |
| 53 | + end, |
| 54 | + }) |
| 55 | +end |
| 56 | + |
| 57 | +-- Get all augroups for the events |
| 58 | +---@param event string |
| 59 | +---@param pattern? string |
| 60 | +function M.get_augroups(event, pattern) |
| 61 | + local events = M.trigger_events[event] or { event } |
| 62 | + ---@type table<string,true> |
| 63 | + local groups = {} |
| 64 | + for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = events, pattern = pattern })) do |
| 65 | + if autocmd.group then |
| 66 | + groups[autocmd.group] = true |
| 67 | + end |
| 68 | + end |
| 69 | + return groups |
| 70 | +end |
| 71 | + |
| 72 | +---@param event string|string[] |
| 73 | +---@param pattern? string |
| 74 | +---@param groups table<string,true> |
| 75 | +function M.trigger(event, pattern, groups) |
| 76 | + local events = M.trigger_events[event] or { event } |
| 77 | + ---@cast events string[] |
| 78 | + for _, e in ipairs(events) do |
| 79 | + for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = e, pattern = pattern })) do |
| 80 | + if autocmd.event == e and autocmd.group and not groups[autocmd.group] then |
| 81 | + if Config.options.debug then |
| 82 | + Util.info({ |
| 83 | + "# Firing Events", |
| 84 | + " - **group:** `" .. autocmd.group_name .. "`", |
| 85 | + " - **event:** " .. autocmd.event, |
| 86 | + pattern and "- **pattern:** ", |
| 87 | + }) |
| 88 | + end |
| 89 | + Util.try(function() |
| 90 | + vim.api.nvim_exec_autocmds(autocmd.event, { group = autocmd.group, modeline = false }) |
| 91 | + end) |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +return M |
0 commit comments