From 55103179426f50f28df03825f4607d6470a8c1e4 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 21 Apr 2024 22:49:01 +0200 Subject: [PATCH] Relocate buffer view when reloading file (#3250) After reloading a file that has been externally modified, the buffer view may become invalid: the displayed subset of lines of the file may no longer exist, since the file may have been truncated. So relocate the buffer view in this case. In particular, this fixes crashes caused by out of bounds accesses to the line array by displayBuffer() trying to display no longer existing lines. --- internal/action/bufpane.go | 10 ++++++++-- internal/action/command.go | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/action/bufpane.go b/internal/action/bufpane.go index 73574859..cd53e688 100644 --- a/internal/action/bufpane.go +++ b/internal/action/bufpane.go @@ -415,6 +415,12 @@ func (h *BufPane) Name() string { return n } +// ReOpen reloads the file opened in the bufpane from disk +func (h *BufPane) ReOpen() { + h.Buf.ReOpen() + h.Relocate() +} + func (h *BufPane) getReloadSetting() string { reloadSetting := h.Buf.Settings["reload"] return reloadSetting.(string) @@ -433,11 +439,11 @@ func (h *BufPane) HandleEvent(event tcell.Event) { if !yes || canceled { h.Buf.UpdateModTime() } else { - h.Buf.ReOpen() + h.ReOpen() } }) } else if reload == "auto" { - h.Buf.ReOpen() + h.ReOpen() } else if reload == "disabled" { h.Buf.DisableReload() } else { diff --git a/internal/action/command.go b/internal/action/command.go index 43125213..4e82007a 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -403,13 +403,13 @@ func (h *BufPane) ReopenCmd(args []string) { InfoBar.YNPrompt("Save file before reopen?", func(yes, canceled bool) { if !canceled && yes { h.Save() - h.Buf.ReOpen() + h.ReOpen() } else if !canceled { - h.Buf.ReOpen() + h.ReOpen() } }) } else { - h.Buf.ReOpen() + h.ReOpen() } }