Skip to main content

UndoStack

Struct UndoStack 

Source
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

Source

pub fn new() -> Self

An empty stack.

Source

pub fn push(&mut self, entry: UndoEntry)

Record a new undo entry. Any pending redo history is dropped.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn redo_depth(&self) -> usize

Number of entries available to redo.

Trait Implementations§

Source§

impl Clone for UndoStack

Source§

fn clone(&self) -> UndoStack

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UndoStack

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for UndoStack

Source§

fn default() -> UndoStack

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.