2023-01-02 08:36:52 +00:00
|
|
|
local Util = require("lazy.core.util")
|
2022-12-05 13:45:50 +00:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
|
|
|
|
---@class LazyHandler
|
|
|
|
---@field type LazyHandlerTypes
|
|
|
|
---@field extends? LazyHandler
|
|
|
|
---@field active table<string,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 = {}
|
|
|
|
|
|
|
|
function M.setup()
|
|
|
|
for _, type in pairs(M.types) do
|
|
|
|
M.handlers[type] = M.new(type)
|
|
|
|
end
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2023-01-02 08:36:52 +00:00
|
|
|
Util.try(function()
|
|
|
|
M.enable(plugin)
|
|
|
|
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)
|
|
|
|
for type, handler in pairs(M.handlers) do
|
|
|
|
if plugin[type] then
|
|
|
|
handler:del(plugin)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.enable(plugin)
|
|
|
|
if not plugin._.loaded then
|
|
|
|
for type, handler in pairs(M.handlers) do
|
|
|
|
if plugin[type] then
|
|
|
|
handler:add(plugin)
|
|
|
|
end
|
|
|
|
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 = {}
|
|
|
|
self.type = type
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param value string
|
|
|
|
---@protected
|
2022-12-16 08:13:08 +00:00
|
|
|
function M:_add(value) end
|
2022-12-05 13:45:50 +00:00
|
|
|
|
|
|
|
---@param value string
|
|
|
|
---@protected
|
2022-12-16 08:13:08 +00:00
|
|
|
function M:_del(value) end
|
2022-12-05 13:45:50 +00:00
|
|
|
|
2022-12-22 20:58:01 +00:00
|
|
|
---@return string
|
|
|
|
function M:key(value)
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
2022-12-05 13:45:50 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M:add(plugin)
|
2023-01-11 12:43:57 +00:00
|
|
|
local values = {}
|
2022-12-16 08:13:08 +00:00
|
|
|
for _, value in ipairs(plugin[self.type] or {}) do
|
2022-12-22 20:58:01 +00:00
|
|
|
local key = self:key(value)
|
2023-01-11 12:43:57 +00:00
|
|
|
values[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
for key, value in pairs(values) 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)
|
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-01-11 12:43:57 +00:00
|
|
|
local values = {}
|
2022-12-16 08:13:08 +00:00
|
|
|
for _, value in ipairs(plugin[self.type] or {}) do
|
2023-01-11 12:43:57 +00:00
|
|
|
local key = self:key(value)
|
|
|
|
values[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
for key, value in pairs(values) do
|
2022-12-22 20:58:01 +00:00
|
|
|
local key = self:key(value)
|
|
|
|
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
|