-
Notifications
You must be signed in to change notification settings - Fork 883
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
Neatly-aligned multi-line expression folded into a single line #1684
Comments
Here's another case of a compact and IMHO very readable matrix notation exploded into multiple lines: let custom_primaries = CIExyYTRIPLE {
Red: CIExyY {x:0.630, y:0.340, Y:1.0},
Green: CIExyY {x:0.310, y:0.595, Y:1.0},
Blue: CIExyY {x:0.155, y:0.070, Y:1.0},
}; → let custom_primaries = CIExyYTRIPLE {
Red: CIExyY {
x: 0.630,
y: 0.340,
Y: 1.0,
},
Green: CIExyY {
x: 0.310,
y: 0.595,
Y: 1.0,
},
Blue: CIExyY {
x: 0.155,
y: 0.070,
Y: 1.0,
},
}; |
Unfortunately rustfmt is not smart enough to do these types of formatting. For now, please try |
This is one of the the larger pains I have with code formatting tools in most languages, where I otherwise love being able to automatically enforce style consistency. @topecongiro: Is there an architectural reason that rustfmt can't (or would have a difficult time) supporting this behavior? Would a pull request implementing it be likely to be accepted? (Any other issues specific to preserving well aligned code?) |
@kestred we basically can't do this - the architecture of rustfmt is that it rewrites the AST, not the source, and we don't take hints from the source in general. We found that if we do take hints from the source then we end up with non-idempotency. |
@nrc thanks for the explanation-- I hadn't considered the idempotency issue but am glad it is a goal. AlternativeRather than attempting to preserve neatly aligned code, it may instead make sense to proactively identify places in source code which could be intentionally neatly aligned. In some cases, this type of pro-active alignment already occurs (notably alignment of "end-of-line" comments for structure definitions and after I think (at least) these additional cases should be considered for proactive alignment:
Heuristics to identify when items/expressions would benefit from alignment:
ReasoningThe reasoning for the change (and consequently, a possible RFC to the style guide) I hope is fairly straightforward, as @kornelski mentioned:
Other CommentsOther, good reasoning, but with a harder solution:
vs
If the second solution was implemented, well supported, and known to users; then as |
Tangentially, I suspect this closed issue may not be the right place for a slightly more involved discussion ^; happy to move somewhere else if it makes sense (rustfmt RFC? internals forum?) |
I know it's a tough edge case, but rustfmt is not aware of multi-line formatting patterns/regularities in expressions.
I work with image processing and my code is full of expressions repeated for R, G, and B. Laying out such expressions this way:
makes it much easier to understand, spot errors, and edit them (with sublime's multiple carets).
However, rustfmt treats it as just a one long expression, and always unwraps it given a chance:
Would it be possible to preserve such aligned blocks of code? e.g. if parens and operators are aligned, and/or if there's a small edit distance between the lines.
The text was updated successfully, but these errors were encountered: