-
Notifications
You must be signed in to change notification settings - Fork 931
Format expressions with binary and unary operators #99
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
Conversation
ast::UnOp::UnNeg => "-" | ||
}; | ||
|
||
operator_str.to_owned() + &self.rewrite_expr(expr, width - operator_str.len(), offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer using format! rather than concatenation - I think this particular case is OK, but in general you are more likely to get better allocation behaviour using format!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also may as well use 1
instead of operator_str.len()
in this case
@marcusklaas is this ready to go? It also needs a rebase. |
Rebased & ready 2 go! |
Sorry, needs another rebase |
@marcusklaas If you have a bit of time, could you rebase this ? It would be fine to have this merged. |
Absolutely! |
Rebased. I've been trying to also format comments in between the operator and the operands, but couldn't think of a consistent way to display them. It'd probably be easiest to always use block-style comments inline, but that may ruin code like a +
// long comment for b
b or a // comment for a
+ b // comment for b I'd like to still give it a try in a different PR, but I could really use some ideas/ input on how to tackle this... |
honestly, I wouldn't bother - it's such an edge case I don't think it is worth fixing until we have a 'proper' way of doing it (which means comments in the AST, or something) |
Format expressions with binary and unary operators
Hmmm, the problem isn't so much capturing the comments, but rather formatting them. If (when?) we eventually we get comments in the AST, we'll still have to figure this out. Agreed that it's not really important right now, though. |
I experimented quite a bit and think this is a decent heuristic for formatting binary operations. It's not very clear when a binary operation triggers a line break inside a function call, as it breaks the '1 parameter per line' pattern.
cc https://github.com/nrc/rustfmt/issues/29, https://github.com/nrc/rustfmt/issues/30