Skip to content

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

Merged
merged 1 commit into from
Jul 2, 2015
Merged

Format expressions with binary and unary operators #99

merged 1 commit into from
Jul 2, 2015

Conversation

marcusklaas
Copy link
Contributor

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

ast::UnOp::UnNeg => "-"
};

operator_str.to_owned() + &self.rewrite_expr(expr, width - operator_str.len(), offset)
Copy link
Member

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!

Copy link
Member

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

@nrc
Copy link
Member

nrc commented Jun 23, 2015

@marcusklaas is this ready to go? It also needs a rebase.

@marcusklaas
Copy link
Contributor Author

Rebased & ready 2 go!

@nrc
Copy link
Member

nrc commented Jun 23, 2015

Sorry, needs another rebase

@cassiersg
Copy link
Contributor

@marcusklaas If you have a bit of time, could you rebase this ? It would be fine to have this merged.

@marcusklaas
Copy link
Contributor Author

Absolutely!

@marcusklaas
Copy link
Contributor Author

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...

@nrc
Copy link
Member

nrc commented Jul 2, 2015

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)

nrc added a commit that referenced this pull request Jul 2, 2015
Format expressions with binary and unary operators
@nrc nrc merged commit 08b9837 into rust-lang:master Jul 2, 2015
@marcusklaas
Copy link
Contributor Author

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.

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

Successfully merging this pull request may close these issues.

3 participants