Elixir code formatter?

I find it hard to keep my elixir code base consistent. There are so many ways to express the same thing in elixir and it is hard not to mix styles even in your own code base.

Is there a configurable elixir formatter out there somewhere that could help with these things.

The formatter in editor of course helps with indentation and similar things. I am more looking for things like:

  • Always put parenthesis around function calls
  • Always put [] on options lists
  • Convert all keyword list to proper lists and maps

Or the other way around if you are built wrong:)

I did find https://github.com/lpil/exfmt but it still seems early stages and is not documented well.

Just wondering if there is anything else out there or if this is something anyone else has experienced problem with. I come from the erlang world and there are not as many ways to express yourself there.

I use credo in my project. It seems more popular than dogma and newer. Works out of the box and there’s a editor plugin for Visual Studio Code as well.

I’ve gotten really used to prettier for Javascript code, I wish something like that existed for Elixir.

2 Likes

I’m currently working (https://github.com/KronicDeth/intellij-elixir/compare/v5.1.0...98) on the formatter (https://github.com/KronicDeth/intellij-elixir/issues/98) for IntelliJ Elixir. It’s working well (https://github.com/C-S-D/alembic/pull/46/files) for https://github.com/C-S-D/alembic, but I need to get it passing against our other two open-source libraries (https://github.com/C-S-D/calcinator and https://github.com/C-S-D/retort) before I’ll deem it ready-for-release, but then it will be in the next release of IntelliJ Elixir. So, it’s not generally available now, but will be soon and I could spin out a beta build if you want to test it out.

4 Likes

Yeah. I tried exfmt. But it swallowed/deleted some code in my implementation.

It is still very raw.

I’m hoping it grows into something like elm-format

1 Like

Thanks!

That looks pretty comprehensive. I will test it out

I wonder if we could not just piggyback on the output of Macro.to_string.
By default it does almost what you want, except it:

  • Does not care about def, defmodule etc. usually being used without parentheses, so by default the result would be similar to:
defmodule(Foo) do
  def(bar(a, b)) do
    "test"
  end
end
  • Will not wrap [] around keyword lists used as last argument in a function.

However, Macro.to_string allows you to pass in a second function which can alter how a certain node is shown as string.

This does mean we need to check quite some special cases (infix binary operators, block-containing keyword lists, etc.) but I still think it is the easiest way, since a major part of the work is already done for us.

1 Like

@devonestes mentioned that elixirfmt is being worked on in GSOC

1 Like

Always? Because even def is just a call too, almost everything is:

iex(2)> quote do def add(a,b), do: a+b end |> Macro.to_string() |> IO.puts()
def(add(a, b)) do
  a + b
end
:ok

Even if you special-cased def and the similar, what about library-specific versions?

I do like and use credo though. :slight_smile:

That bugs me, [/] should always wrap those…

But yep, this here.

Always? Because even def is just a call too, almost everything is:

Ok not always:) Just where I want them to be :slight_smile:

I thought def was a macro not a function if that makes any distinction
and perhaps it is not possible to make that destinction in a linter.

Macro’s are functions too, they are just called at compile-time instead of run-time, but they are still functions. :slight_smile:

Maybe @lpil could answer some questions about how documentation has being going on.

We have a student working on an Elixir Formatter as part of the Google Summer of Code. @lpil has been invited to the group and is helping us progress on this front at the same time he improves his library. Maybe we will have a beta version by Elixir v1.6 (which is Jan/2018) but no guarantees.

4 Likes

My formatter is not well documented because it’s not ready to be used. Once it’s a bit more robust and feature complete work on developer experience will begin. :slight_smile:

4 Likes

That is great news. Hope it turns out well.