Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): support running all init functions #1801

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ function M.startup()
for _, plugin in pairs(Config.plugins) do
if plugin.init then
Util.track({ plugin = plugin.name, init = "init" })
Util.try(function()
plugin.init(plugin)
end, "Failed to run `init` for **" .. plugin.name .. "**")
M.init(plugin)
Util.track()
end
end
Expand Down Expand Up @@ -157,6 +155,16 @@ function M.startup()
Util.track()
end

---@param plugin LazyPlugin
function M.init(plugin)
Util.try(function()
local inits = Plugin.super_functions(plugin, "init")
for _, init in ipairs(inits) do
init(plugin)
end
end, "Failed to run `init` for **" .. plugin.name .. "**")
end

function M.get_start_plugins()
---@type LazyPlugin[]
local start = {}
Expand Down Expand Up @@ -264,9 +272,7 @@ function M.reload(plugin)

-- run init
if plugin.init then
Util.try(function()
plugin.init(plugin)
end, "Failed to run `init` for **" .. plugin.name .. "**")
M.init(plugin)
end

-- if this is a start plugin, load it now
Expand Down
18 changes: 18 additions & 0 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,22 @@ function M._values(root, plugin, prop, is_list)
end
end

-- Collects super values associated with a property that are functions
-- Used for init key
---@param plugin LazyPlugin
---@param prop string
---@return fun(self: LazyPlugin)[]
function M.super_functions(plugin, prop)
if not plugin[prop] then
return {}
end
local super = getmetatable(plugin)
local result = super and M.super_functions(super.__index, prop) or {}
local value = rawget(plugin, prop)
if type(value) == "function" then
table.insert(result, value)
end
return result
end

return M