Skip to content

Commit b39f0e3

Browse files
committed
Refactor mix format text diff
1 parent 5ba7817 commit b39f0e3

File tree

1 file changed

+20
-35
lines changed

1 file changed

+20
-35
lines changed

lib/mix/lib/mix/tasks/format.ex

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,6 @@ defmodule Mix.Tasks.Format do
201201
ins: [text: :green, space: :green_background]
202202
]
203203

204-
@default_opts [
205-
after: 2,
206-
before: 2,
207-
color: true,
208-
line: 1
209-
]
210-
211204
@doc """
212205
Returns which features this plugin should plug into.
213206
"""
@@ -684,6 +677,7 @@ defmodule Mix.Tasks.Format do
684677

685678
{file, unformatted, formatted} ->
686679
[
680+
IO.ANSI.bright(),
687681
IO.ANSI.red(),
688682
file,
689683
"\n",
@@ -695,14 +689,13 @@ defmodule Mix.Tasks.Format do
695689
end
696690

697691
@doc false
698-
@spec text_diff_format(String.t(), String.t(), keyword()) :: iolist()
699-
def text_diff_format(code, code, opts \\ default_opts())
692+
@spec text_diff_format(String.t(), String.t()) :: iolist()
693+
def text_diff_format(old, new, opts \\ [])
700694

701695
def text_diff_format(code, code, _opts), do: []
702696

703697
def text_diff_format(old, new, opts) do
704-
opts = Keyword.merge(@default_opts, opts)
705-
698+
opts = Keyword.validate!(opts, after: 2, before: 2, color: IO.ANSI.enabled?(), line: 1)
706699
crs? = String.contains?(old, "\r") || String.contains?(new, "\r")
707700

708701
old = String.split(old, "\n")
@@ -720,10 +713,6 @@ defmodule Mix.Tasks.Format do
720713
|> diff_to_iodata({line, line}, opts)
721714
end
722715

723-
@doc false
724-
@spec default_opts() :: keyword()
725-
def default_opts, do: @default_opts
726-
727716
defp diff_to_iodata(diff, line_nums, opts, iodata \\ [])
728717

729718
defp diff_to_iodata([], _line_nums, _opts, iodata), do: Enum.reverse(iodata)
@@ -873,29 +862,25 @@ defmodule Mix.Tasks.Format do
873862
end)
874863
end
875864

876-
defp colorize(str, kind, space, opts) do
877-
case Keyword.fetch!(opts, :color) && Keyword.has_key?(@colors, kind) do
878-
false ->
879-
str
865+
defp colorize(str, kind, space?, opts) do
866+
if Keyword.fetch!(opts, :color) && Keyword.has_key?(@colors, kind) do
867+
color = Keyword.fetch!(@colors, kind)
880868

881-
true ->
882-
color = Keyword.fetch!(@colors, kind)
869+
if space? do
870+
str
871+
|> String.split(~r/[\t\s]+/, include_captures: true)
872+
|> Enum.map(fn
873+
<<start::binary-size(1), _::binary>> = str when start in ["\t", "\s"] ->
874+
IO.ANSI.format([color[:space], str])
883875

884-
case space do
885-
false ->
876+
str ->
886877
IO.ANSI.format([color[:text], str])
887-
888-
true ->
889-
str
890-
|> String.split(~r/[\t\s]+/, include_captures: true)
891-
|> Enum.map(fn
892-
<<start::binary-size(1), _::binary>> = str when start in ["\t", "\s"] ->
893-
IO.ANSI.format([color[:space], str])
894-
895-
str ->
896-
IO.ANSI.format([color[:text], str])
897-
end)
898-
end
878+
end)
879+
else
880+
IO.ANSI.format([color[:text], str])
881+
end
882+
else
883+
str
899884
end
900885
end
901886

0 commit comments

Comments
 (0)