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