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

View File

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