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.
This commit is contained in:
Dmytro Maluka
2024-04-21 22:49:01 +02:00
committed by GitHub
parent 169a9a65fa
commit 5510317942
2 changed files with 11 additions and 5 deletions

View File

@@ -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 {

View File

@@ -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()
}
}