@@ -125,6 +125,23 @@ impl<'a> FmtVisitor<'a> {
125
125
format ! ( "{}({})" , callee_str, args_str)
126
126
}
127
127
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
+
128
145
pub fn rewrite_expr ( & mut self , expr : & ast:: Expr , width : usize , offset : usize ) -> String {
129
146
match expr. node {
130
147
ast:: Expr_ :: ExprLit ( ref l) => {
@@ -140,6 +157,9 @@ impl<'a> FmtVisitor<'a> {
140
157
ast:: Expr_ :: ExprCall ( ref callee, ref args) => {
141
158
return self . rewrite_call ( callee, args, width, offset) ;
142
159
}
160
+ ast:: Expr_ :: ExprParen ( ref subexpr) => {
161
+ return self . rewrite_paren ( subexpr, width, offset) ;
162
+ }
143
163
_ => { }
144
164
}
145
165
0 commit comments