Skip to content

Commit 0baeca5

Browse files
committed
Add rewrite for ExprParen
1 parent 6a5ef17 commit 0baeca5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/expr.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ 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
131+
let subexpr_str = self.rewrite_expr(subexpr, width-1, offset+1);
132+
debug!("rewrite_paren, subexpr_str: `{}`", subexpr_str);
133+
let mut lines = subexpr_str.rsplit('\n');
134+
let last_line_len = lines.next().unwrap().len();
135+
let last_line_offset = if lines.next().is_none() {offset+1} else {0};
136+
if width + offset - last_line_offset - last_line_len > 0 {
137+
format!("({})", subexpr_str)
138+
} else {
139+
// FIXME That's correct unless we have width < 2. Return an Optrion for such cases ?
140+
format!("({}\n{} )", subexpr_str, make_indent(offset))
141+
}
142+
}
143+
144+
128145
pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
129146
match expr.node {
130147
ast::Expr_::ExprLit(ref l) => {
@@ -140,6 +157,9 @@ impl<'a> FmtVisitor<'a> {
140157
ast::Expr_::ExprCall(ref callee, ref args) => {
141158
return self.rewrite_call(callee, args, width, offset);
142159
}
160+
ast::Expr_::ExprParen(ref subexpr) => {
161+
return self.rewrite_paren(subexpr, width, offset);
162+
}
143163
_ => {}
144164
}
145165

0 commit comments

Comments
 (0)