Skip to content

Commit b8d8648

Browse files
committed
refactor: moved handler to separate modules
1 parent 1ae4e0c commit b8d8648

File tree

7 files changed

+320
-228
lines changed

7 files changed

+320
-228
lines changed

lua/lazy/core/handler.lua

-220
This file was deleted.

lua/lazy/core/handler/cmd.lua

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local Util = require("lazy.core.util")
2+
local Loader = require("lazy.core.loader")
3+
4+
---@class LazyCmdHandler:LazyHandler
5+
local M = {}
6+
7+
local function _load(plugin, cmd)
8+
vim.api.nvim_del_user_command(cmd)
9+
Util.track({ cmd = cmd })
10+
Loader.load(plugin, { cmd = cmd })
11+
Util.track()
12+
end
13+
14+
---@param plugin LazyPlugin
15+
---@param cmd string
16+
function M:_add(plugin, cmd)
17+
vim.api.nvim_create_user_command(cmd, function(event)
18+
_load(plugin, cmd)
19+
vim.cmd(
20+
("%s %s%s%s %s"):format(
21+
event.mods or "",
22+
event.line1 == event.line2 and "" or event.line1 .. "," .. event.line2,
23+
cmd,
24+
event.bang and "!" or "",
25+
event.args or ""
26+
)
27+
)
28+
end, {
29+
bang = true,
30+
nargs = "*",
31+
complete = function()
32+
_load(plugin, cmd)
33+
-- HACK: trick Neovim to show the newly loaded command completion
34+
vim.api.nvim_input("<space><bs><tab>")
35+
end,
36+
})
37+
end
38+
39+
---@param _plugin LazyPlugin
40+
---@param value string
41+
function M:_del(_plugin, value)
42+
pcall(vim.api.nvim_del_user_command, value)
43+
end
44+
45+
return M

lua/lazy/core/handler/event.lua

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

lua/lazy/core/handler/ft.lua

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local Event = require("lazy.core.handler.event")
2+
local Loader = require("lazy.core.loader")
3+
4+
---@class LazyFiletypeHandler:LazyEventHandler
5+
local M = {}
6+
M.extends = Event
7+
8+
---@param value string
9+
function M:_value(value)
10+
return "FileType " .. value
11+
end
12+
13+
---@param plugin LazyPlugin
14+
---@param value string
15+
function M:_add(plugin, value)
16+
Loader.ftdetect(plugin)
17+
Event._add(self, plugin, value)
18+
end
19+
20+
return M

0 commit comments

Comments
 (0)