fix(event): move all ft logic to the event handler

This commit is contained in:
Folke Lemaitre 2023-10-07 11:15:12 +02:00
parent f2132946c7
commit 8871602e54
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 74 additions and 57 deletions

View File

@ -5,15 +5,22 @@ local Loader = require("lazy.core.loader")
---@class LazyEventOpts ---@class LazyEventOpts
---@field event string ---@field event string
---@field pattern? string ---@field pattern? string
---@field group? string
---@field exclude? string[] ---@field exclude? string[]
---@field data? any ---@field data? any
---@field buf? number} ---@field buffer? number
---@class LazyEventHandler:LazyHandler ---@class LazyEventHandler:LazyHandler
---@field events table<string,true> ---@field events table<string,true>
---@field group number ---@field group number
local M = {} local M = {}
-- Event dependencies
M.triggers = {
FileType = "BufReadPost",
BufReadPost = "BufReadPre",
}
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
@ -33,22 +40,45 @@ function M:_add(value)
end end
done = true done = true
Util.track({ [self.type] = value }) Util.track({ [self.type] = value })
local groups = M.get_augroups(ev.event, pattern)
local state = M.get_state(ev.event, pattern, ev.buf, ev.data)
-- 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({ for _, s in ipairs(state) do
event = ev.event, M.trigger(s)
pattern = pattern, end
exclude = groups,
data = ev.data,
buf = ev.buf,
})
Util.track() Util.track()
end, end,
}) })
end end
-- 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
---@param value string ---@param value string
function M:_event(value) function M:_event(value)
if value == "VeryLazy" then if value == "VeryLazy" then
@ -61,10 +91,9 @@ end
-- Get all augroups for the events -- Get all augroups for the events
---@param event string ---@param event string
---@param pattern? string function M.get_augroups(event)
function M.get_augroups(event, pattern)
local groups = {} ---@type string[] local groups = {} ---@type string[]
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event, pattern = pattern })) do for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event })) do
if autocmd.group_name then if autocmd.group_name then
table.insert(groups, autocmd.group_name) table.insert(groups, autocmd.group_name)
end end
@ -72,41 +101,48 @@ function M.get_augroups(event, pattern)
return groups return groups
end end
---@param opts LazyEventOpts -- Trigger an event. When a group is given, only the events in that group will be triggered.
function M:_trigger(opts) -- When exclude is set, the events in those groups will be skipped.
M.trigger(opts)
end
---@param opts LazyEventOpts ---@param opts LazyEventOpts
function M.trigger(opts) function M.trigger(opts)
if opts.group or opts.exclude == nil then
return M._trigger(opts)
end
local done = {} ---@type table<string,true> local done = {} ---@type table<string,true>
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event, pattern = opts.pattern })) do for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event })) do
local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string
local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name)) local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name))
done[id] = true done[id] = true
if autocmd.group and not skip then if autocmd.group and not skip then
if Config.options.debug then opts.group = autocmd.group_name
Util.info({ M._trigger(opts)
"# 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 end
end end
-- 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
return M return M

View File

@ -1,7 +1,5 @@
local Event = require("lazy.core.handler.event") local Event = require("lazy.core.handler.event")
local Util = require("lazy.core.util")
local Loader = require("lazy.core.loader") local Loader = require("lazy.core.loader")
local Config = require("lazy.core.config")
---@class LazyFiletypeHandler:LazyEventHandler ---@class LazyFiletypeHandler:LazyEventHandler
local M = {} local M = {}
@ -20,21 +18,4 @@ function M:add(plugin)
end end
end end
---@param opts LazyEventOpts
function M:_trigger(opts)
Util.try(function()
if Config.options.debug then
Util.info({
"# Firing Events",
" - **event:** FileType",
opts.pattern and (" - **pattern:** " .. opts.pattern),
opts.buf and (" - **buf:** " .. opts.buf),
})
end
Util.track({ event = "FileType" })
vim.api.nvim_exec_autocmds("FileType", { modeline = false, buffer = opts.buf })
Util.track()
end)
end
return M return M