text_delta v1.4.0 TextDelta.Application View Source

The application of a delta onto a text state.

Text state is always represented as a set of TextDelta.Operation.insert/0 operations. This means that any application should always result in a set of insert operations or produce an error tuple.

In simpler terms this means that it is not possible to apply delta, which combined length of retain and delete operations is longer than the length of original text. This situation will always result in :length_mismatch error.

Link to this section Summary

Types

Reason for an application error

Result of an application

Functions

Applies given delta to a particular text state, resulting in a new state

Applies given delta to a particular text state, resulting in a new state

Link to this section Types

Link to this type error_reason() View Source
error_reason() :: :length_mismatch

Reason for an application error.

Link to this type result() View Source
result() :: {:ok, TextDelta.state()} | {:error, error_reason()}

Result of an application.

An ok/error tuple. Represents either a successful application in form of {:ok, new_state} or an error in form of {:error, reason}.

Link to this section Functions

Applies given delta to a particular text state, resulting in a new state.

Text state is a set of TextDelta.Operation.insert/0 operations. If applying delta results in anything but a set of insert operations, :error tuple is returned instead.

Examples

successful application:

iex> doc = TextDelta.insert(TextDelta.new(), "hi")
%TextDelta{ops: [%{insert: "hi"}]}
iex> TextDelta.apply(doc, TextDelta.insert(TextDelta.new(), "oh, "))
{:ok, %TextDelta{ops: [%{insert: "oh, hi"}]}}

error handling:

iex> doc = TextDelta.insert(TextDelta.new(), "hi")
%TextDelta{ops: [%{insert: "hi"}]}
iex> TextDelta.apply(doc, TextDelta.delete(TextDelta.new(), 5))
{:error, :length_mismatch}

Applies given delta to a particular text state, resulting in a new state.

Equivalent to &TextDelta.Application.apply/2, but instead of returning ok/error tuples returns a new state or raises a RuntimeError.