Mix.install([
{:differ, "~> 0.1.1"}
])
defmodule UnFormat do
def run(input, output) do
input
|> Differ.diff(output)
|> Enum.flat_map(fn
{op, str} ->
cond do
# Undo whitespace changes
op == :del && str =~ ~r"^\s*$" ->
[{:eq, str}]
op == :ins && str =~ ~r"^\s*$" ->
[]
# Undo opening parens changes
op == :del && str =~ ~r"^[\s\(]*$" ->
[{:eq, str}]
op == :ins && str =~ ~r"^[\s\(]*$" ->
[]
# Undo closing parens changes
op == :del && str =~ ~r"^[\s\)]*$" ->
[{:eq, str}]
op == :ins && str =~ ~r"^[\s\)]*$" ->
[]
true ->
[{op, str}]
end
end)
|> then(&Differ.patch!(input, &1))
end
end
{:module, UnFormat, <<70, 79, 82, 49, 0, 0, 12, ...>>, {:run, 2}}
input =
"""
config :fake,
:buz, (([:blat]))
config :fake2, 1 + 1
"""
output =
"""
config :fake, :buz, [:blat]
config :fake2, (1 + 1)
config :fake, foo: "baz"
"""
IO.puts(UnFormat.run(input, output))
config :fake,
:buz, (([:blat]))
config :fake2, 1 + 1
config :fake, foo: "baz"
:ok
input =
"""
config :fake, :buz, [1,2,3]
"""
output =
"""
config :fake, :buz, [
1,
2,
3]
config :fake, foo: "baz"
"""
IO.puts(UnFormat.run(input, output))
config :fake, :buz, [1,2,3]
config :fake, foo: "baz"
:ok