Crowdhailer
Running the new Elixir formatter
I’ve been hearing much about the new formatter and it’s something I have been keen to try.
I find examples buy far the most illuminating way to try some new tech. To that end I though I would blindly run the new formatter on my largest open source Elixir code base, Ace.
I did no preparation before hand and in one swoop was able to format the whole project using
$ mix format mix.exs "lib/**/*.{ex,exs}" "test/**/*.{ex,exs}"
Results
- My project is now larger. 1369 lines added but only 752 removed.
A lot of these are empty lines. Every case clause is separated by a new line and so is every function head.
It looks like my personal convention of separating different functions but not different function heads of one function is now over - Brackets are now applied automatically so no more dealing with those warnings. This is a big win.
- Numbers lose underscores
65_535→65535
- Several cases where things are now indented to line up with something above rather than just two spaces, this is probably what I am least happy about but I can get over it for the consistency
- Comments now always end up on a separate line.
In conclusion I think that is a great additions and I hope that the numbers will eventually be formatted with the underscores
Most Liked
NobbZ
Please don’t… I was born 1981, not 1_981…
josevalim
You can have all of them on the same line if you want to:
case foo do
{:ok, bar} -> {:ok, bar}
{:error, baz} -> {:error, baz}
end
But if you have newlines after ->, then the formatter will respect that and proceed to add an empty line between each clause.
Remember the goal of the formatter is to make the code consistent. The opposite of the current behaviour would be to remove lines between clauses, which would affect different areas of @Crowdhailer’s repository. I think users would very likely be more upset if we remove the empty lines they added to space out code.
Between the two below, one is definitely more readable than the other for me (although I agree it is subjective):
defp integer_to_algebra(text) do
case text do
[?0, ?x | rest] ->
"0x" <> String.upcase(List.to_string(rest))
[?0, base | _rest] = digits when base in [?b, ?o] ->
List.to_string(digits)
[?? | _rest] = char ->
List.to_string(char)
decimal ->
List.to_string(insert_underscores(decimal))
end
end
and
defp integer_to_algebra(text) do
case text do
[?0, ?x | rest] ->
"0x" <> String.upcase(List.to_string(rest))
[?0, base | _rest] = digits when base in [?b, ?o] ->
List.to_string(digits)
[?? | _rest] = char ->
List.to_string(char)
decimal ->
List.to_string(insert_underscores(decimal))
end
end
We will open up a discussion on that.
chulkilee
# install erlang with homebre
brew install erlang
# install asdf to manage Elixir versions
if [ -d "$HOME/asdf" ]; then
git clone https://github.com/asdf-vm/asdf.git "$HOME/.asdf" --branch v0.4.0
fi
# don't forget to add following part to your shell config (e.g. .bashrc)
. "$HOME/.asdf/asdf.sh"
# install elixir plugin if missing
plugins=$(asdf plugin-list)
if grep -q -v elixir <<< "$plugins"; then
asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
fi
# install with asdf as in .tool-versions
asdf install
# install master version
asdf install elixir ref:master
# run mix format with elixir master
ASDF_ELIXIR_VERSION=ref-master mix format
I have mix_format alias in my ~/.alias
alias mix_format="ASDF_ELIXIR_VERSION=ref-master mix format"
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









