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, })