Mix format skip Ecto.Query.from/2 parens

I’m quite happy with the new mix format task, except for my Ecto queries :smile:

Currently mix format would convert

query = from u in User, where: is_nil(u.team_id)

to

query = 
  from(u in User, where: is_nil(u.team_id))

and I’d like it to be

query = 
  from u in User, where: is_nil(u.team_id)

I looked at the docs, and there’s an option to make the formatter skip some parens, but I’m either doing it wrong or it’s not meant to work with 3rd party libs. Here’s my .formatter.exs:

[
  inputs: [
    "mix.exs",
    "{config,lib,test}/**/*.{ex,exs}"
  ],
  export: [
    locals_without_parens: [from: 2]
  ]
]

Any ideas, anyone? Thanks!

:export is meant to configure formatter for projects depending on your project, not for your project itself.

Change to the following and you should be good to go:

[
  inputs: [
    "mix.exs",
    "{config,lib,test}/**/*.{ex,exs}"
  ],
  locals_without_parens: [from: 2]
]

When Ecto adopts .formatter.exs and exports the settings, you’ll be able to import them with import_deps: [:ecto] (https://hexdocs.pm/mix/Mix.Tasks.Format.html#module-importing-dependencies-configuration)

3 Likes

Thanks @wojtekmach, well spotted! Yeah, I think I’ll wait for Ecto to adopt the formatter — there’s a lot of Schema/Query DSL which I’d prefer not to list manually.