View Source PieceTable (piece_table v0.1.4)
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.
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.
Deletes a substring from the PieceTable starting at a specified position and with a specified length.
Link to this section Types
Link to this section Functions
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}]}
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"}
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"
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}]}
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}]}}
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: []}}
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: []}
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}]}}
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}]}
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}]}}
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}]}
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}]}}