View Source PieceTable (piece_table v0.1.2)

The PieceTable module provides a naive implementation of the piece-table data structure for efficient text editing operations.

A piece-table represents an editable buffer of text as a sequence of non-overlapping pieces, allowing efficient inserts, deletes, and modifications.

usage

Usage

iex> table = PieceTable.new!("Hello, world!")
iex> table = PieceTable.insert!(table, "you ", 7)
iex> table = PieceTable.delete!(table, 10, 6)
iex> table = PieceTable.undo!(table)
iex> table = PieceTable.redo!(table)
iex> PieceTable.get_text!(table)
"Hello, you!"

Link to this section Summary

Functions

Deletes a substring from the PieceTable starting at a specified position and with a specified length.

Deletes a substring from the PieceTable starting at a specified position and with a specified length.

Retrieves the entire text content from the PieceTable.

Retrieves the entire text content from the PieceTable.

Inserts text into the PieceTable at a specified position.

Inserts text into the PieceTable at a specified position.

Creates a new PieceTable struct. This is intended the only method to build it.

Creates a new PieceTable struct. This is intended the only method to build it.

Redo the next change previously undone to the string.

Redo the next change previously undone to the string.

Undo the latest change applied to the string.

Undo the latest change applied to the string.

Link to this section Types

@type t() :: %PieceTable{
  applied: [tuple()],
  original: String.t(),
  result: String.t(),
  to_apply: [tuple()]
}

Link to this section Functions

Link to this function

delete(table, position, length)

View Source
@spec delete(t(), integer(), integer()) :: {:ok, t()} | {:error, String.t()}

Deletes a substring from the PieceTable starting at a specified position and with a specified length.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the substring will be deleted.
  • position (integer()): The starting position of the substring to be deleted. The position is zero-based.
  • length (integer()): The length of the substring to be deleted.

returns

Returns

  • {:ok, PieceTable.t()}
  • {:error, "invalid arguments"}

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> PieceTable.delete(table, 4, 3)
{:ok, %PieceTable{original: "Initial content", result: "Init content", applied: [%PieceTable.Change{change: :del, text: "ial", position: 4}]}}
Link to this function

delete!(table, position, length)

View Source
@spec delete!(t(), integer(), integer()) :: t()

Deletes a substring from the PieceTable starting at a specified position and with a specified length.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the substring will be deleted.
  • position (integer()): The starting position of the substring to be deleted. The position is zero-based.
  • length (integer()): The length of the substring to be deleted.

returns

Returns

  • PieceTable.t()

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> PieceTable.delete!(table, 4, 3)
%PieceTable{original: "Initial content", result: "Init content", applied: [%PieceTable.Change{change: :del, text: "ial", position: 4}]}
@spec get_text(t()) :: {:ok, String.t()} | {:error, String.t()}

Retrieves the entire text content from the PieceTable.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the text will be retrieved.

returns

Returns

  • {:ok, String.t()}
  • {:error, "not a PieceTable struct"}

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> table = PieceTable.delete!(table, 4, 3)
iex> PieceTable.get_text(table)
{:ok, "Init content"}
@spec get_text!(t()) :: String.t()

Retrieves the entire text content from the PieceTable.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the text will be retrieved.

returns

Returns

  • String.t()

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> table = PieceTable.delete!(table, 4, 3)
iex> PieceTable.get_text!(table)
"Init content"
Link to this function

insert(table, text, position)

View Source
@spec insert(t(), String.t(), integer()) :: {:ok, t()} | {:error, String.t()}

Inserts text into the PieceTable at a specified position.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable where the text will be inserted.
  • text (String.t()): The text to be inserted.
  • position (integer()): The position where the text should be inserted. The position is zero-based.

returns

