Remove Parens in Elixir Formatter


Here's how to make the elixir formatter not add parens to certain functions.

Run formatter

Code formatting in Elixir can be accomplished with:

mix format

Customize format

To control how the formatter works, you can edit a .formatter.exs file. This file usually goes in the root of your project.

There aren't many levers to pull to modify the format. That's kind of the point. There's a conventional elixir code style. Even in this case, I wish there wasn't an option. But that's kinda hard in a language of macros and DSLs. And since there is, you might want to take the option that's great for you.

Instead of:

get(:get_book, :read)

You might like:

get :get_book, :read

To accomplish this, modify .formatter.exs to include something like this:

export_locals_without_parens = [
  plug: 1,
  plug: 2,
  forward: 2,
  forward: 3,
  forward: 4,
  match: 2,
  match: 3,
  get: 2,
  get: 3,
  head: 2,
  head: 3,
  list: 2,
  list: 3,
  post: 2,
  post: 3,
  put: 2,
  put: 3,
  patch: 2,
  patch: 3,
  delete: 2,
  delete: 3,
  options: 2,
  options: 3
]

[
  locals_without_parens: export_locals_without_parens,
]

All the places that you don't want parens, you can list in the list for export_locals_without_parens. Those are function names plus arity.

Note that I've noticed that this will keep a formatter from adding parens. But if the parens are already there and grammatically legal, the formatter won't remove them; and that's a shame.

What do you think? Is there any easier way to do this? (I'd like a "minimize all parens" option. And why do some formatters add the parens but others remove them?)