From 3a2b9d824978cfa746d8b4ac088baaffbe01800b Mon Sep 17 00:00:00 2001 From: David Yonge-Mallo Date: Fri, 20 Mar 2026 08:43:45 +0100 Subject: [PATCH] fix(view): skip redundant buffer re-open on update, fix window rebalance --- lua/diffview/scene/views/diff/diff_view.lua | 14 +++++++++++++- lua/diffview/ui/panel.lua | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lua/diffview/scene/views/diff/diff_view.lua b/lua/diffview/scene/views/diff/diff_view.lua index 2acf5931..72172590 100644 --- a/lua/diffview/scene/views/diff/diff_view.lua +++ b/lua/diffview/scene/views/diff/diff_view.lua @@ -483,6 +483,8 @@ DiffView.update_files = debounce.debounce_trailing( perf:lap("received new file list") + local prev_cur_file = self.panel.cur_file + local files = { { cur_files = self.files.conflicting, new_files = new_files.conflicting }, { cur_files = self.files.working, new_files = new_files.working }, @@ -644,7 +646,17 @@ DiffView.update_files = debounce.debounce_trailing( self.panel:render() self.panel:redraw() - self:set_file(self.panel.cur_file or self.panel:next_file(), false, not self.initialized or nil) + local next_file = self.panel.cur_file or self.panel:next_file() + + -- Only re-open the current entry when something actually changed: + -- first init, cur_file identity changed, or buffers were invalidated. + local needs_reopen = not self.initialized + or next_file ~= prev_cur_file + or not (self.cur_layout and self.cur_layout:is_valid() and self.cur_layout:is_files_loaded()) + + if needs_reopen then + self:set_file(next_file, false, not self.initialized or nil) + end -- Position cursor at the requested row on first open. if not self.initialized and self.options.selected_row then diff --git a/lua/diffview/ui/panel.lua b/lua/diffview/ui/panel.lua index 92d6a875..5298a754 100644 --- a/lua/diffview/ui/panel.lua +++ b/lua/diffview/ui/panel.lua @@ -262,7 +262,7 @@ function Panel:resize() width = math.min(width, max_width) local ok = pcall(api.nvim_win_set_width, self.winid, width) if ok and api.nvim_win_get_width(self.winid) ~= old_width then - vim.cmd("vertical wincmd =") + vim.cmd("wincmd =") -- Re-render so that header lines (path, revision info, etc.) are -- truncated to the actual panel width rather than left at full length. self:render()