tensiondriven
Mix formatter to remove unnecessary newlines?
I’ve looked around for information on this in the mix format source, custom mix formatters, Elixir style guides, and haven’t found anything, so please forgive me if this has been covered…
mix format does a great job of cleaning up unnecessary spaces in Elixir code and breaks long lines into multiple lines very well.
However, while it’s great at adding newlines when needed, there doesn’t seem to be any provision for removing unnecessary newlines.
I’m working on a team where there’s a tendency to use extensive pattern matching in function definitions, etc, which results in many multiline statements. When this code is edited and shortened, often the revised statements will fit on one line, but because the formatter doesn’t do this for us, I frequently see code that has unnecessary line breaks in it. The below should illustrate what I’m seeking. Note that the entirety of this code block has mix format applied:
# Functions w/ long params get split into multiple lines (expected, desired):
def function(
very_long_parameter_that_causes_line_break1,
very_long_parameter_that_causes_line_break2
) do
# ...
end
# Shortening the long variable names does not result in the function definition being shortened: (i would like it to)
def function(
shortened_param1,
shortened_param1
) do
# ...
end
# Manually shortening the params works, but it is laborious:
def function(shortened_param1, shortened_param1) do
# ...
end
# This applies to structs/maps, too. I'd like these unnecessary newlines to be removed...
def function() do
#
%{
a: 1
}
end
# ... so that after mix format, I have:
def function() do
%{a: 1}
end
Is it possible to detect and remove un-necessary newlines using mix format? I started going down the path of writing my own formatter, but am not up on all the details of the Code module and am hoping there’s an easier way - or, if not, perhaps someone can outline an example formatter that I could write which would do this.
Most Liked
LostKobrakai
Iirc there’s a clear stance of the elixir team that the formatter is not meant to be a all purpose formatter. It’s meant to support elixir own development and therefore likely fits a lot of other elixir open source projects as well. But it just might not be the tool for enforcing custom or internal code styles.
josevalim
Exactly what @LostKobrakai said. The formatter would aggressively collapse before but then a lot of people complained that sometimes they would write newlines on purpose and the formatter collapsed (and “ruined”) them. So we started respecting the user choice when it comes to newlines.
This pretty much sums up most formatter discussions. x% people are happy it works in one way, y% people are not.
If we were to change it, now y% people are happy it works in one way, x% people are not.
sodapopcan
Just in terms of reasoning, I believe it has to do with the ambiguity involved in defining “unnecessary”. For example, should the following be collapsed just because it fits in the line limit?
%{
one: some_longer_function_name("some_arg", "some_other_arg"),
two: "hello!"
}
That would look pretty terrible on one line (subjectively) and I believe it’s trying to allow for this type of thing. I’m personally totally cool with this as the diffs caused by tools like Prettier that put everything on a line it can make for some very painful diffs when those lines ultimately get broken up. The big downside, of course, is that you can’t just run the formatter to get stuff exactly as you want it.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









