📎 Webclip
Everything you need to know about Elixir's new formatter
The text introduces Elixir’s new code formatter, explaining that it has landed in the Elixir repository and can already be explored. It walks through installing the development version of Elixir that includes the formatter, then shows how to run mix format on individual files or file patterns, and how to centralize those patterns in a .formatter.exs file. It also covers optional configuration for macros that should not use parentheses, how to integrate formatting checks into a CI pipeline, and a simple way to trigger mix format automatically on save when using vim.
Reading notes#
- The new formatter mentioned in a previous ElixirConf talk has been merged into the Elixir repository and is available for use.
- To try it, you clone the Elixir repo from GitHub with
git clone https://github.com/elixir-lang/elixir.git. - After cloning, you should first run
make clean testto compile and run the test suite on your machine. - Once tests pass, you install Elixir with
make install. - Running
elixir -vshould now show a development version likeElixir 1.6.0-dev (9b5af3303), with the SHA varying. - With this dev version installed, you can start using the formatter.
- To format a single file in a standard mix project, you can run
mix format mix.exs, where the extra argument is the target file. - The
mix formattask formats only the files and patterns explicitly passed to it. - To format many files at once, you can pass glob patterns, for example
mix format mix.exs "lib/**/*.{ex,exs}" "test/**/*.{ex,exs}". mix formataccepts any number of specific files or glob patterns as arguments.- Repeatedly passing long lists of patterns on the command line can be inconvenient.
- A
.formatter.exsfile lets you configure which files should be formatted by default. - A basic
.formatter.exsexample setsinputsto includelib/**/*.{ex,exs},test/**/*.{ex,exs}, andmix.exs. - With that configuration, running
mix formatwith no extra arguments will format all.exand.exsfiles under those paths. - The formatter also supports configuration for macros that should not be forced to use parentheses.
- In
.formatter.exs, thelocals_without_parensoption lists macros by name and arity to exempt them from the parentheses rule. - An example
locals_without_parensconfiguration listsmy_macro: 2andmy_other_macro: 3as macros that should not require parentheses. - The key in that keyword list is the macro name, and the value is its arity.
- For CI integration, you can add a formatting check command to the pipeline.
- The suggested command is
mix format --check-formatted --dry-runso CI fails if files are not formatted. - Because the formatter may change while still in development, the text suggests being cautious about wiring this up immediately.
- For vim users, you can trigger the formatter automatically when saving Elixir files.
- Adding
autocmd BufWritePost *.exs silent :!mix format %andautocmd BufWritePost *.ex silent :!mix format %to.vimrcformats.exand.exsfiles on write. - Enabling
set autoreadin vim helps reload files after external formatting. - The author expects more advanced vim integrations to appear later, possibly using tools like neomake, but the provided commands already allow basic on-save formatting.
