You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Requests: Heredocs/Locals without Parens/HEEx attrs? #88
Hey hey, thanks for Recode! I started writing some of these rules myself before I remember it existed (d'oh).
These are merely suggestions based on my top annoyances that were not covered by Recode. I'm happy to take the implementation of any of them.
Indent heredocs at the same level as their sigil.
There are some considerations here as you can't blindly re-indent everything. I feel the safest is to assume that if the first line is indented to far then reduce everything below it by the same amount (unless it "reverse-overflows").
Remove parens from :locals_without_parens.
I'm not entirely sure this is possible to reliably identify everything in their proper files. There is Mix.Tasks.Format.formatter_for_file/2 though I haven't played around with it too much and seems to return the same results for every file.
Correct if foo == bar, do: true, else: false to foo == bar
Correct boolean={true} HEEx attribute to simply boolean
Not sure if you would want to get HEEx involved but the above always annoys me when I see it.
Thank you for your suggestions. A PR is always welcome if you want to tackle one of your suggestions yourself.
Some comments:
Indent heredocs at the same level as their sigil.
I am not sure if this can be handled in recode. When recode changes the AST the new AST goes to the Elixir formatter and the code will be formatted with the formatter rules. Maybe this is something that has to go in a custom formatter or in something like FreedomFormater.
Remove parens from :locals_without_parens.
Great Idea. That should be possible in recode. The Mix.Tasks.Format.formatter_for_file/2 is the right function to get the keyword list for locals_without_parens. The function returns different results for different files only when the project contains more than one .formatter.exs.
Correct if foo == bar, do: true, else: false to foo == bar
That should be easy and it is a good candidate for a first recode-task.
Correct boolean={true} HEEx attribute to simply boolean
Recode should also handle HEEx files. For now I haven't tried to change such files.
The function returns different results for different files only when the project contains more than one .formatter.exs.
Ah that makes sense, thanks for explaining that! I have half-way done implementation of this (it just doesn't take arity into account) so happy to fix it up and make a PR. I can certainly also handle the comparison one.
As for heredoc formatting, Elixir's formatter leave indentation alone so long as it's legal. I have a naive implementation that I could work on a bit if you're interested. To clarify I'm looking to turn this:
Ah, now I understand the thing with the heredoc. For HD in general this makes less sense, because we can't know if someone did it for a good reason. Maybe a task like DocFormatting would be useful.
Ya, basically doc formatting, but I'm also thinking for LiveView as well:
~H"""
<div>
</div>
"""
becomes:
~H"""
<div>
</div>
"""
I agree it's harder to figure out here. As far as I can tell, the only good heuristic is to see if the first line is indented too far then just un-indent every line by that much unless it's illegal to do.
For example:
@doc""" Hello! Oh hiI'm back here for some reason I'm a list: - one - two - three"""
would become:
@doc"""Hello!Oh hiI'm back here for some reasonI'm a list: - one - two - three"""
But:
@doc"""I'm a doc. The rest of me is written like this for some reason..."""
...would just be left alone.
The point of this corrector is just to catch people doing the extra indent on every line which is very common. Anyone who does something like that last example are people who just like to watch the world burn AFAIC and there's not much you can do about them 😅
Activity
NickNeck commentedon Jun 26, 2024
Thank you for your suggestions. A PR is always welcome if you want to tackle one of your suggestions yourself.
Some comments:
I am not sure if this can be handled in
recode. When recode changes the AST the new AST goes to the Elixir formatter and the code will be formatted with the formatter rules. Maybe this is something that has to go in a custom formatter or in something likeFreedomFormater.Great Idea. That should be possible in
recode. TheMix.Tasks.Format.formatter_for_file/2is the right function to get the keyword list forlocals_without_parens. The function returns different results for different files only when the project contains more than one.formatter.exs.That should be easy and it is a good candidate for a first recode-task.
Recode should also handle
HEExfiles. For now I haven't tried to change such files.sodapopcan commentedon Jun 26, 2024
Hey Marcus!
Ah that makes sense, thanks for explaining that! I have half-way done implementation of this (it just doesn't take arity into account) so happy to fix it up and make a PR. I can certainly also handle the comparison one.
As for heredoc formatting, Elixir's formatter leave indentation alone so long as it's legal. I have a naive implementation that I could work on a bit if you're interested. To clarify I'm looking to turn this:
to this:
Not a big deal if you aren't interested, though.
Thanks and I'll be in touch!
NickNeck commentedon Jun 28, 2024
Ah, now I understand the thing with the
heredoc. For HD in general this makes less sense, because we can't know if someone did it for a good reason. Maybe a task likeDocFormattingwould be useful.sodapopcan commentedon Jun 30, 2024
Ya, basically doc formatting, but I'm also thinking for LiveView as well:
becomes:
I agree it's harder to figure out here. As far as I can tell, the only good heuristic is to see if the first line is indented too far then just un-indent every line by that much unless it's illegal to do.
For example:
would become:
But:
...would just be left alone.
The point of this corrector is just to catch people doing the extra indent on every line which is very common. Anyone who does something like that last example are people who just like to watch the world burn AFAIC and there's not much you can do about them 😅
The indentation level could even be configurable!
NickNeck commentedon Jul 2, 2024
I think
~Hwill be formatted byPhoenix.LiveView.HTMLFormatter.A checker for
@doccould be useful for a few people.