Skip to content

Commit ad489d7

Browse files
josevalimJosé Valim
authored andcommitted
Improve the docs for mix format
Closes #7327.
1 parent f8afbf3 commit ad489d7

File tree

1 file changed

+40
-46
lines changed

1 file changed

+40
-46
lines changed

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

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,82 +13,76 @@ defmodule Mix.Tasks.Format do
1313
1414
## Formatting options
1515
16-
Formatting is done with the `Code.format_string!/2` function.
17-
For complete list of formatting options please refer to its
18-
description.
19-
A `.formatter.exs` file can also be defined for customizing input
20-
files and the formatter itself.
16+
The formatter will read a `.formatter.exs` in the current directory for
17+
formatter configuration. Evaluating this file should return a keyword list
18+
with any of the options supported by `Code.format_string!/2`.
19+
20+
The `.formatter.exs` also supports other options:
21+
22+
* `:inputs` (a list of paths and patterns) - specifies the default inputs
23+
to be used by this task. For example, `["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]`.
24+
25+
* `:import_deps` (a list of dependencies as atoms) - specifies a list
26+
of dependencies whose formatter configuration will be imported.
27+
When specified, the formatter should run in the same directory as
28+
the `mix.exs` file that defines those depednencies. See the "Importing
29+
dependencies configuration" section below for more information.
30+
31+
* `:export` (a keyword list) - specifies formatter configuration to be exported.
32+
See the "Importing dependencies configuration" section below.
2133
2234
## Task-specific options
2335
2436
* `--check-formatted` - check that the file is already formatted.
2537
This is useful in pre-commit hooks and CI scripts if you want to
26-
reject contributions with unformatted code. However, keep in mind,
27-
that the formatting output may differ between Elixir versions as
38+
reject contributions with unformatted code. However keep in mind
39+
that the formatted output may differ between Elixir versions as
2840
improvements and fixes are applied to the formatter.
2941
30-
* `--check-equivalent` - check if the file after formatting has the
31-
same AST. If the ASTs are not equivalent, it is a bug in the code
32-
formatter. This option is recommended if you are automatically
33-
formatting files.
42+
* `--check-equivalent` - check if the files after formatting have the
43+
same AST as before formatting. If the ASTs are not equivalent,
44+
it is a bug in the code formatter. This option is recommended if you
45+
are automatically formatting files.
3446
3547
* `--dry-run` - do not save files after formatting.
3648
37-
* `--dot-formatter` - the file with formatter configuration.
38-
Defaults to `.formatter.exs` if one is available, see next section.
49+
* `--dot-formatter` - path to the file with formatter configuration.
50+
Defaults to `.formatter.exs` if one is available. See the "`.formatter.exs`"
51+
section for more information.
3952
4053
If any of the `--check-*` flags are given and a check fails, the formatted
41-
contents won't be written to disk nor printed to stdout.
42-
43-
## `.formatter.exs`
44-
45-
The formatter will read a `.formatter.exs` in the current directory for
46-
formatter configuration. It should return a keyword list with any of the
47-
options supported by `Code.format_string!/2`.
48-
49-
The `.formatter.exs` also supports other options:
50-
51-
* `:inputs` (a list of paths and patterns) - specifies the default inputs
52-
to be used by this task. For example, `["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]`.
53-
54-
* `:import_deps` (a list of dependencies as atoms) - specifies a list
55-
of dependencies whose formatter configuration will be imported.
56-
See the "Importing dependencies configuration" section below for more
57-
information.
58-
59-
* `:export` (a keyword list) - specifies formatter configuration to be exported. See the
60-
"Importing dependencies configuration" section below.
54+
contents won't be written to disk nor printed to standard output.
6155
6256
## When to format code
6357
64-
We recommend developers to format code directly in their editors. Either
65-
automatically on save or via an explicit command/key binding. If such option
66-
is not yet available in your editor of choice, adding the required integration
67-
is relatively simple as it is a matter of invoking
58+
We recommend developers to format code directly in their editors, either
59+
automatically when saving a file or via an explicit command or key binding. If
60+
such option is not yet available in your editor of choice, adding the required
61+
integration is usually a matter of invoking:
6862
6963
cd $project && mix format $file
7064
7165
where `$file` refers to the current file and `$project` is the root of your
7266
project.
7367
7468
It is also possible to format code across the whole project by passing a list
75-
of patterns and files to `mix format`, as showed at the top of this task
69+
of patterns and files to `mix format`, as shown at the top of this task
7670
documentation. This list can also be set in the `.formatter.exs` under the
7771
`:inputs` key.
7872
7973
## Importing dependencies configuration
8074
8175
This task supports importing formatter configuration from dependencies.
8276
83-
A dependency that wants to export formatter configuration needs to have a `.formatter.exs` file
84-
at the root of the project. In this file, the dependency can export a `:export` option with
85-
configuration to export. For now, only one option is supported under `:export`:
86-
`:locals_without_parens` (whose value has the same shape as the value of the
87-
`:locals_without_parens` in `Code.format_string!/2`).
77+
A dependency that wants to export formatter configuration needs to have a
78+
`.formatter.exs` file at the root of the project. In this file, the dependency
79+
can export a `:export` option with configuration to export. For now, only one
80+
option is supported under `:export`: `:locals_without_parens` (whose value has
81+
the same shape as the value of the `:locals_without_parens` in `Code.format_string!/2`).
8882
89-
The functions listed under `:locals_without_parens` in the `:export` option of a dependency
90-
can be imported in a project by listing that dependency in the `:import_deps`
91-
option of the formatter configuration file of the project.
83+
The functions listed under `:locals_without_parens` in the `:export` option of
84+
a dependency can be imported in a project by listing that dependency in the
85+
`:import_deps` option of the formatter configuration file of the project.
9286
9387
For example, consider I have a project `my_app` that depends on `my_dep`.
9488
`my_dep` wants to export some configuration, so `my_dep/.formatter.exs`

0 commit comments

Comments
 (0)