Skip to content

Commit 8cda8e1

Browse files
Print formatted file names in mix format --verbose (#15119)
1 parent 978e404 commit 8cda8e1

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

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

Lines changed: 17 additions & 5 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
@@ -764,18 +767,27 @@ defmodule Mix.Tasks.Format do
764767
:ok
765768

766769
true ->
767-
write_or_print(file, input, output)
770+
write_or_print(file, input, output, task_opts)
768771
end
769772
rescue
770773
exception ->
771774
{:exit, file, exception, __STACKTRACE__}
772775
end
773776

774-
defp write_or_print(file, input, output) do
777+
defp write_or_print(file, input, output, task_opts) do
775778
cond do
776-
file == :stdin -> IO.write(output)
777-
input == output -> :ok
778-
true -> File.write!(file, output)
779+
file == :stdin ->
780+
IO.write(output)
781+
782+
input == output ->
783+
:ok
784+
785+
true ->
786+
File.write!(file, output)
787+
788+
if task_opts[:verbose] do
789+
Mix.shell().info([:green, "* formatting ", :reset, Path.relative_to_cwd(file)])
790+
end
779791
end
780792

781793
:ok

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)