Skip to content

Commit 5510317

Browse files
authored
Relocate buffer view when reloading file (zyedidia#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.
1 parent 169a9a6 commit 5510317

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

internal/action/bufpane.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ func (h *BufPane) Name() string {
415415
return n
416416
}
417417

418+
// ReOpen reloads the file opened in the bufpane from disk
419+
func (h *BufPane) ReOpen() {
420+
h.Buf.ReOpen()
421+
h.Relocate()
422+
}
423+
418424
func (h *BufPane) getReloadSetting() string {
419425
reloadSetting := h.Buf.Settings["reload"]
420426
return reloadSetting.(string)
@@ -433,11 +439,11 @@ func (h *BufPane) HandleEvent(event tcell.Event) {
433439
if !yes || canceled {
434440
h.Buf.UpdateModTime()
435441
} else {
436-
h.Buf.ReOpen()
442+
h.ReOpen()
437443
}
438444
})
439445
} else if reload == "auto" {
440-
h.Buf.ReOpen()
446+
h.ReOpen()
441447
} else if reload == "disabled" {
442448
h.Buf.DisableReload()
443449
} else {

internal/action/command.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,13 @@ func (h *BufPane) ReopenCmd(args []string) {
403403
InfoBar.YNPrompt("Save file before reopen?", func(yes, canceled bool) {
404404
if !canceled && yes {
405405
h.Save()
406-
h.Buf.ReOpen()
406+
h.ReOpen()
407407
} else if !canceled {
408-
h.Buf.ReOpen()
408+
h.ReOpen()
409409
}
410410
})
411411
} else {
412-
h.Buf.ReOpen()
412+
h.ReOpen()
413413
}
414414
}
415415

0 commit comments

Comments
 (0)