diff --git a/src/expr.rs b/src/expr.rs index c1cbf175645..66a79a3a777 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -125,6 +125,23 @@ impl<'a> FmtVisitor<'a> { format!("{}({})", callee_str, args_str) } + fn rewrite_paren(&mut self, subexpr: &ast::Expr, width: usize, offset: usize) -> String { + debug!("rewrite_paren, width: {}, offset: {}", width, offset); + // 1 is for opening paren + let subexpr_str = self.rewrite_expr(subexpr, width-1, offset+1); + debug!("rewrite_paren, subexpr_str: `{}`", subexpr_str); + let mut lines = subexpr_str.rsplit('\n'); + let last_line_len = lines.next().unwrap().len(); + let last_line_offset = if lines.next().is_none() {offset+1} else {0}; + if width + offset - last_line_offset - last_line_len > 0 { + format!("({})", subexpr_str) + } else { + // FIXME That's correct unless we have width < 2. Return an Optrion for such cases ? + format!("({}\n{} )", subexpr_str, make_indent(offset)) + } + } + + pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String { match expr.node { ast::Expr_::ExprLit(ref l) => { @@ -140,6 +157,9 @@ impl<'a> FmtVisitor<'a> { ast::Expr_::ExprCall(ref callee, ref args) => { return self.rewrite_call(callee, args, width, offset); } + ast::Expr_::ExprParen(ref subexpr) => { + return self.rewrite_paren(subexpr, width, offset); + } _ => {} }