mix format
mix format is the formatter that ships with Elixir. It reads the files named in .formatter.exs, rewrites the whitespace to one style and leaves the meaning alone. A team does not argue about indentation; a pipeline runs the check, and a file that would be rewritten fails the build.
The formatter file
The generated .formatter.exs lists the project files with a glob. The list is the only place that decides what the formatter touches, and a folder of generated code stays out of it:
mix new fmt_demo
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/fmt_demo.ex
* creating test
* creating test/test_helper.exs
* creating test/fmt_demo_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd fmt_demo
mix test
Run "mix help" for more commands.
fmt_demo/lib/fmt_demo.ex
defmodule FmtDemo do @moduledoc false @doc """ Hello world. ## Examples iex> FmtDemo.hello() :world """ def hello do :world end end
The formatter uses a line length of 98 by default and no other rule a reader has to learn. A project that wants a shorter line adds line_length: 80 to the keyword list of the formatter file; nothing else in the file changes.
The check a pipeline runs
--check-formatted does not rewrite anything. It exits 1 and prints the files that would change, which is the form a continuous integration job runs:
cd fmt_demo
mix format --check-formatted
The command printed nothing and exited 0: every file is already formatted. The next section makes one file bad on purpose and shows the report.
The diff of a bad file
The file is indented by four spaces with one command of sed, and the check reports the line it would rewrite. The report names the file, then prints a numbered diff with -| for the old line and +| for the new one:
sed -i 's/^ def hello/ def hello/' lib/fmt_demo.ex
mix format --check-formatted
|
10 10 | :world
11 11 | """
12 -| def hello do
12 +| def hello do
13 13 | :world
14 14 | end
|
The path line above the diff is left out of the block because it carries the absolute path of the machine; the prose says which file it names. The diff is the whole message: the old line is four spaces in, the new one is two.
mix format
mix format without the check rewrites the files in place and prints nothing. The check that follows passes, and the code is the same apart from the whitespace:
mix format
mix format --check-formatted
The second command printed nothing and exited 0. The formatter is the last step before a commit, and the Credo lesson adds the checks the formatter cannot see: a module without a doc, a long function, a name that says nothing.
The commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
mix formatchange in a file?Answer
The whitespace, to one style, leaving the meaning alone
The formatter rewrites layout and leaves the code semantically the same.
-
What does
mix format --check-formatteddo?Answer
Exits 1 when a file would be rewritten, and prints nothing when all are formatted
The check mode is read-only and reports the files that would change.
-
Which file lists the files the formatter reads?
Answer
.formatter.exs
The inputs key of .formatter.exs is the list of globs.
-
What is the default line length of the formatter?
Answer
98
Elixir formats to 98 columns unless the formatter file sets line_length.
-
What does the
-|line of the diff mean?Answer
The line the formatter would replace
The old line is marked -| and the new line +|.
-
What does a pipeline do with
mix format --check-formatted?Answer
It fails the build when a file is not formatted
The non-zero status of the check is the signal a CI job uses.
-
What does
mix formatprint when it rewrites a file?Answer
Nothing on success
The formatter is quiet; only the check mode prints a report.
-
How do you format one file only?
Answer
mix format lib/app.ex
mix format takes the paths to format after the command.
-
Which change does the formatter make to
def hello doindented by four spaces?Answer
It moves it to two spaces
Elixir indents with two spaces at each level.
-
What does the formatter leave alone?
Answer
The meaning of the code
It changes layout, not behaviour; the line length can be configured, but the code stays the same.
Автор статьи: Arthur Isaev