Skip to content

Commit

Permalink
fix(loader): don't load handlers before installing plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 16, 2023
1 parent ed6c9ff commit 1cfd6d1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lua/lazy/core/handler/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ end

---@param plugin LazyPlugin
function M:values(plugin)
local Plugin = require("lazy.core.plugin")
---@type table<string,any>
local values = {}
---@diagnostic disable-next-line: no-unknown
for _, value in ipairs(plugin[self.type] or {}) do
for _, value in ipairs(Plugin.values(plugin, self.type, true)) do
local event = self:parse(value)
values[event.id] = event
end
Expand Down
11 changes: 10 additions & 1 deletion lua/lazy/core/handler/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ end

---@param plugin LazyPlugin
function M.disable(plugin)
if not plugin._.handlers_enabled then
return
end
plugin._.handlers_enabled = false
for type, handler in pairs(M.handlers) do
if plugin[type] then
handler:del(plugin)
Expand All @@ -49,11 +53,15 @@ end
---@param plugin LazyPlugin
function M.enable(plugin)
if not plugin._.loaded then
if plugin._.handlers_enabled then
return
end
for type, handler in pairs(M.handlers) do
if plugin[type] then
handler:add(plugin)
end
end
plugin._.handlers_enabled = true
end
end

Expand All @@ -80,10 +88,11 @@ function M:_del(_value) end

---@param plugin LazyPlugin
function M:values(plugin)
local Plugin = require("lazy.core.plugin")
---@type table<string,any>
local values = {}
---@diagnostic disable-next-line: no-unknown
for _, value in ipairs(plugin[self.type] or {}) do
for _, value in ipairs(Plugin.values(plugin, self.type, true)) do
values[value] = value
end
return values
Expand Down
3 changes: 2 additions & 1 deletion lua/lazy/core/handler/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ end

---@param plugin LazyPlugin
function M:values(plugin)
return M.resolve(plugin.keys)
local Plugin = require("lazy.core.plugin")
return M.resolve(Plugin.values(plugin, "keys", true))
end

---@param spec? (string|LazyKeysSpec)[]
Expand Down
29 changes: 19 additions & 10 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ end
function Spec:parse(spec)
self:normalize(spec)
self:fix_disabled()

-- calculate handlers
for _, plugin in pairs(self.plugins) do
for _, handler in pairs(Handler.types) do
if plugin[handler] then
plugin[handler] = M.values(plugin, handler, true)
end
end
end
end

-- PERF: optimized code to get package name without using lua patterns
Expand Down Expand Up @@ -609,8 +600,26 @@ end
---@param prop string
---@param is_list? boolean
function M.values(plugin, prop, is_list)
if not plugin[prop] then
return {}
end
plugin._.values = plugin._.values or {}
local key = prop .. (is_list and "_list" or "")
if plugin._.values[key] == nil then
plugin[prop] = M._values(plugin, prop, is_list)
plugin._.values[key] = true
end
return plugin[prop] or {}
end

-- Merges super values or runs the values function to override values or return new ones
-- Used for opts, cmd, event, ft and keys
---@param plugin LazyPlugin
---@param prop string
---@param is_list? boolean
function M._values(plugin, prop, is_list)
---@type table
local ret = plugin._.super and M.values(plugin._.super, prop, is_list) or {}
local ret = plugin._.super and M._values(plugin._.super, prop, is_list) or {}
local values = rawget(plugin, prop)

if not values then
Expand Down
2 changes: 2 additions & 0 deletions lua/lazy/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
---@field module? string
---@field dir? string Explicit dir or dev set for this plugin
---@field rtp_loaded? boolean
---@field values? table<string,boolean>
---@field handlers_enabled? boolean

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

Expand Down
26 changes: 14 additions & 12 deletions lua/lazy/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ function M:plugin(plugin)
else
self:append(" ")
local reason = {}
if plugin._.kind ~= "disabled" then
if plugin._.kind ~= "disabled" and plugin._.handlers_enabled then
for handler in pairs(Handler.types) do
if plugin[handler] then
local values = Handler.handlers[handler]:values(plugin)
Expand Down Expand Up @@ -542,17 +542,19 @@ function M:details(plugin)
end
end)

for handler in pairs(Handler.types) do
if plugin[handler] then
table.insert(props, {
handler,
function()
for _, value in ipairs(plugin[handler]) do
self:reason({ [handler] = value })
self:append(" ")
end
end,
})
if plugin._.handlers_enabled then
for handler in pairs(Handler.types) do
if plugin[handler] then
table.insert(props, {
handler,
function()
for _, value in ipairs(Plugin.values(plugin, handler, true)) do
self:reason({ [handler] = value })
self:append(" ")
end
end,
})
end
end
end
self:props(props, { indent = 6 })
Expand Down

0 comments on commit 1cfd6d1

Please sign in to comment.