refactor: simplified handler code

This commit is contained in:
Folke Lemaitre 2022-12-16 09:13:08 +01:00
parent 17d1653b4a
commit ecf03a6892
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
7 changed files with 58 additions and 91 deletions

View File

@ -4,18 +4,17 @@ local Loader = require("lazy.core.loader")
---@class LazyCmdHandler:LazyHandler ---@class LazyCmdHandler:LazyHandler
local M = {} local M = {}
local function _load(plugin, cmd) function M:_load(cmd)
vim.api.nvim_del_user_command(cmd) vim.api.nvim_del_user_command(cmd)
Util.track({ cmd = cmd }) Util.track({ cmd = cmd })
Loader.load(plugin, { cmd = cmd }) Loader.load(self.active[cmd], { cmd = cmd })
Util.track() Util.track()
end end
---@param plugin LazyPlugin
---@param cmd string ---@param cmd string
function M:_add(plugin, cmd) function M:_add(cmd)
vim.api.nvim_create_user_command(cmd, function(event) vim.api.nvim_create_user_command(cmd, function(event)
_load(plugin, cmd) self:_load(cmd)
vim.cmd( vim.cmd(
("%s %s%s%s %s"):format( ("%s %s%s%s %s"):format(
event.mods or "", event.mods or "",
@ -29,7 +28,7 @@ function M:_add(plugin, cmd)
bang = true, bang = true,
nargs = "*", nargs = "*",
complete = function(_, line) complete = function(_, line)
_load(plugin, cmd) self:_load(cmd)
-- NOTE: return the newly loaded command completion -- NOTE: return the newly loaded command completion
return vim.fn.getcompletion(line, "cmdline") return vim.fn.getcompletion(line, "cmdline")
end, end,

View File

@ -11,26 +11,11 @@ M.trigger_events = {
BufRead = { "BufReadPre", "BufRead" }, BufRead = { "BufReadPre", "BufRead" },
BufReadPost = { "BufReadPre", "BufRead", "BufReadPost" }, BufReadPost = { "BufReadPre", "BufRead", "BufReadPost" },
} }
M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
function M:init()
self.group = vim.api.nvim_create_augroup("lazy_handler_" .. self.type, { clear = true })
self.events = {}
end
---@param event_spec string
function M:_add(_, event_spec)
if not self.events[event_spec] then
self:listen(event_spec)
end
end
---@param value string ---@param value string
function M:_value(value) function M:_add(value)
return value == "VeryLazy" and "User VeryLazy" or value local event_spec = self:_event(value)
end
function M:listen(event_spec)
self.events[event_spec] = true
---@type string?, string? ---@type string?, string?
local event, pattern = event_spec:match("^(%w+)%s+(.*)$") local event, pattern = event_spec:match("^(%w+)%s+(.*)$")
event = event or event_spec event = event or event_spec
@ -39,14 +24,13 @@ function M:listen(event_spec)
once = true, once = true,
pattern = pattern, pattern = pattern,
callback = function() callback = function()
if not self.active[event_spec] then if not self.active[value] then
return return
end end
Util.track({ [self.type] = event_spec }) Util.track({ [self.type] = value })
local groups = M.get_augroups(event, pattern) local groups = M.get_augroups(event, pattern)
-- load the plugins -- load the plugins
Loader.load(self.active[event_spec], { [self.type] = event_spec }) Loader.load(self.active[value], { [self.type] = value })
self.events[event_spec] = nil
-- check if any plugin created an event handler for this event and fire the group -- check if any plugin created an event handler for this event and fire the group
M.trigger(event, pattern, groups) M.trigger(event, pattern, groups)
Util.track() Util.track()
@ -54,6 +38,11 @@ function M:listen(event_spec)
}) })
end end
---@param value string
function M:_event(value)
return value == "VeryLazy" and "User VeryLazy" or value
end
-- Get all augroups for the events -- Get all augroups for the events
---@param event string ---@param event string
---@param pattern? string ---@param pattern? string

View File

@ -6,15 +6,16 @@ local M = {}
M.extends = Event M.extends = Event
---@param value string ---@param value string
function M:_value(value) function M:_event(value)
return "FileType " .. value return "FileType " .. value
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param value string function M:add(plugin)
function M:_add(plugin, value) self.super.add(self, plugin)
if plugin.ft then
Loader.ftdetect(plugin.dir) Loader.ftdetect(plugin.dir)
Event._add(self, plugin, value) end
end end
return M return M

View File

@ -4,6 +4,7 @@ local Config = require("lazy.core.config")
---@field type LazyHandlerTypes ---@field type LazyHandlerTypes
---@field extends? LazyHandler ---@field extends? LazyHandler
---@field active table<string,table<string,string>> ---@field active table<string,table<string,string>>
---@field super LazyHandler
local M = {} local M = {}
---@enum LazyHandlerTypes ---@enum LazyHandlerTypes
@ -50,68 +51,44 @@ end
function M.new(type) function M.new(type)
---@type LazyHandler ---@type LazyHandler
local handler = require("lazy.core.handler." .. type) local handler = require("lazy.core.handler." .. type)
local self = setmetatable({}, { local super = handler.extends or M
__index = function(_, k) local self = setmetatable({}, { __index = setmetatable(handler, { __index = super }) })
return handler[k] or (handler.extends and handler.extends[k]) or M[k] self.super = super
end,
})
self.active = {} self.active = {}
self.type = type self.type = type
self:init()
return self return self
end end
---@protected
function M:init() end
---@param plugin LazyPlugin
---@param value string ---@param value string
---@protected ---@protected
function M:_add(plugin, value) end function M:_add(value) end
---@param plugin LazyPlugin
---@param value string ---@param value string
---@protected ---@protected
function M:_del(plugin, value) end function M:_del(value) end
---@param value string
function M:_value(value)
return value
end
---@param values? string|string[]
---@param fn fun(value:string)
function M:foreach(values, fn)
if type(values) == "string" then
fn(values)
elseif values ~= nil then
for _, value in ipairs(values) do
fn(value)
end
end
end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:add(plugin) function M:add(plugin)
self:foreach(plugin[self.type], function(value) for _, value in ipairs(plugin[self.type] or {}) do
value = self:_value(value) if not self.active[value] then
if not (self.active[value] and self.active[value][plugin.name]) then self.active[value] = {}
self.active[value] = self.active[value] or {} self:_add(value)
self.active[value][plugin.name] = plugin.name end
self:_add(plugin, value) self.active[value][plugin.name] = plugin.name
end end
end)
end end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:del(plugin) function M:del(plugin)
self:foreach(plugin[self.type], function(value) for _, value in ipairs(plugin[self.type] or {}) do
value = self:_value(value)
if self.active[value] and self.active[value][plugin.name] then if self.active[value] and self.active[value][plugin.name] then
self.active[value][plugin.name] = nil self.active[value][plugin.name] = nil
self:_del(plugin, value) if vim.tbl_isempty(self.active[value]) then
self:_del(value)
self.active[value] = nil
end
end
end end
end)
end end
return M return M

View File

@ -4,22 +4,20 @@ local Loader = require("lazy.core.loader")
---@class LazyKeysHandler:LazyHandler ---@class LazyKeysHandler:LazyHandler
local M = {} local M = {}
---@param plugin LazyPlugin
---@param keys string ---@param keys string
function M:_add(plugin, keys) function M:_add(keys)
vim.keymap.set("n", keys, function() vim.keymap.set("n", keys, function()
vim.keymap.del("n", keys) vim.keymap.del("n", keys)
Util.track({ keys = keys }) Util.track({ keys = keys })
Loader.load(plugin, { keys = keys }) Loader.load(self.active[keys], { keys = keys })
vim.api.nvim_input(keys) vim.api.nvim_input(keys)
Util.track() Util.track()
end) end)
end end
---@param _plugin LazyPlugin ---@param keys string
---@param value string function M:_del(keys)
function M:_del(_plugin, value) pcall(vim.keymap.del, "n", keys)
pcall(vim.keymap.del, "n", value)
end end
return M return M

View File

@ -21,10 +21,10 @@ local M = {}
---@field build? string|fun(LazyPlugin) ---@field build? string|fun(LazyPlugin)
---@class LazyPluginHandlers: table<LazyHandlerTypes, string|string[]> ---@class LazyPluginHandlers: table<LazyHandlerTypes, string|string[]>
---@field event? string|string[] ---@field event? string[]
---@field cmd? string|string[] ---@field cmd? string[]
---@field ft? string|string[] ---@field ft? string[]
---@field keys? string|string[] ---@field keys? string[]
---@class LazyPluginRef ---@class LazyPluginRef
---@field branch? string ---@field branch? string
@ -101,6 +101,11 @@ function Spec:add(plugin, is_dep)
Util.error("Invalid plugin spec " .. vim.inspect(plugin)) Util.error("Invalid plugin spec " .. vim.inspect(plugin))
end end
plugin.event = type(plugin.event) == "string" and { plugin.event } or plugin.event
plugin.keys = type(plugin.keys) == "string" and { plugin.keys } or plugin.keys
plugin.cmd = type(plugin.cmd) == "string" and { plugin.cmd } or plugin.cmd
plugin.ft = type(plugin.ft) == "string" and { plugin.ft } or plugin.ft
plugin._ = {} plugin._ = {}
plugin._.dep = is_dep plugin._.dep = is_dep

View File

@ -242,8 +242,6 @@ function M:reason(reason, opts)
end end
if key == "event" then if key == "event" then
value = value:match("User (.*)") or value value = value:match("User (.*)") or value
elseif key == "ft" then
value = value:match("FileType (.*)") or value
end end
local hl = "LazyHandler" .. key:sub(1, 1):upper() .. key:sub(2) local hl = "LazyHandler" .. key:sub(1, 1):upper() .. key:sub(2)
local icon = Config.options.ui.icons[key] local icon = Config.options.ui.icons[key]