Juggernaut.Schemata.Text (Juggernaut v0.4.0)

Copy Markdown View Source

Source text addressed by byte offset, and the edit tree a metamutant is rendered from (design notes §10.3, §10.4).

Positions in mutants are {line, column}, 1-based, columns counted in codepoints (as Juggernaut.Diff counts them); offset/2 turns one into a byte offset.

An edit covers the bytes from..to (end exclusive) of the source:

  • {:wrap, from, to, prefix, suffix, owner}: the original bytes, with the edits inside them applied, between two lists of chunks;
  • {:replace, from, to, chunks}: the chunks instead of the bytes; edits inside are dropped;
  • {:region, from, to, owner}: the bytes with the edits inside applied, owned by owner.

Edits must nest or be disjoint. Rendering gives chunks, {text, owner}, where the owner says which switches a piece of output belongs to: nil (code as written), {:group, switches} (code a site or a lifted function wraps) or {:branch, switches} (one mutant's code). spans/1 places the chunks in the output by line and column, and blame/2 names the switches responsible for a compile diagnostic there.

Summary

Functions

The switches a diagnostic at position ({line, column} or a line) blames: the owner of the chunk there, or for a bare line every branch on it (else every group). Empty when the position is in code as written.

Indexes source by line.

The byte offset of {line, column}, or :error outside the text.

The owner of the chunk at {line, column}, or nil (code as written, or a bare line).

The bytes of range (a mutant location), or :error.

Renders from..to of text with edits (which must lie inside it) applied; gaps belong to owner.

Renders the whole text with edits applied.

The bytes from..to (end exclusive).

Where each owned chunk lands in the output: start and end {line, column}.

The output text of chunks.

Types

chunk()

@type chunk() :: {iodata(), owner()}

edit()

@type edit() ::
  {:wrap, non_neg_integer(), non_neg_integer(), [chunk()], [chunk()], owner()}
  | {:replace, non_neg_integer(), non_neg_integer(), [chunk()]}
  | {:region, non_neg_integer(), non_neg_integer(), owner()}

owner()

@type owner() :: nil | {:group | :branch, [pos_integer()]}

position()

@type position() :: {pos_integer(), pos_integer()}

span()

@type span() :: %{start: position(), stop: position(), owner: owner()}

t()

@type t() :: %Juggernaut.Schemata.Text{
  lines: tuple(),
  source: binary(),
  starts: tuple()
}

Functions

blame(spans, line)

@spec blame([span()], position() | pos_integer()) :: [pos_integer()]

The switches a diagnostic at position ({line, column} or a line) blames: the owner of the chunk there, or for a bare line every branch on it (else every group). Empty when the position is in code as written.

new(source)

@spec new(binary()) :: t()

Indexes source by line.

offset(text, arg2)

@spec offset(t(), position()) :: {:ok, non_neg_integer()} | :error

The byte offset of {line, column}, or :error outside the text.

owner_at(spans, position)

@spec owner_at([span()], position() | pos_integer()) :: owner()

The owner of the chunk at {line, column}, or nil (code as written, or a bare line).

range_offsets(text, map)

@spec range_offsets(t(), %{start: position(), end: position()}) ::
  {:ok, {non_neg_integer(), non_neg_integer()}} | :error

The bytes of range (a mutant location), or :error.

render(text, edits, from, to, owner \\ nil)

@spec render(t(), [edit()], non_neg_integer(), non_neg_integer(), owner()) :: [
  chunk()
]

Renders from..to of text with edits (which must lie inside it) applied; gaps belong to owner.

render_all(text, edits)

@spec render_all(t(), [edit()]) :: [chunk()]

Renders the whole text with edits applied.

slice(text, from, to)

@spec slice(t(), non_neg_integer(), non_neg_integer()) :: binary()

The bytes from..to (end exclusive).

spans(chunks)

@spec spans([chunk()]) :: [span()]

Where each owned chunk lands in the output: start and end {line, column}.

to_binary(chunks)

@spec to_binary([chunk()]) :: binary()

The output text of chunks.