mirror of https://github.com/folke/lazy.nvim.git
feat: for `event=`, fire any new autocmds created by loading the plugins for the event
This commit is contained in:
parent
e4cf8b1416
commit
ebf15fc198
|
@ -29,23 +29,79 @@ function M.setup()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param events string|string[]
|
||||||
|
---@param pattern? string
|
||||||
|
function M.get_augroups(events, pattern)
|
||||||
|
-- Check for all autocmd groups listening for the events
|
||||||
|
---@type table<string,true>
|
||||||
|
local groups = {}
|
||||||
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = events, pattern = pattern })) do
|
||||||
|
if autocmd.group then
|
||||||
|
groups[autocmd.group] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return groups
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param groups table<string,true>
|
||||||
|
---@param events string|string[]
|
||||||
|
---@param pattern? string
|
||||||
|
function M.trigger(groups, events, pattern)
|
||||||
|
events = type(events) == "string" and { events } or events
|
||||||
|
---@cast events string[]
|
||||||
|
for _, event in ipairs(events) do
|
||||||
|
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event, pattern = pattern })) do
|
||||||
|
if autocmd.event == event and autocmd.group and not groups[autocmd.group] then
|
||||||
|
if Config.options.debug then
|
||||||
|
local lines = {
|
||||||
|
"# Firing Events",
|
||||||
|
" - **event:** " .. autocmd.event,
|
||||||
|
" - **group:** `" .. autocmd.group_name .. "`",
|
||||||
|
}
|
||||||
|
if pattern then
|
||||||
|
table.insert(lines, 2, "- **pattern:** " .. pattern)
|
||||||
|
end
|
||||||
|
Util.info(lines)
|
||||||
|
end
|
||||||
|
vim.api.nvim_exec_autocmds(autocmd.event, { group = autocmd.group, modeline = false })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@type table<string, LazyHandler>
|
---@type table<string, LazyHandler>
|
||||||
M.handlers = {}
|
M.handlers = {}
|
||||||
function M.handlers.event(grouped)
|
function M.handlers.event(grouped)
|
||||||
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
|
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
|
||||||
for event, plugins in pairs(grouped) do
|
for event_spec, plugins in pairs(grouped) do
|
||||||
---@cast event string
|
if event_spec == "VeryLazy" then
|
||||||
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
|
event_spec = "User VeryLazy"
|
||||||
Loader.load(plugins, { event = event })
|
end
|
||||||
|
if event_spec == "VimEnter" and vim.v.vim_did_enter == 1 then
|
||||||
|
Loader.load(plugins, { event = event_spec })
|
||||||
else
|
else
|
||||||
local _event, pattern = event:match("^(%w+)%s+(.*)$")
|
local event, pattern = event_spec:match("^(%w+)%s+(.*)$")
|
||||||
vim.api.nvim_create_autocmd(_event or event, {
|
event = event or event_spec
|
||||||
|
vim.api.nvim_create_autocmd(event, {
|
||||||
group = group,
|
group = group,
|
||||||
once = true,
|
once = true,
|
||||||
pattern = pattern,
|
pattern = pattern,
|
||||||
callback = function()
|
callback = function()
|
||||||
Util.track({ event = event })
|
Util.track({ event = event_spec })
|
||||||
Loader.load(plugins, { event = event })
|
local events = { event }
|
||||||
|
if event == "BufRead" then
|
||||||
|
events = { "BufReadPre", "BufRead" }
|
||||||
|
elseif event == "BufReadPost" then
|
||||||
|
events = { "BufReadPre", "BufRead", "BufReadPost" }
|
||||||
|
end
|
||||||
|
|
||||||
|
local groups = M.get_augroups(events, pattern)
|
||||||
|
|
||||||
|
-- load the plugins
|
||||||
|
Loader.load(plugins, { event = event_spec })
|
||||||
|
|
||||||
|
-- check if any plugin created an event handler for this event and fire the group
|
||||||
|
M.trigger(groups, events, pattern)
|
||||||
Util.track()
|
Util.track()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
@ -55,7 +111,6 @@ end
|
||||||
|
|
||||||
function M.handlers.keys(grouped)
|
function M.handlers.keys(grouped)
|
||||||
for keys, plugins in pairs(grouped) do
|
for keys, plugins in pairs(grouped) do
|
||||||
---@cast keys string
|
|
||||||
vim.keymap.set("n", keys, function()
|
vim.keymap.set("n", keys, function()
|
||||||
vim.keymap.del("n", keys)
|
vim.keymap.del("n", keys)
|
||||||
Util.track({ keys = keys })
|
Util.track({ keys = keys })
|
||||||
|
@ -69,14 +124,15 @@ end
|
||||||
function M.handlers.ft(grouped)
|
function M.handlers.ft(grouped)
|
||||||
local group = vim.api.nvim_create_augroup("lazy_handler_ft", { clear = true })
|
local group = vim.api.nvim_create_augroup("lazy_handler_ft", { clear = true })
|
||||||
for ft, plugins in pairs(grouped) do
|
for ft, plugins in pairs(grouped) do
|
||||||
---@cast ft string
|
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
once = true,
|
once = true,
|
||||||
pattern = ft,
|
pattern = ft,
|
||||||
group = group,
|
group = group,
|
||||||
callback = function()
|
callback = function()
|
||||||
Util.track({ ft = ft })
|
Util.track({ ft = ft })
|
||||||
|
local groups = M.get_augroups("FileType", ft)
|
||||||
Loader.load(plugins, { ft = ft })
|
Loader.load(plugins, { ft = ft })
|
||||||
|
M.trigger(groups, "FileType", ft)
|
||||||
Util.track()
|
Util.track()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
@ -85,7 +141,6 @@ end
|
||||||
|
|
||||||
function M.handlers.cmd(grouped)
|
function M.handlers.cmd(grouped)
|
||||||
for cmd, plugins in pairs(grouped) do
|
for cmd, plugins in pairs(grouped) do
|
||||||
---@cast cmd string
|
|
||||||
local function _load()
|
local function _load()
|
||||||
vim.api.nvim_del_user_command(cmd)
|
vim.api.nvim_del_user_command(cmd)
|
||||||
Util.track({ cmd = cmd })
|
Util.track({ cmd = cmd })
|
||||||
|
|
Loading…
Reference in New Issue