From 2da7462a7728214ee5dd103fcc8a65526079e9da Mon Sep 17 00:00:00 2001 From: David Mynors Date: Sat, 21 Mar 2026 15:24:47 +0000 Subject: [PATCH] fix: defer TabClosed cleanup with vim.schedule to avoid E5108 The TabClosed autocmd runs cleanup_diff synchronously, which calls welcome_window.apply_opts, which sets window options via vim.wo. This requires switching windows internally, which is forbidden during TabClosed. Wrapping in vim.schedule defers the cleanup until after the event, matching the existing pattern used by the WinClosed handler. Fixes "Problem while switching windows" error when using open_in_prev_tab with close_on_open_in_prev_tab enabled. --- lua/codediff/ui/lifecycle/cleanup.lua | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lua/codediff/ui/lifecycle/cleanup.lua b/lua/codediff/ui/lifecycle/cleanup.lua index 40679055..4bc9f164 100644 --- a/lua/codediff/ui/lifecycle/cleanup.lua +++ b/lua/codediff/ui/lifecycle/cleanup.lua @@ -176,19 +176,21 @@ function M.setup_autocmds() vim.api.nvim_create_autocmd("TabClosed", { group = augroup, callback = function() - -- TabClosed doesn't give us the tab number, so we need to scan - -- Remove any diffs for tabs that no longer exist - local valid_tabs = {} - for _, tabpage in ipairs(vim.api.nvim_list_tabpages()) do - valid_tabs[tabpage] = true - end + vim.schedule(function() + -- TabClosed doesn't give us the tab number, so we need to scan + -- Remove any diffs for tabs that no longer exist + local valid_tabs = {} + for _, tabpage in ipairs(vim.api.nvim_list_tabpages()) do + valid_tabs[tabpage] = true + end - local active_diffs = session.get_active_diffs() - for tabpage, _ in pairs(active_diffs) do - if not valid_tabs[tabpage] then - cleanup_diff(tabpage) + local active_diffs = session.get_active_diffs() + for tabpage, _ in pairs(active_diffs) do + if not valid_tabs[tabpage] then + cleanup_diff(tabpage) + end end - end + end) end, })