fix: handler resilience against trigger_load calls in hooks

This commit is contained in:
Marc Jakobi
2024-08-28 12:56:02 +02:00
committed by Marc Jakobi
parent 8c89ac4680
commit 163b2471e2
4 changed files with 46 additions and 13 deletions
+16 -4
View File
@@ -17,9 +17,21 @@ function M.lookup(name)
end
---@param cmd string
---@return string[] loaded_plugin_names
local function load(cmd)
vim.api.nvim_del_user_command(cmd)
loader.load(pending[cmd])
local plugins = pending[cmd]
-- Make sure trigger_load calls in before hooks can't interfere with the state,
-- but they can load a plugin before it's loaded by this handler
vim
.iter(vim.deepcopy(pending[cmd]))
---@param plugin lz.n.Plugin
:each(function(_, plugin)
if pending[cmd][plugin.name] then
loader.load(plugin)
end
end)
return vim.tbl_keys(plugins)
end
---@param cmd string
@@ -44,14 +56,14 @@ local function add_cmd(cmd)
command.range = { event.line1, event.line2 }
end
load(cmd)
local plugin_names = load(cmd)
local info = vim.api.nvim_get_commands({})[cmd] or vim.api.nvim_buf_get_commands(0, {})[cmd]
if not info then
vim.schedule(function()
---@type string
local plugins = "`" .. table.concat(vim.tbl_values(pending[cmd]), ", ") .. "`"
vim.notify("Command `" .. cmd .. "` not found after loading " .. plugins, vim.log.levels.ERROR)
local plugins_str = "`" .. table.concat(plugin_names, ", ") .. "`"
vim.notify("Command `" .. cmd .. "` not found after loading " .. plugins_str, vim.log.levels.ERROR)
end)
return
end
+10 -1
View File
@@ -32,7 +32,16 @@ local function on_colorscheme(name)
-- already loaded
return
end
loader.load(plugins)
-- Make sure trigger_load calls in before hooks can't interfere with the state,
-- but they can load a plugin before it's loaded by this handler
vim
.iter(vim.deepcopy(pending[name]))
---@param plugin lz.n.Plugin
:each(function(_, plugin)
if pending[name][plugin.name] then
loader.load(plugin)
end
end)
end
local function init()
+10 -3
View File
@@ -158,9 +158,16 @@ local function add_event(event)
-- HACK: work-around for https://github.com/neovim/neovim/issues/25526
done = true
local state = get_state(ev.event, ev.buf, ev.data)
-- load the plugins
loader.load(pending[event.id])
-- check if any plugin created an event handler for this event and fire the group
-- Make sure trigger_load calls in before hooks can't interfere with the state,
-- but they can load a plugin before it's loaded by this handler
vim
.iter(vim.deepcopy(pending[event.id]))
---@param plugin lz.n.Plugin
:each(function(_, plugin)
if pending[event.id][plugin.name] then
loader.load(plugin)
end
end)
---@param s lz.n.EventOpts
vim.iter(state):each(function(s)
trigger(s)
+10 -5
View File
@@ -101,13 +101,18 @@ local function add_keys(keys)
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
local plugins = pending[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
pending[keys.id] = nil
if plugins then
loader.load(plugins)
end
-- Make sure trigger_load calls in before hooks can't interfere with the state,
-- but they can load a plugin before it's loaded by this handler
vim
.iter(vim.deepcopy(pending[keys.id]))
---@param plugin lz.n.Plugin
:each(function(_, plugin)
if pending[keys.id][plugin.name] then
loader.load(plugin)
end
end)
-- Create the real buffer-local mapping
if keys.ft then
set(keys, buf)