Quillon.Selection.Point (Quillon v0.4.0)

Copy Markdown View Source

A single place in a document: which block, and where inside it.

A point pairs a Quillon.Path path to a block with a character offset counted inside that block, the same offset the transform layer takes. The two together are what nothing in the library expressed before - paths address nodes and never characters, and offsets address characters but only within one block.

iex> Quillon.Selection.Point.new([0, 2], 5)
%Quillon.Selection.Point{path: [0, 2], offset: 5}

Treat a point as opaque: build it with new/2 and read it with path/1 and offset/1 rather than matching on the struct. Anchors are expected to grow an id-based form once documents carry stable ids, and code that goes through the accessors will not need changing.

Summary

Types

A path to a block, as Quillon.Path defines it

t()

A place in a document

Functions

Order two points as they appear in the document.

Build a point.

The character offset inside that block.

The path to the block this point is in.

Types

path()

@type path() :: [non_neg_integer()]

A path to a block, as Quillon.Path defines it

t()

@type t() :: %Quillon.Selection.Point{offset: non_neg_integer(), path: path()}

A place in a document

Functions

compare(a, b)

@spec compare(t(), t()) :: :lt | :eq | :gt

Order two points as they appear in the document.

Paths compare element by element. A shorter path that is a prefix of a longer one comes first, since an ancestor is reached before its descendant. Points in the same block fall back to comparing offsets.

Examples

iex> a = Quillon.Selection.Point.new([0], 0)
iex> b = Quillon.Selection.Point.new([1], 0)
iex> Quillon.Selection.Point.compare(a, b)
:lt

iex> a = Quillon.Selection.Point.new([0], 2)
iex> b = Quillon.Selection.Point.new([0], 7)
iex> Quillon.Selection.Point.compare(a, b)
:lt

iex> a = Quillon.Selection.Point.new([0], 3)
iex> b = Quillon.Selection.Point.new([0], 3)
iex> Quillon.Selection.Point.compare(a, b)
:eq

iex> a = Quillon.Selection.Point.new([0], 0)
iex> b = Quillon.Selection.Point.new([0, 1], 0)
iex> Quillon.Selection.Point.compare(a, b)
:lt

new(path, offset)

@spec new(path(), non_neg_integer()) :: t()

Build a point.

Examples

iex> Quillon.Selection.Point.new([0], 0)
%Quillon.Selection.Point{path: [0], offset: 0}

iex> Quillon.Selection.Point.new([], 0)
%Quillon.Selection.Point{path: [], offset: 0}

offset(point)

@spec offset(t()) :: non_neg_integer()

The character offset inside that block.

Examples

iex> Quillon.Selection.Point.new([0, 2], 5) |> Quillon.Selection.Point.offset()
5

path(point)

@spec path(t()) :: path()

The path to the block this point is in.

Examples

iex> Quillon.Selection.Point.new([0, 2], 5) |> Quillon.Selection.Point.path()
[0, 2]