Skip to content

Commit 2f169e7

Browse files
authored
refactor(handlers): lazy resolving of plugin handlers (#1126)
* refactor(handlers): lazy resolving of plugin handlers * test: fixed tests
1 parent b9c604e commit 2f169e7

File tree

9 files changed

+126
-120
lines changed

9 files changed

+126
-120
lines changed

lua/lazy/core/config.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ M.defaults = {
5555
icons = {
5656
cmd = "",
5757
config = "",
58-
event = "",
58+
event = " ",
5959
ft = "",
6060
init = "",
6161
import = "",
@@ -67,7 +67,7 @@ M.defaults = {
6767
runtime = "",
6868
require = "󰢱 ",
6969
source = "",
70-
start = "",
70+
start = " ",
7171
task = "",
7272
list = {
7373
"",

lua/lazy/core/handler/event.lua

+1-14
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
3737

3838
---@param spec LazyEventSpec
3939
---@return LazyEvent
40-
function M:parse(spec)
40+
function M:_parse(spec)
4141
local ret = M.mappings[spec] --[[@as LazyEvent?]]
4242
if ret then
4343
return ret
@@ -62,19 +62,6 @@ function M:parse(spec)
6262
return ret
6363
end
6464

65-
---@param plugin LazyPlugin
66-
function M:values(plugin)
67-
local Plugin = require("lazy.core.plugin")
68-
---@type table<string,any>
69-
local values = {}
70-
---@diagnostic disable-next-line: no-unknown
71-
for _, value in ipairs(Plugin.values(plugin, self.type, true)) do
72-
local event = self:parse(value)
73-
values[event.id] = event
74-
end
75-
return values
76-
end
77-
7865
---@param event LazyEvent
7966
function M:_add(event)
8067
local done = false

lua/lazy/core/handler/ft.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function M:add(plugin)
1313
end
1414
end
1515

16-
function M:parse(value)
16+
---@return LazyEvent
17+
function M:_parse(value)
1718
return {
1819
id = value,
1920
event = "FileType",

lua/lazy/core/handler/init.lua

+37-24
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,20 @@ end
3939

4040
---@param plugin LazyPlugin
4141
function M.disable(plugin)
42-
if not plugin._.handlers_enabled then
43-
return
44-
end
45-
plugin._.handlers_enabled = false
46-
for type, handler in pairs(M.handlers) do
47-
if plugin[type] then
48-
handler:del(plugin)
49-
end
42+
for type in pairs(plugin._.handlers or {}) do
43+
M.handlers[type]:del(plugin)
5044
end
5145
end
5246

5347
---@param plugin LazyPlugin
5448
function M.enable(plugin)
5549
if not plugin._.loaded then
56-
if plugin._.handlers_enabled then
57-
return
50+
if not plugin._.handlers then
51+
M.load(plugin)
5852
end
59-
for type, handler in pairs(M.handlers) do
60-
if plugin[type] then
61-
handler:add(plugin)
62-
end
53+
for type in pairs(plugin._.handlers or {}) do
54+
M.handlers[type]:add(plugin)
6355
end
64-
plugin._.handlers_enabled = true
6556
end
6657
end
6758

@@ -86,21 +77,40 @@ function M:_add(_value) end
8677
---@protected
8778
function M:_del(_value) end
8879

80+
---@param value any
81+
---@param _plugin LazyPlugin
82+
---@return string|{id:string}
83+
function M:_parse(value, _plugin)
84+
assert(type(value) == "string", "Expected string, got " .. vim.inspect(value))
85+
return value
86+
end
87+
88+
---@param values any[]
8989
---@param plugin LazyPlugin
90-
function M:values(plugin)
91-
local Plugin = require("lazy.core.plugin")
90+
function M:_values(values, plugin)
9291
---@type table<string,any>
93-
local values = {}
94-
---@diagnostic disable-next-line: no-unknown
95-
for _, value in ipairs(Plugin.values(plugin, self.type, true)) do
96-
values[value] = value
92+
local ret = {}
93+
for _, value in ipairs(values) do
94+
local parsed = self:_parse(value, plugin)
95+
ret[type(parsed) == "string" and parsed or parsed.id] = parsed
96+
end
97+
return ret
98+
end
99+
100+
---@param plugin LazyPlugin
101+
function M.load(plugin)
102+
local Plugin = require("lazy.core.plugin")
103+
plugin._.handlers = {}
104+
for type, handler in pairs(M.handlers) do
105+
if plugin[type] then
106+
plugin._.handlers[type] = handler:_values(Plugin.values(plugin, type, true), plugin)
107+
end
97108
end
98-
return values
99109
end
100110

101111
---@param plugin LazyPlugin
102112
function M:add(plugin)
103-
for key, value in pairs(self:values(plugin)) do
113+
for key, value in pairs(plugin._.handlers[self.type] or {}) do
104114
if not self.active[key] then
105115
self.active[key] = {}
106116
self:_add(value)
@@ -112,7 +122,10 @@ end
112122

113123
---@param plugin LazyPlugin
114124
function M:del(plugin)
115-
for key, value in pairs(self:values(plugin)) do
125+
if not plugin._.handlers then
126+
return
127+
end
128+
for key, value in pairs(plugin._.handlers[self.type] or {}) do
116129
if self.active[key] and self.active[key][plugin.name] then
117130
self.active[key][plugin.name] = nil
118131
if vim.tbl_isempty(self.active[key]) then

lua/lazy/core/handler/keys.lua

+14-7
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ local Util = require("lazy.core.util")
1919
---@field rhs? string|fun() rhs
2020
---@field mode? string
2121
---@field id string
22+
---@field name string
2223

2324
---@class LazyKeysHandler:LazyHandler
2425
local M = {}
2526

27+
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
28+
2629
---@param value string|LazyKeysSpec
2730
---@param mode? string
2831
---@return LazyKeys
@@ -37,23 +40,27 @@ function M.parse(value, mode)
3740
ret[2] = nil
3841
ret.mode = mode or "n"
3942
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
43+
4044
if ret.mode ~= "n" then
4145
ret.id = ret.id .. " (" .. ret.mode .. ")"
4246
end
4347
return ret
4448
end
4549

50+
---@param keys LazyKeys
51+
function M.to_string(keys)
52+
return keys.lhs .. (keys.mode == "n" and "" or " (" .. keys.mode .. ")")
53+
end
54+
4655
---@param lhs string
4756
---@param mode? string
4857
function M:have(lhs, mode)
4958
local keys = M.parse(lhs, mode)
5059
return self.managed[keys.id] ~= nil
5160
end
5261

53-
---@param plugin LazyPlugin
54-
function M:values(plugin)
55-
local Plugin = require("lazy.core.plugin")
56-
return M.resolve(Plugin.values(plugin, "keys", true))
62+
function M:_values(values)
63+
return M.resolve(values)
5764
end
5865

5966
---@param spec? (string|LazyKeysSpec)[]
@@ -79,7 +86,6 @@ end
7986

8087
---@param keys LazyKeys
8188
function M.opts(keys)
82-
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
8389
local opts = {} ---@type LazyKeysBase
8490
---@diagnostic disable-next-line: no-unknown
8591
for k, v in pairs(keys) do
@@ -106,8 +112,9 @@ function M:_add(keys)
106112
self.active[keys.id] = nil
107113

108114
if plugins then
109-
Util.track({ keys = lhs })
110-
Loader.load(plugins, { keys = lhs })
115+
local name = M.to_string(keys)
116+
Util.track({ keys = name })
117+
Loader.load(plugins, { keys = name })
111118
Util.track()
112119
end
113120

lua/lazy/core/plugin.lua

+11-12
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,6 @@ function M.load()
556556
Config.plugins[name]._ = plugin._
557557
Config.plugins[name]._.dep = dep
558558
Config.plugins[name]._.super = super
559-
-- FIXME: work-around for changes related to Plugin.values
560-
for handler in pairs(Handler) do
561-
Config.plugins[name][handler] = plugin[handler]
562-
end
563559
end
564560
end
565561
Util.track()
@@ -607,29 +603,32 @@ function M.values(plugin, prop, is_list)
607603
if not plugin[prop] then
608604
return {}
609605
end
610-
plugin._.values = plugin._.values or {}
606+
plugin._.cache = plugin._.cache or {}
611607
local key = prop .. (is_list and "_list" or "")
612-
if plugin._.values[key] == nil then
613-
plugin[prop] = M._values(plugin, prop, is_list)
614-
plugin._.values[key] = true
608+
if plugin._.cache[key] == nil then
609+
plugin._.cache[key] = M._values(plugin, plugin, prop, is_list)
615610
end
616-
return plugin[prop] or {}
611+
return plugin._.cache[key]
617612
end
618613

619614
-- Merges super values or runs the values function to override values or return new ones
620615
-- Used for opts, cmd, event, ft and keys
616+
---@param root LazyPlugin
621617
---@param plugin LazyPlugin
622618
---@param prop string
623619
---@param is_list? boolean
624-
function M._values(plugin, prop, is_list)
620+
function M._values(root, plugin, prop, is_list)
621+
if not plugin[prop] then
622+
return {}
623+
end
625624
---@type table
626-
local ret = plugin._.super and M._values(plugin._.super, prop, is_list) or {}
625+
local ret = plugin._.super and M._values(root, plugin._.super, prop, is_list) or {}
627626
local values = rawget(plugin, prop)
628627

629628
if not values then
630629
return ret
631630
elseif type(values) == "function" then
632-
ret = values(plugin, ret) or ret
631+
ret = values(root, ret) or ret
633632
return type(ret) == "table" and ret or { ret }
634633
end
635634

lua/lazy/types.lua

+7-8
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
---@field module? string
2121
---@field dir? string Explicit dir or dev set for this plugin
2222
---@field rtp_loaded? boolean
23-
---@field values? table<string,boolean>
24-
---@field handlers_enabled? boolean
23+
---@field handlers? LazyPluginHandlers
24+
---@field cache? table<string,any>
2525

2626
---@alias PluginOpts table|fun(self:LazyPlugin, opts:table):table?
2727

@@ -32,12 +32,11 @@
3232
---@field build? string|fun(self:LazyPlugin)|(string|fun(self:LazyPlugin))[]
3333
---@field opts? PluginOpts
3434

35-
---@class LazyPluginHandlers
36-
---@field event? LazyEventSpec[]
37-
---@field cmd? string[]
38-
---@field ft? string[]
39-
---@field keys? (string|LazyKeysSpec)[]
40-
---@field module? false
35+
---@class LazyPluginHandlers: {[string]: any}
36+
---@field event? table<string,LazyEvent>
37+
---@field ft? table<string,LazyEvent>
38+
---@field keys? table<string,LazyKeys>
39+
---@field cmd? table<string,string>
4140

4241
---@class LazyPluginRef
4342
---@field branch? string

0 commit comments

Comments
 (0)