Subscribe to access all episodes. View plans →

#21: Elixir Formatter Preview

Published October 31, 2017

Elixir 1.6


Since its announcement at ElixirConf this year, the formatter coming to Elixir 1.6 is getting a lot of love.

In this episode we’ll take a quick dive into the formatter to get a feel for how it will work.

Since it’s planned to be released with Elixir 1.6 we’ll need to install it. I’ll use homebrew to install.

$ brew install elixir --HEAD

Once it’s finished let’s check that the version is correct:

$ elixir -v

Great, we see 1.6.

Now let’s view the formatter’s current documentation by running:

$ mix help format

We can see that to run the formatter, we’ll use mix format and pass it either a file or file pattern.

There’s also info on different options the formatter accepts. In addition it tells us there’s a .formatter.exs file we can use to customize the formatter and recommends when to run the formatter.

Now that we know the basics of the formatter - let’s see it in action.

Here I’ve got a simple module with a few minor formatting issues, like an extra space between the module and the first function and no spaces between function parameters

Let’s go back to the command line and run:

$ mix format lib/teacher.ex --check-formatted

Which will tell us if the file has been formatted.

Great it tells us that the file hasn’t yet been formatted.

Let’s run the formatter on it:

$ mix format lib/teacher.ex

And if we go back to our module we can see it’s been formatted - the empty line between the module definition and the first function is gone. And there are spaces between the function parameters.

Now let’s take the next step and create a .formatter.exs file that we can use to customize the formatter.

In it we’ll add the inputs field, and give it a list of the file patterns we want to format. In this case we’ll tell it to format all ‘.ex’ and ‘.exs’ files in our lib directory and our mix.exs file.

There are a few other options we can add to customize our formatter - let’s add two of them: line_length and rename_deprecated_at

First we’ll add line_length, which simply tells the formatter our limit on the length of a line.

Let’s set ours to 60.

Before we add rename_deprecated_at let’s take another look at our module.

In our word_counter function we’re calling Enum.partition which is deprecated in favor of Enum.split_with.

Back in our .formatter.exs file we’ll add rename_deprecated_at, which changes all deprecated functions to their non-deprecated equivalent.

Then we’ll specify the version we’re using - “1.4.5”.

.formatter.exs

[
  inputs: [
    "lib/**/*.{ex,exs}",
    "mix.exs"
  ],
  line_length: 60,
  rename_deprecated_at: "1.4.5"
]

And with that let’s go to the command line and run: $ mix format

Going back to our module, we can see that it’s been formatted according to our new specifications.

Our long lines were broken up and the deprecated Enum.partition and been changed to Enum.split_with

Currently the formatting rules are still being defined, but the elixir-style-guide is the closest guide.

I think this is a great addition to Elixir and can’t wait until Elixir 1.6 is released.

© 2024 HEXMONSTER LLC