Returns

  • {:ok, PieceTable.t()}
  • {:error, "invalid arguments"}

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> PieceTable.insert(table, ", before", 15)
{:ok, %PieceTable{original: "Initial content", result: "Initial content, before", applied: [%PieceTable.Change{change: :ins, text: ", before", position: 15}]}}
Link to this function

insert!(table, text, position)

View Source
@spec insert!(t(), String.t(), integer()) :: t()

Inserts text into the PieceTable at a specified position.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable where the text will be inserted.
  • text (String.t()): The text to be inserted.
  • position (integer()): The position where the text should be inserted. The position is zero-based.

returns

Returns

  • PieceTable.t()

examples

Examples

iex> table = PieceTable.new!("Initial content")
iex> PieceTable.insert!(table, ", before", 15)
%PieceTable{original: "Initial content", result: "Initial content, before", applied: [%PieceTable.Change{change: :ins, text: ", before", position: 15}]}
@spec new(String.t()) :: {:ok, t()} | {:error, String.t()}

Creates a new PieceTable struct. This is intended the only method to build it.

parameters

Parameters

  • text (String.t()): The initial content of the piece table.

returns

Returns

  • {:ok, %PieceTable{}}
  • {:error, "original text is not a string"}

examples

Examples

iex> PieceTable.new("test")
{:ok, %PieceTable{original: "test", result: "test", applied: []}}
@spec new!(String.t()) :: t()

Creates a new PieceTable struct. This is intended the only method to build it.

parameters

Parameters

  • text (String.t()): The initial content of the piece table.

returns-piecetable

Returns %PieceTable{}

examples

Examples

iex> PieceTable.new!("test")
%PieceTable{original: "test", result: "test", applied: []}
@spec redo(t()) :: {:ok, t()} | {:last, t()} | {:error, String.t()}

Redo the next change previously undone to the string.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the text will be retrieved.

returns

Returns

  • {:ok, PieceTable.t()}
  • {:last, PieceTable.t()}
  • {:error, "not a PieceTable struct"}

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> table = PieceTable.delete!(table, 4, 3)
iex> {:ok, table} = PieceTable.undo(table)
iex> PieceTable.redo(table)
{:ok, %PieceTable{original: "Initial content", result: "Init content", applied: [%PieceTable.Change{change: :del, text: "ial", position: 4}]}}
@spec redo!(t()) :: t()

Redo the next change previously undone to the string.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the text will be retrieved.

returns

Returns

  • PieceTable.t()

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> table = PieceTable.delete!(table, 4, 3)
iex> {:ok, table} = PieceTable.undo(table)
iex> PieceTable.redo!(table)
%PieceTable{original: "Initial content", result: "Init content", applied: [%PieceTable.Change{change: :del, text: "ial", position: 4}]}
@spec undo(t()) :: {:ok, t()} | {:first, t()} | {:error, String.t()}

Undo the latest change applied to the string.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the text will be retrieved.

returns

Returns

  • {:ok, PieceTable.t()}
  • {:first, PieceTable.t()}
  • {:error, "not a PieceTable struct"}

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> table = PieceTable.delete!(table, 4, 3)
iex> PieceTable.undo(table)
{:ok, %PieceTable{original: "Initial content", result: "Initial content", applied: [], to_apply: [%PieceTable.Change{change: :del, text: "ial", position: 4}]}}
@spec undo!(t()) :: t()

Undo the latest change applied to the string.

parameters

Parameters

  • table (PieceTable.t()): The PieceTable from which the text will be retrieved.

returns

Returns

  • PieceTable.t()

examples

Examples

iex> {:ok, table} = PieceTable.new("Initial content")
iex> table = PieceTable.delete!(table, 4, 3)
iex> PieceTable.undo!(table)
%PieceTable{original: "Initial content", result: "Initial content", applied: [], to_apply: [%PieceTable.Change{change: :del, text: "ial", position: 4}]}
Link to this function

update_piece_table(table, change)

View Source
@spec update_piece_table(t(), PieceTable.Change.t()) :: t()