Undo and redo history for a buffer document.
The buffer process owns the live document and version counter. This struct remembers the patches needed to move backward and forward through document history, including the source of each edit for diagnostics and attribution.
This module is pure state. It does not mark buffers dirty, broadcast events, or mutate documents outside applying returned patches to produce a restore document.
Summary
Functions
Resets the coalescing timer so the next recorded edit creates a new undo entry.
Clears undo and redo entries and resets coalescing state.
Returns the coalescing window in milliseconds.
Returns the source of the most recent redo entry, or nil if redo is unavailable.
Returns the source of the most recent undo entry, or nil if undo is unavailable.
Creates an empty undo history.
Records an undo patch, coalescing rapid edits into one history entry.
Records one undo entry containing all patches in the batch.
Records an undo patch without time-based coalescing.
Returns the next document/version and updates history, or :empty when redo is unavailable.
Returns the previous document/version and updates history, or :empty when undo is unavailable.
Undoes all consecutive :agent-sourced entries from the top of the undo stack.
Types
@type edit_source() :: Minga.Buffer.EditSource.undo_source()
@type entry() :: {version(), patches(), edit_source()}
@type patches() :: [Minga.Buffer.UndoPatch.t(), ...]
@opaque t()
@type version() :: non_neg_integer()
Functions
Resets the coalescing timer so the next recorded edit creates a new undo entry.
Clears undo and redo entries and resets coalescing state.
@spec coalesce_ms() :: pos_integer()
Returns the coalescing window in milliseconds.
@spec last_redo_source(t()) :: edit_source() | nil
Returns the source of the most recent redo entry, or nil if redo is unavailable.
@spec last_undo_source(t()) :: edit_source() | nil
Returns the source of the most recent undo entry, or nil if undo is unavailable.
@spec new() :: t()
Creates an empty undo history.
@spec record_edit(t(), version(), Minga.Buffer.UndoPatch.t(), edit_source()) :: t()
Records an undo patch, coalescing rapid edits into one history entry.
@spec record_edit_batch(t(), version(), patches(), edit_source()) :: t()
Records one undo entry containing all patches in the batch.
@spec record_edit_force(t(), version(), Minga.Buffer.UndoPatch.t(), edit_source()) :: t()
Records an undo patch without time-based coalescing.
@spec redo(t(), version(), Minga.Buffer.Document.t()) :: {:ok, Minga.Buffer.UndoHistory.Restore.t(), t()} | :empty
Returns the next document/version and updates history, or :empty when redo is unavailable.
@spec undo(t(), version(), Minga.Buffer.Document.t()) :: {:ok, Minga.Buffer.UndoHistory.Restore.t(), t()} | :empty
Returns the previous document/version and updates history, or :empty when undo is unavailable.
@spec undo_agent_session(t(), version(), Minga.Buffer.Document.t()) :: {:ok, Minga.Buffer.UndoHistory.Restore.t(), t(), pos_integer()} | :empty
Undoes all consecutive :agent-sourced entries from the top of the undo stack.
Walks backward through the undo stack, popping every entry whose source is :agent, applying its patches to roll back the document, and pushing corresponding redo entries. Stops at the first non-agent entry or when the stack is empty.
Returns {:ok, restore, updated_history, undone_count} with the final document state after all agent entries are reversed, or :empty when no agent entries are on top.