2022-12-05 13:45:50 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2023-10-09 10:25:42 +01:00
|
|
|
local Util = require("lazy.core.util")
|
2022-12-05 13:45:50 +00:00
|
|
|
|
|
|
|
---@class LazyHandler
|
|
|
|
---@field type LazyHandlerTypes
|
|
|
|
---@field extends? LazyHandler
|
|
|
|
---@field active table<string,table<string,string>>
|
2023-10-08 09:11:25 +01:00
|
|
|
---@field managed table<string,string>
|
2022-12-16 08:13:08 +00:00
|
|
|
---@field super LazyHandler
|
2022-12-05 13:45:50 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@enum LazyHandlerTypes
|
|
|
|
M.types = {
|
|
|
|
keys = "keys",
|
|
|
|
event = "event",
|
|
|
|
cmd = "cmd",
|
|
|
|
ft = "ft",
|
|
|
|
}
|
|
|
|
|
|
|
|
---@type table<string,LazyHandler>
|
|
|
|
M.handlers = {}
|
|
|
|
|
2023-09-27 11:39:39 +01:00
|
|
|
M.did_setup = false
|
|
|
|
|
|
|
|
function M.init()
|
2022-12-05 13:45:50 +00:00
|
|
|
for _, type in pairs(M.types) do
|
|
|
|
M.handlers[type] = M.new(type)
|
|
|
|
end
|
2023-09-27 11:39:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.setup()
|
|
|
|
M.did_setup = true
|
2022-12-05 13:45:50 +00:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2023-01-02 08:36:52 +00:00
|
|
|
Util.try(function()
|
2023-09-27 11:39:39 +01:00
|
|
|
M.enable(plugin)
|
2023-01-02 08:36:52 +00:00
|
|
|
end, "Failed to setup handlers for " .. plugin.name)
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.disable(plugin)
|
2023-10-16 21:34:44 +01:00
|
|
|
for type in pairs(plugin._.handlers or {}) do
|
|
|
|
M.handlers[type]:del(plugin)
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2023-09-27 11:39:39 +01:00
|
|
|
function M.enable(plugin)
|
|
|
|
if not plugin._.loaded then
|
2023-10-16 21:34:44 +01:00
|
|
|
if not plugin._.handlers then
|
|
|
|
M.load(plugin)
|
2023-10-16 14:05:16 +01:00
|
|
|
end
|
2023-10-16 21:34:44 +01:00
|
|
|
for type in pairs(plugin._.handlers or {}) do
|
|
|
|
M.handlers[type]:add(plugin)
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param type LazyHandlerTypes
|
|
|
|
function M.new(type)
|
|
|
|
---@type LazyHandler
|
|
|
|
local handler = require("lazy.core.handler." .. type)
|
2022-12-16 08:13:08 +00:00
|
|
|
local super = handler.extends or M
|
|
|
|
local self = setmetatable({}, { __index = setmetatable(handler, { __index = super }) })
|
|
|
|
self.super = super
|
2022-12-05 13:45:50 +00:00
|
|
|
self.active = {}
|
2023-10-08 09:11:25 +01:00
|
|
|
self.managed = {}
|
2022-12-05 13:45:50 +00:00
|
|
|
self.type = type
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param _value string
|
2022-12-05 13:45:50 +00:00
|
|
|
---@protected
|
2023-01-16 09:16:35 +00:00
|
|
|
function M:_add(_value) end
|
2022-12-05 13:45:50 +00:00
|
|
|
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param _value string
|
2022-12-05 13:45:50 +00:00
|
|
|
---@protected
|
2023-01-16 09:16:35 +00:00
|
|
|
function M:_del(_value) end
|
2022-12-22 20:58:01 +00:00
|
|
|
|
2023-10-16 21:34:44 +01:00
|
|
|
---@param value any
|
|
|
|
---@param _plugin LazyPlugin
|
|
|
|
---@return string|{id:string}
|
|
|
|
function M:_parse(value, _plugin)
|
|
|
|
assert(type(value) == "string", "Expected string, got " .. vim.inspect(value))
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param values any[]
|
2022-12-05 13:45:50 +00:00
|
|
|
---@param plugin LazyPlugin
|
2023-10-16 21:34:44 +01:00
|
|
|
function M:_values(values, plugin)
|
2023-01-16 09:16:35 +00:00
|
|
|
---@type table<string,any>
|
2023-10-16 21:34:44 +01:00
|
|
|
local ret = {}
|
|
|
|
for _, value in ipairs(values) do
|
|
|
|
local parsed = self:_parse(value, plugin)
|
|
|
|
ret[type(parsed) == "string" and parsed or parsed.id] = parsed
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.load(plugin)
|
|
|
|
local Plugin = require("lazy.core.plugin")
|
|
|
|
plugin._.handlers = {}
|
|
|
|
for type, handler in pairs(M.handlers) do
|
|
|
|
if plugin[type] then
|
|
|
|
plugin._.handlers[type] = handler:_values(Plugin.values(plugin, type, true), plugin)
|
|
|
|
end
|
2023-01-11 12:43:57 +00:00
|
|
|
end
|
2023-01-16 09:16:35 +00:00
|
|
|
end
|
2023-01-11 12:43:57 +00:00
|
|
|
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M:add(plugin)
|
2023-10-16 21:34:44 +01:00
|
|
|
for key, value in pairs(plugin._.handlers[self.type] or {}) do
|
2022-12-22 20:58:01 +00:00
|
|
|
if not self.active[key] then
|
|
|
|
self.active[key] = {}
|
2022-12-16 08:13:08 +00:00
|
|
|
self:_add(value)
|
2023-10-08 18:14:33 +01:00
|
|
|
self.managed[key] = key
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
2022-12-22 20:58:01 +00:00
|
|
|
self.active[key][plugin.name] = plugin.name
|
2022-12-16 08:13:08 +00:00
|
|
|
end
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M:del(plugin)
|
2023-10-16 21:34:44 +01:00
|
|
|
if not plugin._.handlers then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for key, value in pairs(plugin._.handlers[self.type] or {}) do
|
2022-12-22 20:58:01 +00:00
|
|
|
if self.active[key] and self.active[key][plugin.name] then
|
|
|
|
self.active[key][plugin.name] = nil
|
|
|
|
if vim.tbl_isempty(self.active[key]) then
|
2022-12-16 08:13:08 +00:00
|
|
|
self:_del(value)
|
2022-12-22 20:58:01 +00:00
|
|
|
self.active[key] = nil
|
2022-12-16 08:13:08 +00:00
|
|
|
end
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
2022-12-16 08:13:08 +00:00
|
|
|
end
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|