I agree, I significantly prefer Rust’s formatter over elixir’s as well (and not just because it puts trailing comma’s on multi-line constructs, which it does do, lol), but it just seems overall ‘smarter’ and significantly more opinionated about things. Like with the example by @Sebb above with:
case number do
1 -> :one
2 -> :two
3 -> :three
number ->
IO.inspect(number)
:very_big
end
And Elixir’s formatter blows it up into that huge many-line annoyance, compare to how rust handles the same construct (I added an extra block on two
just to show it removes it):
match number {
1 => "one",
2 => {"two"}
3 => "three",
number => {
dbg!(number);
"very_big"
}
}
The cargo fmt
will change it to:
match number {
1 => "one",
2 => "two",
3 => "three",
number => {
dbg!(number);
"very_big"
}
}
It doesn’t matter how many lines things are, what format things are in, etc… etc… etc… The formatter literally just parses it into the Rust AST and then outputs it again. You can override things with #[...]
calls like disallowing formatting on a section for example as well, but honestly I’ve used that like once, it’s just really really good at formatting. Even in the config file the only change I made was to use tabs instead of spaces because screw 4 spaces being so crazy wide. ^.^;
And yes, if you remove the dbg
line it will format back in to:
match number {
1 => "one",
2 => "two",
3 => "three",
number => "very_big",
}
Yeah this is one of the worst things the elixir formatter does, and it’s not really its fault, but rather because I still to this day think that with
’s syntax was very mis-chosen (same with for
too). I had a better form of with
before with
existed as a macro, but the syntax formatting it used was broken by elixir in a backwards incompatible syntax update (around 1.6 or 1.7 or something, and by backwards incompatible I mean it was a form that macro’s took before, then the compiler errored on after the update, so yes, the very definition of backwards incompatible)…
This is something annoying about the formatter as well, it doesn’t really ‘attach’ comments to the AST it builds very well, though I got used to how it does it.