diff --git a/lua/lazy/core/handler/cmd.lua b/lua/lazy/core/handler/cmd.lua index a2766a4..267e338 100644 --- a/lua/lazy/core/handler/cmd.lua +++ b/lua/lazy/core/handler/cmd.lua @@ -4,18 +4,17 @@ local Loader = require("lazy.core.loader") ---@class LazyCmdHandler:LazyHandler local M = {} -local function _load(plugin, cmd) +function M:_load(cmd) vim.api.nvim_del_user_command(cmd) Util.track({ cmd = cmd }) - Loader.load(plugin, { cmd = cmd }) + Loader.load(self.active[cmd], { cmd = cmd }) Util.track() end ----@param plugin LazyPlugin ---@param cmd string -function M:_add(plugin, cmd) +function M:_add(cmd) vim.api.nvim_create_user_command(cmd, function(event) - _load(plugin, cmd) + self:_load(cmd) vim.cmd( ("%s %s%s%s %s"):format( event.mods or "", @@ -29,7 +28,7 @@ function M:_add(plugin, cmd) bang = true, nargs = "*", complete = function(_, line) - _load(plugin, cmd) + self:_load(cmd) -- NOTE: return the newly loaded command completion return vim.fn.getcompletion(line, "cmdline") end, diff --git a/lua/lazy/core/handler/event.lua b/lua/lazy/core/handler/event.lua index 545f42b..7e38afa 100644 --- a/lua/lazy/core/handler/event.lua +++ b/lua/lazy/core/handler/event.lua @@ -11,26 +11,11 @@ M.trigger_events = { BufRead = { "BufReadPre", "BufRead" }, BufReadPost = { "BufReadPre", "BufRead", "BufReadPost" }, } - -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 +M.group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true }) ---@param value string -function M:_value(value) - return value == "VeryLazy" and "User VeryLazy" or value -end - -function M:listen(event_spec) - self.events[event_spec] = true +function M:_add(value) + local event_spec = self:_event(value) ---@type string?, string? local event, pattern = event_spec:match("^(%w+)%s+(.*)$") event = event or event_spec @@ -39,14 +24,13 @@ function M:listen(event_spec) once = true, pattern = pattern, callback = function() - if not self.active[event_spec] then + if not self.active[value] then return end - Util.track({ [self.type] = event_spec }) + Util.track({ [self.type] = value }) local groups = M.get_augroups(event, pattern) -- load the plugins - Loader.load(self.active[event_spec], { [self.type] = event_spec }) - self.events[event_spec] = nil + Loader.load(self.active[value], { [self.type] = value }) -- check if any plugin created an event handler for this event and fire the group M.trigger(event, pattern, groups) Util.track() @@ -54,6 +38,11 @@ function M:listen(event_spec) }) end +---@param value string +function M:_event(value) + return value == "VeryLazy" and "User VeryLazy" or value +end + -- Get all augroups for the events ---@param event string ---@param pattern? string diff --git a/lua/lazy/core/handler/ft.lua b/lua/lazy/core/handler/ft.lua index ef6c5dc..f0947c6 100644 --- a/lua/lazy/core/handler/ft.lua +++ b/lua/lazy/core/handler/ft.lua @@ -6,15 +6,16 @@ local M = {} M.extends = Event ---@param value string -function M:_value(value) +function M:_event(value) return "FileType " .. value end ---@param plugin LazyPlugin ----@param value string -function M:_add(plugin, value) - Loader.ftdetect(plugin.dir) - Event._add(self, plugin, value) +function M:add(plugin) + self.super.add(self, plugin) + if plugin.ft then + Loader.ftdetect(plugin.dir) + end end return M diff --git a/lua/lazy/core/handler/init.lua b/lua/lazy/core/handler/init.lua index fc023ef..4cca808 100644 --- a/lua/lazy/core/handler/init.lua +++ b/lua/lazy/core/handler/init.lua @@ -4,6 +4,7 @@ local Config = require("lazy.core.config") ---@field type LazyHandlerTypes ---@field extends? LazyHandler ---@field active table> +---@field super LazyHandler local M = {} ---@enum LazyHandlerTypes @@ -50,68 +51,44 @@ end function M.new(type) ---@type LazyHandler local handler = require("lazy.core.handler." .. type) - local self = setmetatable({}, { - __index = function(_, k) - return handler[k] or (handler.extends and handler.extends[k]) or M[k] - end, - }) + local super = handler.extends or M + local self = setmetatable({}, { __index = setmetatable(handler, { __index = super }) }) + self.super = super self.active = {} self.type = type - self:init() return self end +---@param value string ---@protected -function M:init() end +function M:_add(value) end + +---@param value string +---@protected +function M:_del(value) end ---@param plugin LazyPlugin ----@param value string ----@protected -function M:_add(plugin, value) end - ----@param plugin LazyPlugin ----@param value string ----@protected -function M:_del(plugin, 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) +function M:add(plugin) + for _, value in ipairs(plugin[self.type] or {}) do + if not self.active[value] then + self.active[value] = {} + self:_add(value) end + self.active[value][plugin.name] = plugin.name end end ----@param plugin LazyPlugin -function M:add(plugin) - self:foreach(plugin[self.type], function(value) - value = self:_value(value) - if not (self.active[value] and self.active[value][plugin.name]) then - self.active[value] = self.active[value] or {} - self.active[value][plugin.name] = plugin.name - self:_add(plugin, value) - end - end) -end - ---@param plugin LazyPlugin function M:del(plugin) - self:foreach(plugin[self.type], function(value) - value = self:_value(value) + for _, value in ipairs(plugin[self.type] or {}) do if self.active[value] and self.active[value][plugin.name] then 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 return M diff --git a/lua/lazy/core/handler/keys.lua b/lua/lazy/core/handler/keys.lua index 14827f2..301b408 100644 --- a/lua/lazy/core/handler/keys.lua +++ b/lua/lazy/core/handler/keys.lua @@ -4,22 +4,20 @@ local Loader = require("lazy.core.loader") ---@class LazyKeysHandler:LazyHandler local M = {} ----@param plugin LazyPlugin ---@param keys string -function M:_add(plugin, keys) +function M:_add(keys) vim.keymap.set("n", keys, function() vim.keymap.del("n", keys) Util.track({ keys = keys }) - Loader.load(plugin, { keys = keys }) + Loader.load(self.active[keys], { keys = keys }) vim.api.nvim_input(keys) Util.track() end) end ----@param _plugin LazyPlugin ----@param value string -function M:_del(_plugin, value) - pcall(vim.keymap.del, "n", value) +---@param keys string +function M:_del(keys) + pcall(vim.keymap.del, "n", keys) end return M diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index d41e2c7..2859465 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -21,10 +21,10 @@ local M = {} ---@field build? string|fun(LazyPlugin) ---@class LazyPluginHandlers: table ----@field event? string|string[] ----@field cmd? string|string[] ----@field ft? string|string[] ----@field keys? string|string[] +---@field event? string[] +---@field cmd? string[] +---@field ft? string[] +---@field keys? string[] ---@class LazyPluginRef ---@field branch? string @@ -101,6 +101,11 @@ function Spec:add(plugin, is_dep) Util.error("Invalid plugin spec " .. vim.inspect(plugin)) 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._.dep = is_dep diff --git a/lua/lazy/view/render.lua b/lua/lazy/view/render.lua index 886d324..ad4450f 100644 --- a/lua/lazy/view/render.lua +++ b/lua/lazy/view/render.lua @@ -242,8 +242,6 @@ function M:reason(reason, opts) end if key == "event" then value = value:match("User (.*)") or value - elseif key == "ft" then - value = value:match("FileType (.*)") or value end local hl = "LazyHandler" .. key:sub(1, 1):upper() .. key:sub(2) local icon = Config.options.ui.icons[key]