Improve Undo & Redo actions return values

Return false if there is nothing to undo/redo.

This also fixes false "Undid action" and "Redid actions" infobar
messages in the case when no action was actually undone or redone.
This commit is contained in:
Dmytro Maluka
2024-06-15 01:00:36 +02:00
parent 762e31f2fd
commit 781f057e6f
2 changed files with 18 additions and 12 deletions

View File

@@ -1163,7 +1163,9 @@ func (h *BufPane) DiffPrevious() bool {
// Undo undoes the last action // Undo undoes the last action
func (h *BufPane) Undo() bool { func (h *BufPane) Undo() bool {
h.Buf.Undo() if !h.Buf.Undo() {
return false
}
InfoBar.Message("Undid action") InfoBar.Message("Undid action")
h.Relocate() h.Relocate()
return true return true
@@ -1171,7 +1173,9 @@ func (h *BufPane) Undo() bool {
// Redo redoes the last action // Redo redoes the last action
func (h *BufPane) Redo() bool { func (h *BufPane) Redo() bool {
h.Buf.Redo() if !h.Buf.Redo() {
return false
}
InfoBar.Message("Redid action") InfoBar.Message("Redid action")
h.Relocate() h.Relocate()
return true return true

View File

@@ -253,11 +253,11 @@ func (eh *EventHandler) Execute(t *TextEvent) {
ExecuteTextEvent(t, eh.buf) ExecuteTextEvent(t, eh.buf)
} }
// Undo the first event in the undo stack // Undo the first event in the undo stack. Returns false if the stack is empty.
func (eh *EventHandler) Undo() { func (eh *EventHandler) Undo() bool {
t := eh.UndoStack.Peek() t := eh.UndoStack.Peek()
if t == nil { if t == nil {
return return false
} }
startTime := t.Time.UnixNano() / int64(time.Millisecond) startTime := t.Time.UnixNano() / int64(time.Millisecond)
@@ -266,15 +266,16 @@ func (eh *EventHandler) Undo() {
for { for {
t = eh.UndoStack.Peek() t = eh.UndoStack.Peek()
if t == nil { if t == nil {
return break
} }
if t.Time.UnixNano()/int64(time.Millisecond) < endTime { if t.Time.UnixNano()/int64(time.Millisecond) < endTime {
return break
} }
eh.UndoOneEvent() eh.UndoOneEvent()
} }
return true
} }
// UndoOneEvent undoes one event // UndoOneEvent undoes one event
@@ -303,11 +304,11 @@ func (eh *EventHandler) UndoOneEvent() {
eh.RedoStack.Push(t) eh.RedoStack.Push(t)
} }
// Redo the first event in the redo stack // Redo the first event in the redo stack. Returns false if the stack is empty.
func (eh *EventHandler) Redo() { func (eh *EventHandler) Redo() bool {
t := eh.RedoStack.Peek() t := eh.RedoStack.Peek()
if t == nil { if t == nil {
return return false
} }
startTime := t.Time.UnixNano() / int64(time.Millisecond) startTime := t.Time.UnixNano() / int64(time.Millisecond)
@@ -316,15 +317,16 @@ func (eh *EventHandler) Redo() {
for { for {
t = eh.RedoStack.Peek() t = eh.RedoStack.Peek()
if t == nil { if t == nil {
return break
} }
if t.Time.UnixNano()/int64(time.Millisecond) > endTime { if t.Time.UnixNano()/int64(time.Millisecond) > endTime {
return break
} }
eh.RedoOneEvent() eh.RedoOneEvent()
} }
return true
} }
// RedoOneEvent redoes one event // RedoOneEvent redoes one event