diff --git a/lua/lz/n/handler/cmd.lua b/lua/lz/n/handler/cmd.lua index f4de88c..2708f09 100644 --- a/lua/lz/n/handler/cmd.lua +++ b/lua/lz/n/handler/cmd.lua @@ -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 diff --git a/lua/lz/n/handler/colorscheme.lua b/lua/lz/n/handler/colorscheme.lua index 8aa2c08..3d1c79f 100644 --- a/lua/lz/n/handler/colorscheme.lua +++ b/lua/lz/n/handler/colorscheme.lua @@ -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() diff --git a/lua/lz/n/handler/event.lua b/lua/lz/n/handler/event.lua index 541aa9f..f4e6e7a 100644 --- a/lua/lz/n/handler/event.lua +++ b/lua/lz/n/handler/event.lua @@ -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) diff --git a/lua/lz/n/handler/keys.lua b/lua/lz/n/handler/keys.lua index 34525f7..df76876 100644 --- a/lua/lz/n/handler/keys.lua +++ b/lua/lz/n/handler/keys.lua @@ -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)