# `Juggernaut.Schemata.Text`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/schemata/text.ex#L1)

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.

# `chunk`

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

# `edit`

```elixir
@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`

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

# `position`

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

# `span`

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

# `t`

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

# `blame`

```elixir
@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`

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

Indexes `source` by line.

# `offset`

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

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

# `owner_at`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

Renders the whole text with `edits` applied.

# `slice`

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

The bytes `from..to` (end exclusive).

# `spans`

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

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

# `to_binary`

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

The output text of `chunks`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
