tensiondriven
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tensiondriven
I see there are projects like Overview — Sourceror v1.12.2 and fix/lib/fix.ex at master · wojtekmach/fix · GitHub which aim to do deeper introspection on Elixir code - all i’m looking for here is whitespace formatting.
dimitarvp
Yep, quite a few people noticed that
mix formatdoes not aggressively collapse code that was expanded before. I don’t like it either but apparently the maintainers disagree.There’s a tool made on top of
sourceror, namely GitHub - hrzndhrn/recode: A linter with autocorrection and a refactoring tool. · GitHub (one contributor to it is @Marcus). You can check that tool out but sadly it doesn’t support the feature you and I want yet.tensiondriven
Brilliant, this is a start. Thanks @dimitarvp
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?
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.
dimitarvp
Look at OP’s 3rd example. If you manually expand – needlessly – a very short expression, then the formatter never collapses it back.
sodapopcan
I did—At least if by third example I assume you’re talking about breaking up
%{\n\ta: 1\n}. Certainly an argument could be made that if there is only one element it could collapse, but all I was trying to say was that it seems like it’s trying to keep the rule dead simple which is: “If you want line breaks in your function heads and data-structures, you got 'em.”dimitarvp
Sure, but that particular choice is oddly specific and I suspect it’s tied to the personal preferences of the maintainers. Which, if I was politically inclined, would say is unfair.
I like Rust’s formatter more – it’s (a) ruthless and (b) very configurable. It would collapse back the example OP gave us. Its philosophy is “if I can fit that in one line then I absolutely will”. Or “if I spot needless curly braces (only one expression) then I am removing them”. A canonical form of [minimal] code, we might say.
Most people just go with the defaults, especially if contributing to open source. But there’s also place for internal team enforced formatter config. IMO the perfect state of affairs: strongly opinionated defaults with ability to customize.
Code is not a form of personal art expression.
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.
sodapopcan
To be clear I wasn’t saying you’re wrong, just trying to offer some reasoning as to the why it is the way it is. Although we certainly aren’t aligned on “if it can fit on one line, do it” but absolutely aligned on allowing customization for internal formatters.
Personally the only thing I really hate is not being able to put
doon its own line inwithstatements.withcan get super ugly and I find that little change makes everything dramatically clearer.The default with the outdent denoting the block always looks to me like someone forgot to format.
That’s a bit of a tangent but does technically have to do with whitespace
dimitarvp
Sorry if I was unclear: I am not saying you’re wrong either. Just preferences.