VSCode Elixir Format - Prevent auto parentheses

Hello !

I would like to prevent automatic parentheses while coding in Elixir in VSCode.

I can’t see where to configure that.

Any suggestions ?

1 Like

The elixir formatter doesn’t have much in the way of options. The idea is that you accept how it is and move on with your life, free from worrying about formatting.

There are libraries, ecto, phoenix etc, that tell it not to add them to some macros, which you need to add to formatter.exs.

If you’re just talking about it creating the second bracket, look in the VS Code settings.

3 Likes

The good news is, that’s one of the few customisable options.
The bad news is, you have to specify every function name and arity where you don’t want it to automatically add the parenthesis.

Check out the mix format and Code.format_format_string/2 docs. You’re looking for :locals_without_parens.

See Plug for an example.

2 Likes

I’ve also had this issue. The config is in formatter.exs and running mix format works as expected. In vscode though it only works intermittently, the rest of the time it will start adding brackets in again.

1 Like

Is this about them being added ‘on format’ or during typing? The latter seems like an option which is editor specific….

I have to precise. It’s annoying for 2 particular situations:

Macros like add and field (in Ecto Migrations and Schemas)

3 Likes

Have you got the inpit_deps set in .formatter.exs? The ecto setting should stop that. However, as someone pointed out, it will randomly screw it up every now and then. You have git to save you in those instances.

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