Skip to content

Commit c385688

Browse files
committed
Merge pull request #74 from cassiersg/paren-expr
Add reformating for parentheses
2 parents 6a5ef17 + 09bd4a7 commit c385688

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ impl<'a> FmtVisitor<'a> {
125125
format!("{}({})", callee_str, args_str)
126126
}
127127

128+
fn rewrite_paren(&mut self, subexpr: &ast::Expr, width: usize, offset: usize) -> String {
129+
debug!("rewrite_paren, width: {}, offset: {}", width, offset);
130+
// 1 is for opening paren, 2 is for opening+closing, we want to keep the closing
131+
// paren on the same line as the subexpr
132+
let subexpr_str = self.rewrite_expr(subexpr, width-2, offset+1);
133+
debug!("rewrite_paren, subexpr_str: `{}`", subexpr_str);
134+
format!("({})", subexpr_str)
135+
}
136+
128137
pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
129138
match expr.node {
130139
ast::Expr_::ExprLit(ref l) => {
@@ -140,6 +149,9 @@ impl<'a> FmtVisitor<'a> {
140149
ast::Expr_::ExprCall(ref callee, ref args) => {
141150
return self.rewrite_call(callee, args, width, offset);
142151
}
152+
ast::Expr_::ExprParen(ref subexpr) => {
153+
return self.rewrite_paren(subexpr, width, offset);
154+
}
143155
_ => {}
144156
}
145157

tests/idem/paren.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test parenthesis
2+
3+
fn foo() {
4+
let very_long_variable_name = (a + first + simple + test);
5+
let very_long_variable_name = (a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBBB +
6+
b + c);
7+
}

0 commit comments

Comments
 (0)