How can I force `mix format` to enforce the use of parentheses wherever they are possible?

My partner and I are working on a codebase together and we have something different in our setup that is causing his system to add parens while mine does not. We are both using the same .formatter.exs file, which looks like this:

[
  import_deps: [:ecto, :phoenix],
  inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
  subdirectories: ["priv/*/migrations"]
]

Our working theory is that we have something different configured in our VSCode plugins. In any case, I’d like us to get our systems on the exact same page in regard to formatting, so I’d just like to enforce the use of parentheses everywhere. How can I do this using mix format?

I’ve read the docs here – mix format: parens and no parens in function calls – but it is still not clear to me how I can force the use of parentheses everywhere possible.

I know that ecto and phoenix export their locals_without_parens and therefore no parens are added to function like from/2 or the ecto schema stuff (take a look at Ecto and Phoenix to see all functions / macros).
Not sure why your partners mix format adds them, if he uses the same config with [:ecto, :phoenix] as imports.

Edit: Which plugins do you use for formatting in VSCode? Just ElixirLS?

1 Like

That would result in quite weird looking code:

defmodule(Foo) do
  def(foo(arg)) do
    case(arg) do
      :ok -> {:ok, bar()}
      _ -> :error
    end
  end

  def(bar(), do: 10)
end
3 Likes

Hi, its not exactly what you are asking for, but I experienced the same, when formatting with ms code as a shortcut.
At the end we agreed to only use mix format on the terminal.

You can also try to delete the line with the import_deps, which makes it possible to omit brackets in phoenix router and schema files … and more others.