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
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
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
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
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
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!







