# `Juggernaut.Diff`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/diff.ex#L1)

Where a site is in the original text, and the mutant as a patch of that text
(design notes §6, `docs/dependencies/sourceror.md`).

The diff is never rendered output compared with the raw file: the original lines are
patched over the site's range with the rendered replacement, so everything outside the
site keeps its spelling.

Three of Sourceror's range defects are corrected here:

  * a node written in parentheses is ranged over them; its replacement is wrapped in
    parentheses too, so the patch keeps them;
  * a string or sigil with an escaped delimiter is ranged one column short per escape;
    its end is found by scanning the original text;
  * an operator whose last operand is a multi-line string, charlist or sigil is ranged
    past that operand; it ends where the operand ends.

A replacement starting with a sign that would follow a sign in the text (the literal
under a unary minus) is written in parentheses (`separate_sign/3`).

The fourth defect (an anonymous call such as `(fn x -> x end).(1)` ranged from inside the
callee's parentheses) is not corrected: such sites keep Sourceror's range.

# `position`

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

# `range`

```elixir
@type range() :: %{start: position(), end: position()}
```

# `patch`

```elixir
@spec patch([String.t()], range(), String.t()) :: [String.t()]
```

`lines` with `range` replaced by `text` (later lines re-indented as the first).

# `range`

```elixir
@spec range(Macro.t(), [String.t()]) :: {:ok, range()} | :error
```

The corrected range of `node` in `lines` (the file split on newlines), or `:error`.

# `replacement_text`

```elixir
@spec replacement_text(Macro.t(), String.t(), String.t()) :: String.t()
```

The replacement text for `node`'s range: `rendered`, wrapped in parentheses when the
range includes parentheses written around the node.

# `separate_sign`

```elixir
@spec separate_sign(String.t(), [String.t()], range()) :: String.t()
```

`text` in parentheses when it starts with a sign and the character just before
`range` is one: the literal under `-0.05` is `0.05`, and its replacement `-0.95`
written there would read `--0.95`, which does not parse. `-(-0.95)` is the mutant.

# `slice`

```elixir
@spec slice([String.t()], range()) :: String.t()
```

The text of `lines` over `range`.

# `unified`

```elixir
@spec unified([String.t()], range(), String.t()) :: String.t()
```

A unified diff of one hunk: the lines `range` covers, before and after `text` replaces
it. `@@ -L +L @@` when one line is replaced by one.

---

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