Minga.Buffer.UndoHistory (Minga v0.1.0)

Copy Markdown View Source

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

edit_source()

@type edit_source() :: Minga.Buffer.EditSource.undo_source()

entry()

@type entry() :: {version(), patches(), edit_source()}

patches()

@type patches() :: [Minga.Buffer.UndoPatch.t(), ...]

t()

@opaque t()

version()

@type version() :: non_neg_integer()

Functions

break_coalescing(history)

@spec break_coalescing(t()) :: t()

Resets the coalescing timer so the next recorded edit creates a new undo entry.

clear(history)

@spec clear(t()) :: t()

Clears undo and redo entries and resets coalescing state.

coalesce_ms()

@spec coalesce_ms() :: pos_integer()

Returns the coalescing window in milliseconds.

last_redo_source(undo_history)

@spec last_redo_source(t()) :: edit_source() | nil

Returns the source of the most recent redo entry, or nil if redo is unavailable.

last_undo_source(undo_history)

@spec last_undo_source(t()) :: edit_source() | nil

Returns the source of the most recent undo entry, or nil if undo is unavailable.

new()

@spec new() :: t()

Creates an empty undo history.

record_edit(history, version, patch, source)

@spec record_edit(t(), version(), Minga.Buffer.UndoPatch.t(), edit_source()) :: t()

Records an undo patch, coalescing rapid edits into one history entry.

record_edit_batch(history, version, patches, source)

@spec record_edit_batch(t(), version(), patches(), edit_source()) :: t()

Records one undo entry containing all patches in the batch.

record_edit_force(history, version, patch, source)

@spec record_edit_force(t(), version(), Minga.Buffer.UndoPatch.t(), edit_source()) ::
  t()

Records an undo patch without time-based coalescing.

redo(history, current_version, current_document)

@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.

undo(history, current_version, current_document)

@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.

undo_agent_session(history, current_version, current_document)

@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.