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.

4 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.

2 Likes

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)

4 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

I just ran into this issue and found this thread. Then I added and removed a dep and rearranged the order and then the parentheses went away when I saved. In my case this was for the prop declaration in surface.

Not sure if this was just luck or whether it helped. Can’t reproduce it now because problem is fixed and the parentheses are being removed. Hopefully this helps someone else test it in the future.

I experienced the same issue.
Similar to @Ivor, resetting the dependencies helped me.

rm mix.lock; rm -rf _build; rm -rf deps; mix deps.get

Before: Parantheses added by mix format, e.g. to ecto field macro.
After: Parantheses no longer added.

I did not test if all the steps were necessary, or if a single one of them might have helped.

1 Like

This is a bug with ElixirLS and it’s a trick one, it has to do with race conditions.
It’s being tracked here: Formatter doesn't respect formatter.exs while your code is still compiling · Issue #526 · elixir-lsp/elixir-ls · GitHub

3 Likes

Runing MIX_ENV=test mix compile --force and restarting VSCode seems to fix it for me.

2 Likes