pub struct UndoStack { /* private fields */ }Expand description
A linear undo / redo stack of UndoEntrys.
This is a passive container: it never touches a buffer. The owner (in
practice Document) applies the popped entry’s
edits and records the resulting inverse on the other side — see
Self::pop_for_undo / Self::record_redo. Pushing a new entry
discards redo history (no undo tree yet).
§Examples
use lattice_core::{UndoEntry, UndoStack};
let entry = |label: &str| UndoEntry { inverse_edits: vec![], label: label.into() };
let mut stack = UndoStack::new();
stack.push(entry("a"));
stack.push(entry("b"));
// Undo "b": pop it, apply its edits (elided), record the redo side.
let undone = stack.pop_for_undo().map(|e| e.label);
assert_eq!(undone.as_deref(), Some("b"));
stack.record_redo(entry("b"));
assert_eq!((stack.undo_depth(), stack.redo_depth()), (1, 1));
// A fresh edit drops the redo history.
stack.push(entry("c"));
assert_eq!((stack.undo_depth(), stack.redo_depth()), (2, 0));Implementations§
Source§impl UndoStack
impl UndoStack
Sourcepub fn push(&mut self, entry: UndoEntry)
pub fn push(&mut self, entry: UndoEntry)
Record a new undo entry. Any pending redo history is dropped.
Sourcepub fn amend_top(&mut self, inverses: Vec<Edit>)
pub fn amend_top(&mut self, inverses: Vec<Edit>)
Fold inverses into the most recent undo entry instead of
pushing a new one – the primitive behind undo-group coalescing
(a vim insert session collapses to a single undo unit). The
caller passes the just-applied operation’s inverse edits in the
same stored order push would use (reverse-application order);
they are prepended so the combined entry still replays
newest -> oldest during undo (inv(eN) .. inv(e1)).
Redo is intentionally not cleared: an amend never diverges the
history (the initiating push that opened the group already
cleared redo, and no redo can accrue mid-group). If there is no
top entry to amend – which the group bookkeeping is meant to
prevent – it falls back to a plain push so the edit stays
undoable rather than being silently lost.
Sourcepub fn pop_for_undo(&mut self) -> Option<UndoEntry>
pub fn pop_for_undo(&mut self) -> Option<UndoEntry>
Pop the most recent undo entry, or None if there is none.
This does not touch the redo stack: the caller applies
entry.inverse_edits to the buffer and passes the resulting
“inverse-of-the-inverse” back via Self::record_redo.
Sourcepub fn record_redo(&mut self, redo_entry: UndoEntry)
pub fn record_redo(&mut self, redo_entry: UndoEntry)
Reciprocal of pop_for_undo. Stores the edit set that would replay the
undone operation onto the redo stack.
Sourcepub fn pop_for_redo(&mut self) -> Option<UndoEntry>
pub fn pop_for_redo(&mut self) -> Option<UndoEntry>
Pop the most recent redo entry, or None if there is none. Like
Self::pop_for_undo, the caller applies it and records the result
via Self::record_undo.
Sourcepub fn record_undo(&mut self, undo_entry: UndoEntry)
pub fn record_undo(&mut self, undo_entry: UndoEntry)
Reciprocal of Self::pop_for_redo: push the edit set that undoes a
just-redone operation. Unlike Self::push it leaves the redo stack
intact, so further redos remain available.
Sourcepub fn undo_depth(&self) -> usize
pub fn undo_depth(&self) -> usize
Number of entries available to undo. Document
compares it against the depth recorded at save time to decide
dirtiness.
Sourcepub fn redo_depth(&self) -> usize
pub fn redo_depth(&self) -> usize
Number of entries available to redo.