From a9db6ec6fa127b57912bc59e5d03f7e99d22607f Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Thu, 23 Oct 2025 17:50:03 +0300 Subject: feat(pack): add `active` field to `PackChanged` event data Problem: Inside `PackChanged[Pre]` callbacks it might be useful to tell if the affected plugin is active or not. It is already possible via extra `vim.pack.get({ 'plug-name' })[1].active`, but it is not quite user-friendly for something that might be needed frequently in real world use cases. Solution: Supply extra `active` event data field. --- runtime/lua/vim/pack.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'runtime/lua/vim/pack.lua') diff --git a/runtime/lua/vim/pack.lua b/runtime/lua/vim/pack.lua index 1a0a3c4540..f17fce56a9 100644 --- a/runtime/lua/vim/pack.lua +++ b/runtime/lua/vim/pack.lua @@ -97,6 +97,7 @@ --- - [PackChanged]() - after plugin's state has changed. --- --- Each event populates the following |event-data| fields: +--- - `active` - whether plugin was added via |vim.pack.add()| to current session. --- - `kind` - one of "install" (install on disk), "update" (update existing --- plugin), "delete" (delete from disk). --- - `spec` - plugin's specification with defaults made explicit. @@ -408,11 +409,18 @@ local function plug_list_from_names(names) return plugs end +--- Map from plugin path to its data. +--- Use map and not array to avoid linear lookup during startup. +--- @type table +local active_plugins = {} +local n_active_plugins = 0 + --- @param p vim.pack.Plug --- @param event_name 'PackChangedPre'|'PackChanged' --- @param kind 'install'|'update'|'delete' local function trigger_event(p, event_name, kind) - local data = { kind = kind, spec = vim.deepcopy(p.spec), path = p.path } + local active = active_plugins[p.path] ~= nil + local data = { active = active, kind = kind, spec = vim.deepcopy(p.spec), path = p.path } api.nvim_exec_autocmds(event_name, { pattern = p.path, data = data }) end @@ -686,12 +694,6 @@ local function infer_update_details(p) p.info.update_details = table.concat(newer_semver_tags, '\n') end ---- Map from plugin path to its data. ---- Use map and not array to avoid linear lookup during startup. ---- @type table -local active_plugins = {} -local n_active_plugins = 0 - --- @param plug vim.pack.Plug --- @param load boolean|fun(plug_data: {spec: vim.pack.Spec, path: string}) local function pack_add(plug, load) -- cgit