my-nvim-lua/lua/core/hooks.lua

69 lines
1.4 KiB
Lua
Raw Normal View History

2021-10-02 05:15:50 +00:00
local hooks, overrides, M = {}, {}, {}
local allowed_hooks = {
2021-10-02 05:15:50 +00:00
"install_plugins",
"setup_mappings",
"ready",
}
2021-10-02 05:15:50 +00:00
local function has_value(tab, val)
for _, value in ipairs(tab) do
if value == val then
return true
end
end
2021-10-02 05:15:50 +00:00
return false
end
M.add = function(name, fn)
2021-10-02 05:15:50 +00:00
if not (has_value(allowed_hooks, name)) then
error("Custom lua uses unallowed hook " .. name)
end
if hooks[name] == nil then
hooks[name] = {}
end
table.insert(hooks[name], fn)
end
M.run = function(name, args)
2021-10-02 05:15:50 +00:00
if hooks[name] == nil then
return
end
2021-10-02 05:15:50 +00:00
for _, hook in pairs(hooks[name]) do
hook(args)
end
end
2021-08-31 14:20:57 +00:00
M.createOverrides = function(module)
2021-10-02 05:15:50 +00:00
local O = {}
2021-08-31 14:20:57 +00:00
2021-10-02 05:15:50 +00:00
O.get = function(name, default)
local current = default
if overrides[module] and overrides[module][name] then
if type(overrides[module][name]) == "function" then
current = overrides[module][name]
elseif type(overrides[module][name]) == "table" then
for _, override in pairs(overrides[module][name]) do
current = override(current)
end
2021-08-31 14:20:57 +00:00
end
end
2021-10-02 05:15:50 +00:00
return current
end
2021-08-31 14:20:57 +00:00
2021-10-02 05:15:50 +00:00
return O
2021-08-31 14:20:57 +00:00
end
M.override = function(module, name, overwrite)
2021-10-02 05:15:50 +00:00
if overrides[module] == nil then
overrides[module] = {}
end
if overrides[module][name] == nil then
overrides[module][name] = {}
end
table.insert(overrides[module][name], overwrite)
2021-08-31 14:20:57 +00:00
end
2021-10-02 05:15:50 +00:00
return M