Skip to content

Commit

Permalink
feat: add before hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jun 10, 2024
1 parent 64a0bef commit 19beffc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lua/lz/n/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ function M.load_startup_plugins(plugins)
end
end

---@alias hook_key "before" | "after"

---@param hook_key hook_key
---@param plugin lz.n.Plugin
local function config(plugin)
if type(plugin.after) == "function" then
local function hook(hook_key, plugin)
if type(plugin[hook_key]) == "function" then
xpcall(
plugin.after,
plugin[hook_key],
vim.schedule_wrap(function(err)
vim.notify(
"Failed to run 'config' for " .. plugin.name .. ": " .. tostring(err or ""),
"Failed to run '" .. hook_key .. "' hook for " .. plugin.name .. ": " .. tostring(err or ""),
vim.log.levels.ERROR
)
end),
Expand All @@ -99,8 +102,9 @@ function M.load(plugins)
---@cast plugin lz.n.Plugin
end
if loadable then
hook("before", plugin)
M._load(plugin)
config(plugin)
hook("after", plugin)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lua/lz/n/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ error("Cannot import a meta module")

--- @class lz.n.PluginHooks
--- @field beforeAll? fun(self:lz.n.Plugin) Will be run before loading any plugins
--- @field before? fun(self:lz.n.Plugin) Will be run before loading this plugin
--- @field after? fun(self:lz.n.Plugin) Will be executed after loading this plugin

--- @class lz.n.PluginHandlers
Expand Down

0 comments on commit 19beffc

Please sign in to comment.