Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for consistency, stop lines from automatically collapsing even if such a joined line would not exceed max_width #5765

Closed
effinsky opened this issue May 7, 2023 · 4 comments

Comments

@effinsky
Copy link

effinsky commented May 7, 2023

For consistent formatting across multiple like statements, declarations, assignments etc, make it possible for separate lines to not unfold into a single line even if that line would be < max_width. This is inspired by own felt need/preference, the black Python formatter for Python, and the Zig formatter.

In both black for Python and zigfmt if you put a trailing comma on a list of params, for instance, that'll be a cue for the formatter to not collapse multiple lines into a single line even if they could be since, say, they comprise a single statement and are below max line length.

Wish we could have a thing like that in rustfmt. First and foremost this would help readability in cases where you have a series of statements where one does not fit a single line and therefore will be broken down into multiple lines, but the rest do and therefore will not be broken down. For ease of visual pattern recognition I'd like to allow for breaking them all down into the same formatting scheme across multiple lines.

Currently rustfmt formats this and like examples:
Screenshot 2023-05-07 at 21 11 19

I'd like to force line breaking into this much nicer formatting without sprinkling fmt skip and such all over the place:
Screenshot 2023-05-07 at 21 12 25

In Python's black... Unformatted:
Screenshot 2023-05-07 at 21 17 01

If I run black, it behaves similar to how rustfmt would, producing:
Screenshot 2023-05-07 at 21 18 09

But if I add trailing commas manually here and here:
Screenshot 2023-05-07 at 21 18 09

Then I get the nice result of:
Screenshot 2023-05-07 at 21 21 32

What do you think?

@ytmimi
Copy link
Contributor

ytmimi commented May 7, 2023

@effinsky Thanks for reaching out. Not sure what version of rustfmt you're using, but you can try to set short_array_element_width_threshold to a sufficiently small value and that should force rustfmt to format the lists vertically.

Also want to note that something similar was brought up in #5190.

Hope that helps, but I'm going to close this as we don't usually want the existence or lack of a comma in the input to dictate how we layout lists.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2023
@calebcartwright
Copy link
Member

Thank you for reaching out, and taking the time to articulate your suggestion so thoroughly!

To add on to what Yacin said, this is not something we'd be willing to incorporate for a few reasons, the main one being that this would be a source of idempotence issues due to the presence of existing features that manipulate trailing commas.

Your best bet to get your preferred formatting style is likely going to be to utilize a combination of the short_array_element_width_threshold and line comments where necessary to force a vertical layout

@effinsky
Copy link
Author

effinsky commented May 7, 2023

Thanks, and fyi rustfmt 1.5.2-nightly. I do find it odd that one needs to tweak the formatter to get visually consistent behavior. Your preference is what it is, of course, but I think it'd be easier to get this manually. It's not just about array behavior, after all. Nonetheless, cheers for your consideration.

@effinsky
Copy link
Author

effinsky commented Jan 23, 2024

how about adding a setting to preserve shorter lines even if more than a single statement or expression would otherwise fit on a single line? so no trailing comma manipulation, but a general setting or Preserve? or something like "max_line_length_within_limit" with a true, false, so that when I say false to that, I'll get to keep my shorter lines for alignment, readability etc?

I mean quite clearly at present we do lose some readability sometimes where we have subsequent analogous lines formatted differently. I'd be cool to be able to have that consistent formatting line to line, as in the examples above.

you can use fmt skip on those, yes, but that way you lose all formatting and all benefits of the tool. I'd rather not have these skips sprinkled over the code since that reduces readability too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants