Skip to content

Commit

Permalink
fix(ft): fix ft handlers to properly use new events. Fixes #1084
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 7, 2023
1 parent 6687afa commit e4ea874
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
24 changes: 18 additions & 6 deletions lua/lazy/core/handler/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ local Util = require("lazy.core.util")
local Config = require("lazy.core.config")
local Loader = require("lazy.core.loader")

---@class LazyEventOpts
---@field event string
---@field pattern? string
---@field exclude? string[]
---@field data? any
---@field buf? number}

---@class LazyEventHandler:LazyHandler
---@field events table<string,true>
---@field group number
Expand Down Expand Up @@ -30,7 +37,7 @@ function M:_add(value)
-- 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
M.trigger({
self:_trigger({
event = ev.event,
pattern = pattern,
exclude = groups,
Expand All @@ -56,21 +63,26 @@ end
---@param event string
---@param pattern? string
function M.get_augroups(event, pattern)
local groups = {} ---@type number[]
local groups = {} ---@type string[]
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event, pattern = pattern })) do
if autocmd.group then
table.insert(groups, autocmd.group)
if autocmd.group_name then
table.insert(groups, autocmd.group_name)
end
end
return groups
end

---@param opts {event:string, pattern?:string, exclude?:string[], data?:any, buf?:number}
---@param opts LazyEventOpts
function M:_trigger(opts)
M.trigger(opts)
end

---@param opts LazyEventOpts
function M.trigger(opts)
local done = {} ---@type table<string,true>
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.tbl_contains(opts.exclude, autocmd.group))
local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name))
done[id] = true
if autocmd.group and not skip then
if Config.options.debug then
Expand Down
30 changes: 15 additions & 15 deletions lua/lazy/core/handler/ft.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ function M:add(plugin)
end
end

---@param pattern? string
function M:trigger(_, pattern, _)
for _, group in ipairs({ "filetypeplugin", "filetypeindent" }) do
Util.try(function()
if Config.options.debug then
Util.info({
"# Firing Events",
" - **group:** `" .. group .. "`",
" - **event:** FileType",
pattern and (" - **pattern:** " .. pattern),
})
end
vim.api.nvim_exec_autocmds("FileType", { group = group, modeline = false, pattern = pattern })
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

0 comments on commit e4ea874

Please sign in to comment.