text_delta v1.2.0 TextDelta.Application

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.

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

Types

error_reason()
error_reason() :: :length_mismatch

Reason for an application error.

result()
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}.

Functions

apply(state, delta)

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}
apply!(state, delta)
apply!(TextDelta.state, TextDelta.t) ::
  TextDelta.state |
  no_return

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.