@@ -201,13 +201,6 @@ defmodule Mix.Tasks.Format do
201
201
ins: [ text: :green , space: :green_background ]
202
202
]
203
203
204
- @ default_opts [
205
- after: 2 ,
206
- before: 2 ,
207
- color: true ,
208
- line: 1
209
- ]
210
-
211
204
@ doc """
212
205
Returns which features this plugin should plug into.
213
206
"""
@@ -684,6 +677,7 @@ defmodule Mix.Tasks.Format do
684
677
685
678
{ file , unformatted , formatted } ->
686
679
[
680
+ IO.ANSI . bright ( ) ,
687
681
IO.ANSI . red ( ) ,
688
682
file ,
689
683
"\n " ,
@@ -695,14 +689,13 @@ defmodule Mix.Tasks.Format do
695
689
end
696
690
697
691
@ 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 \\ [ ] )
700
694
701
695
def text_diff_format ( code , code , _opts ) , do: [ ]
702
696
703
697
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 )
706
699
crs? = String . contains? ( old , "\r " ) || String . contains? ( new , "\r " )
707
700
708
701
old = String . split ( old , "\n " )
@@ -720,10 +713,6 @@ defmodule Mix.Tasks.Format do
720
713
|> diff_to_iodata ( { line , line } , opts )
721
714
end
722
715
723
- @ doc false
724
- @ spec default_opts ( ) :: keyword ( )
725
- def default_opts , do: @ default_opts
726
-
727
716
defp diff_to_iodata ( diff , line_nums , opts , iodata \\ [ ] )
728
717
729
718
defp diff_to_iodata ( [ ] , _line_nums , _opts , iodata ) , do: Enum . reverse ( iodata )
@@ -873,29 +862,25 @@ defmodule Mix.Tasks.Format do
873
862
end )
874
863
end
875
864
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 )
880
868
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 ] )
883
875
884
- case space do
885
- false ->
876
+ str ->
886
877
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
899
884
end
900
885
end
901
886
0 commit comments