Skip to content

Commit 781f057

Browse files
committed
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.
1 parent 762e31f commit 781f057

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

internal/action/actions.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,15 +1163,19 @@ func (h *BufPane) DiffPrevious() bool {
11631163

11641164
// Undo undoes the last action
11651165
func (h *BufPane) Undo() bool {
1166-
h.Buf.Undo()
1166+
if !h.Buf.Undo() {
1167+
return false
1168+
}
11671169
InfoBar.Message("Undid action")
11681170
h.Relocate()
11691171
return true
11701172
}
11711173

11721174
// Redo redoes the last action
11731175
func (h *BufPane) Redo() bool {
1174-
h.Buf.Redo()
1176+
if !h.Buf.Redo() {
1177+
return false
1178+
}
11751179
InfoBar.Message("Redid action")
11761180
h.Relocate()
11771181
return true

internal/buffer/eventhandler.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ func (eh *EventHandler) Execute(t *TextEvent) {
253253
ExecuteTextEvent(t, eh.buf)
254254
}
255255

256-
// Undo the first event in the undo stack
257-
func (eh *EventHandler) Undo() {
256+
// Undo the first event in the undo stack. Returns false if the stack is empty.
257+
func (eh *EventHandler) Undo() bool {
258258
t := eh.UndoStack.Peek()
259259
if t == nil {
260-
return
260+
return false
261261
}
262262

263263
startTime := t.Time.UnixNano() / int64(time.Millisecond)
@@ -266,15 +266,16 @@ func (eh *EventHandler) Undo() {
266266
for {
267267
t = eh.UndoStack.Peek()
268268
if t == nil {
269-
return
269+
break
270270
}
271271

272272
if t.Time.UnixNano()/int64(time.Millisecond) < endTime {
273-
return
273+
break
274274
}
275275

276276
eh.UndoOneEvent()
277277
}
278+
return true
278279
}
279280

280281
// UndoOneEvent undoes one event
@@ -303,11 +304,11 @@ func (eh *EventHandler) UndoOneEvent() {
303304
eh.RedoStack.Push(t)
304305
}
305306

306-
// Redo the first event in the redo stack
307-
func (eh *EventHandler) Redo() {
307+
// Redo the first event in the redo stack. Returns false if the stack is empty.
308+
func (eh *EventHandler) Redo() bool {
308309
t := eh.RedoStack.Peek()
309310
if t == nil {
310-
return
311+
return false
311312
}
312313

313314
startTime := t.Time.UnixNano() / int64(time.Millisecond)
@@ -316,15 +317,16 @@ func (eh *EventHandler) Redo() {
316317
for {
317318
t = eh.RedoStack.Peek()
318319
if t == nil {
319-
return
320+
break
320321
}
321322

322323
if t.Time.UnixNano()/int64(time.Millisecond) > endTime {
323-
return
324+
break
324325
}
325326

326327
eh.RedoOneEvent()
327328
}
329+
return true
328330
}
329331

330332
// RedoOneEvent redoes one event

0 commit comments

Comments
 (0)