ca1989

ca1989

Elixir formatter in neovim (using coc.nvim) sometimes adds parenthesis around macros

Hi all,

my editor (neovim + elixir-ls via coc.nvim) does format on save.

Randomly, it adds parenthesis around macros, so I have a diff looking like:

- get "/", PageController, :home
+ get("/", PageController, :home)

Is anyone experiencing something similar?

Thank you

Most Liked

linusdm

linusdm

I’d guess this is related to this elixir-ls issue: Formatter doesn't respect formatter.exs while your code is still compiling · Issue #526 · elixir-lsp/elixir-ls · GitHub

If you wait for the underlying compilation to finish, the formatter options are respected again, which makes it seem random.

georgeguimaraes

georgeguimaraes

You defintely can and should use nested .formatter.exs. I also have an umbrella app with several apps within.

Here’s my root .formatter.exs:

[
  inputs: ["mix.exs", "config/*.exs"],
  subdirectories: ["apps/*"]
]

And in the app that uses Ecto ./apps/analytics/.formatter.exs:

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

And in the phoenix app ./apps/analytics_web/.formatter.exs:

[
  import_deps: [:phoenix],
  plugins: [Phoenix.LiveView.HTMLFormatter],
  inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}"]
]

You can always generate a new umbrella Phoenix app that will give a boilerplate on how those files are generated.

This issue is related to LazyVim changing Neovim’s cwd which would cause some weird interactions with neotree, for instance. It would change the file tree from neotree to only display ./apps/analytics_web in certain scenarios.

Since you’re using LazyVim, which uses nvim-lspconfig, the actual “directory” for ElixirLs is defined here:

I changed that recently to give priority to a .git folder in the root dir, so that you only have one ElixirLS in the entire umbrella app.

elurker

elurker

Alright, sorry for the noise - as a newcomer I wasn’t aware there’s a dedicated mix new --umbrella (yay!) to set everything up properly. Coming from JS world where I had to set up monorepos manually, but here it’s turn-key. Now formatting seems to work fine, without any custom NeoVim or LS configuration.

(I should note that I still get the error from above if I try to save an Elixir file without giving ElixirLS a few seconds to start up, if I close and reopen Neovim. But that’s probably expected)

Thanks again for all your help!

dimitarvp

dimitarvp

Oh, here’s a more detailed example:

[
  inputs: ["mix.exs", ".formatter.exs", "{config,lib,test,priv}/**/*.{ex,exs}"],
  locals_without_parens: [
    # Plug
    plug: 1,
    plug: 2,

    # Phoenix
    pipe_through: 1,
    render: :*,
    action_fallback: 1,
    get: :*,
    post: :*,
    put: :*,
    delete: :*,

    # Ecto
    add: 2,
    add: 3,
    field: 2,
    belongs_to: 2,
    from: 2,
    embeds_many: 2
  ]
]
elurker

elurker

That didn’t seem to help at first, but it did re-create .elixir_ls dir in the parent (!) folder when I re-opened the child application folder using cd <app> && nvim . (seemingly without restoring nvim session)

And, it looks like I’ve overlooked the obvious - my .formatter.exs was created automatically by Phoenix at the root of the application directory, but there wasn’t one in the parent folder (where .elixir_ls is). I’ve just added this very simple one there:

[
  inputs: ["*.{heex,ex,exs}"]
]

and suddenly it no longer adds parenthesis! That said, now the one in the application folder doesn’t seem to apply. So It seems elixir_ls really “wants” to live in the parent folder when used via this Neovim setup. This is not the same behavior as in VS Code, where .elixir_ls is stored in the app folder if it’s opened directly. I might need to dig a bit deeper into that.

Thanks for helping me nail down this issue!

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement