Skip to main content

UndoTree

Struct UndoTree 

Source
pub struct UndoTree { /* private fields */ }
Expand description

Undo tree with branching support.

Unlike a simple undo stack, an undo tree preserves all history. When you undo and then make new changes, a new branch is created rather than discarding the undone changes.

§Vim Comparison

This is similar to vim’s :undotree feature. The g- and g+ commands traverse changes in time order (seq_num), while u and Ctrl-R traverse the tree structure.

§Memory Management

The tree has a configurable maximum number of nodes. When exceeded, the oldest nodes are pruned (but the path from root to current is always preserved).

Implementations§

Source§

impl UndoTree

Source

pub const DEFAULT_MAX_NODES: usize = 10000

Default maximum number of nodes.

Source

pub fn new() -> Self

Create a new undo tree.

The tree starts with a root node representing the initial state.

Source

pub fn with_max_nodes(max_nodes: usize) -> Self

Create a new undo tree with custom maximum nodes.

Source

pub fn push( &mut self, edits: Vec<Edit>, cursor_before: Position, cursor_after: Position, )

Push a new change onto the tree with system origin.

Creates a new node as a child of the current node. If the current node already has children (we’re in the middle of the tree after an undo), this creates a new branch.

This is equivalent to push_with_origin(edits, cursor_before, cursor_after, EditOrigin::System).

Source

pub fn push_with_origin( &mut self, edits: Vec<Edit>, cursor_before: Position, cursor_after: Position, origin: EditOrigin, )

Push a new change onto the tree with specified origin.

Creates a new node as a child of the current node, tagged with the specified origin. This enables per-client undo where each client can undo only their own changes.

If the current node already has children (we’re in the middle of the tree after an undo), this creates a new branch.

Source

pub fn undo(&mut self) -> Option<UndoResult>

Undo the current change.

Moves to the parent node and returns the inverse edits along with the cursor position to restore.

Returns None if at the root (nothing to undo).

Source

pub fn redo(&mut self) -> Option<UndoResult>

Redo the last undone change.

Moves to the first child node (or the active branch) and returns the edits along with cursor position to restore.

Returns None if there’s nothing to redo.

Source

pub fn redo_branch(&mut self, branch_idx: usize) -> Option<UndoResult>

Redo a specific branch.

Like redo() but follows a specific branch index.

Source

pub fn branches(&self) -> &[usize]

Get the available branches (children) from current node.

Returns indices that can be passed to redo_branch().

Source

pub fn switch_branch(&mut self, branch_idx: usize) -> bool

Switch the active branch at current node.

This affects which branch redo() will follow. Returns false if the branch index is invalid.

Source

pub fn can_undo(&self) -> bool

Check if undo is possible.

Source

pub fn can_redo(&self) -> bool

Check if redo is possible.

Source

pub fn current_node(&self) -> &UndoNode

Get the current node.

Source

pub const fn current_index(&self) -> usize

Get the current node index.

Source

pub const fn node_count(&self) -> usize

Get total number of nodes in the tree.

Source

pub fn node(&self, index: usize) -> Option<&UndoNode>

Get node by index.

Returns None if index is out of bounds.

Source

pub const fn node_indices(&self) -> Range<usize>

Get all node indices for iteration.

Returns a range that can be used to iterate over all valid node indices.

Source

pub fn active_branch_at(&self, node_index: usize) -> Option<usize>

Get active branch index at a node.

Returns the preferred child index that redo() would follow from the specified node. Returns None if the node doesn’t exist or has no active branch set.

Source

pub const fn max_nodes(&self) -> usize

Get the maximum nodes limit.

Source

pub fn set_max_nodes(&mut self, max: usize)

Set the maximum nodes limit.

Source

pub const fn seq_counter(&self) -> u64

Get the current sequential change number.

Source

pub fn edits_since(&self, from_idx: usize) -> Option<Vec<&Edit>>

Collect all edits applied between a node and the current head.

Walks from the current position back to from_idx via parent links, collecting all edits from intermediate nodes in forward (application) order. The edits from the from_idx node itself are NOT included.

Returns Some(empty_vec) if from_idx is the current position. Returns None if from_idx is not an ancestor of the current position or is out of bounds.

Source

pub fn clear(&mut self)

Clear all history except root.

Source

pub fn from_serializable( nodes_data: Vec<(Vec<Edit>, Position, Position, Duration, Option<usize>, Vec<usize>, u64, EditOrigin)>, current: usize, seq_counter: u64, max_nodes: usize, active_branches: Vec<usize>, ) -> Self

Reconstruct an UndoTree from serialized data.

This is used by the persistence layer to restore undo history from disk. Timestamps are reconstructed relative to “now” using the provided durations.

§Arguments
  • nodes_data - Vector of tuples containing node data:
    • Vec<Edit>: The edits in this node
    • Position: Cursor before edits
    • Position: Cursor after edits
    • Duration: Relative time from tree creation
    • Option<usize>: Parent node index
    • Vec<usize>: Child node indices
    • u64: Sequential change number
    • EditOrigin: Origin of this edit
  • current - Index of current position in tree
  • seq_counter - Current sequential change counter
  • max_nodes - Maximum nodes to retain
  • active_branches - Preferred branch at each node
§Panics

Panics if nodes_data is empty (tree must have at least a root node).

Trait Implementations§

Source§

impl Clone for UndoTree

Source§

fn clone(&self) -> UndoTree

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for UndoTree

Source§

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

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

impl Default for UndoTree

Source§

fn default() -> Self

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.