View Source Rewrite.TextDiff (rewrite v0.1.0)

Formats the diff between two strings.

examples

Examples

iex> code = """
...> defmodule Foo do
...>   @moduledoc   false
...>
...>   def foo, do:  :foo
...>
...>   def three_times(x) do
...>     {x,
...>      x,x}
...>   end
...>
...>   def bar(x) do
...>     {:bar, x}
...>   end
...> end\
...> """
iex> formatted = code |> Code.format_string!() |> IO.iodata_to_binary()
iex> code
...> |> Rewrite.TextDiff.format(formatted, color: false)
...> |> IO.iodata_to_binary()
"""
 1  1   |defmodule Foo do
 2    - |  @moduledoc   false
    2 + |  @moduledoc false
 3  3   |
 4    - |  def foo, do:  :foo
    4 + |  def foo, do: :foo
 5  5   |
 6  6   |  def three_times(x) do
 7    - |    {x,
 8    - |     x,x}
    7 + |    {x, x, x}
 9  8   |  end
10  9   |
     ...|
"""

Link to this section Summary

Link to this section Functions

@spec default_opts() :: keyword()
Link to this function

format(code, code, opts \\ default_opts())

View Source
@spec format(String.t(), String.t(), keyword()) :: iodata()

Formats the diff between two strings.

The returned iodata shows the lines with changes and 2 lines before and after the changed positions. The string contains also a gutter with line number and a - or + for removed and added lines. Multiple lines without changes are marke with ... in the gutter.

options

Options

  • after - the count of lines printed after each change. Defaults to 2.
  • before - the count of lines printed befor each change. Defaults to 2.
  • color - enables color in the output. Defautls to truel.
  • line - the line number of the first line. Defaults to 1.

examples

Examples

iex> code = """
...> defmodule Bar do
...>   @moduledoc false
...>
...>   bar(x, y) do
...>     z = x + y
...>     {x,y  , z}
...>   end
...>
...>   bar(x, y, z) do
...>     {x, y, z}
...>   end
...> end\
...> """
iex> formatted = code |> Code.format_string!() |> IO.iodata_to_binary()
iex> code
...> |> Rewrite.TextDiff.format(formatted, color: false)
...> |> IO.iodata_to_binary()
"""
     ...|
 4  4   |  bar(x, y) do
 5  5   |    z = x + y
 6    - |    {x,y  , z}
    6 + |    {x, y, z}
 7  7   |  end
 8  8   |
     ...|
"""
iex> code
...> |> Rewrite.TextDiff.format(formatted, color: false, after: 1, before: 1)
...> |> IO.iodata_to_binary()
"""
     ...|
 5  5   |    z = x + y
 6    - |    {x,y  , z}
    6 + |    {x, y, z}
 7  7   |  end
     ...|
"""