Skip to content

Commit 168cc37

Browse files
Print formatted file names in mix format --verbose, add --verbose flag
1 parent 1f211d1 commit 168cc37

2 files changed

Lines changed: 71 additions & 14 deletions

File tree

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ defmodule Mix.Tasks.Format do
7777
7878
* `--dry-run` - does not save files after formatting.
7979
80+
* `--verbose` - prints the names of files that were formatted.
81+
8082
* `--dot-formatter` - path to the file with formatter configuration.
8183
Defaults to `.formatter.exs` if one is available. See the
8284
"Formatting options" section above for more information.
@@ -199,6 +201,7 @@ defmodule Mix.Tasks.Format do
199201
no_exit: :boolean,
200202
dot_formatter: :string,
201203
dry_run: :boolean,
204+
verbose: :boolean,
202205
stdin_filename: :string,
203206
force: :boolean,
204207
migrate: :boolean
@@ -264,7 +267,7 @@ defmodule Mix.Tasks.Format do
264267
maybe_cache_timestamps(all_args, files, fn files ->
265268
files
266269
|> Task.async_stream(&format_file(&1, opts), ordered: false, timeout: :infinity)
267-
|> Enum.reduce({[], []}, &collect_status/2)
270+
|> Enum.reduce({[], [], []}, &collect_status/2)
268271
|> check!(opts)
269272
end)
270273
end
@@ -773,39 +776,64 @@ defmodule Mix.Tasks.Format do
773776

774777
defp write_or_print(file, input, output) do
775778
cond do
776-
file == :stdin -> IO.write(output)
777-
input == output -> :ok
778-
true -> File.write!(file, output)
779-
end
779+
file == :stdin ->
780+
IO.write(output)
781+
:ok
780782

781-
:ok
783+
input == output ->
784+
:ok
785+
786+
true ->
787+
File.write!(file, output)
788+
{:formatted, file}
789+
end
782790
end
783791

784792
defp collect_status({:ok, :ok}, acc), do: acc
785793

786-
defp collect_status({:ok, {:exit, _, _, _} = exit}, {exits, not_formatted}) do
787-
{[exit | exits], not_formatted}
794+
defp collect_status({:ok, {:exit, _, _, _} = exit}, {exits, not_formatted, formatted}) do
795+
{[exit | exits], not_formatted, formatted}
788796
end
789797

790-
defp collect_status({:ok, {:not_formatted, file}}, {exits, not_formatted}) do
791-
{exits, [file | not_formatted]}
798+
defp collect_status({:ok, {:not_formatted, file}}, {exits, not_formatted, formatted}) do
799+
{exits, [file | not_formatted], formatted}
792800
end
793801

794-
defp check!({[], []}, _task_opts) do
802+
defp collect_status({:ok, {:formatted, file}}, {exits, not_formatted, formatted}) do
803+
{exits, not_formatted, [file | formatted]}
804+
end
805+
806+
defp check!({[], [], []}, _task_opts) do
807+
:ok
808+
end
809+
810+
defp check!({[], [], formatted}, task_opts) do
811+
if task_opts[:verbose] do
812+
formatted
813+
|> Enum.sort()
814+
|> Enum.each(&Mix.shell().info([:green, "* formatting ", :reset, Path.relative_to_cwd(&1)]))
815+
end
816+
795817
:ok
796818
end
797819

798-
defp check!({[{:exit, :stdin, exception, stacktrace} | _], _not_formatted}, _task_opts) do
820+
defp check!(
821+
{[{:exit, :stdin, exception, stacktrace} | _], _not_formatted, _formatted},
822+
_task_opts
823+
) do
799824
Mix.shell().error("mix format failed for stdin")
800825
reraise exception, stacktrace
801826
end
802827

803-
defp check!({[{:exit, file, exception, stacktrace} | _], _not_formatted}, _task_opts) do
828+
defp check!(
829+
{[{:exit, file, exception, stacktrace} | _], _not_formatted, _formatted},
830+
_task_opts
831+
) do
804832
Mix.shell().error("mix format failed for file: #{Path.relative_to_cwd(file)}")
805833
reraise exception, stacktrace
806834
end
807835

808-
defp check!({_exits, [_ | _] = not_formatted}, task_opts) do
836+
defp check!({_exits, [_ | _] = not_formatted, _formatted}, task_opts) do
809837
no_exit? = Keyword.get(task_opts, :no_exit, false)
810838

811839
message = """

lib/mix/test/mix/tasks/format_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ defmodule Mix.Tasks.FormatTest do
8585
end)
8686
end
8787

88+
test "prints formatted file names with --verbose", context do
89+
in_tmp(context.test, fn ->
90+
File.write!("a.ex", """
91+
foo bar
92+
""")
93+
94+
File.write!("b.ex", """
95+
foo(bar)
96+
""")
97+
98+
Mix.Tasks.Format.run(["a.ex", "b.ex", "--verbose"])
99+
100+
assert_received {:mix_shell, :info, ["* formatting a.ex"]}
101+
refute_received {:mix_shell, :info, ["* formatting b.ex"]}
102+
end)
103+
end
104+
105+
test "does not print formatted file names by default", context do
106+
in_tmp(context.test, fn ->
107+
File.write!("a.ex", """
108+
foo bar
109+
""")
110+
111+
Mix.Tasks.Format.run(["a.ex"])
112+
113+
refute_received {:mix_shell, :info, ["* formatting a.ex"]}
114+
end)
115+
end
116+
88117
test "does not try to format a directory that matches a given pattern", context do
89118
in_tmp(context.test, fn ->
90119
File.mkdir_p!("a.ex")

0 commit comments

Comments
 (0)