@@ -43,7 +43,7 @@ pub(crate) fn rewrite_all_pairs(
4343 context : & RewriteContext ,
4444) -> Option < String > {
4545 // First we try formatting on one line.
46- if let Some ( list) = expr. flatten ( context , false ) {
46+ if let Some ( list) = expr. flatten ( false ) {
4747 if let Some ( r) = rewrite_pairs_one_line ( & list, shape, context) {
4848 return Some ( r) ;
4949 }
@@ -53,7 +53,7 @@ pub(crate) fn rewrite_all_pairs(
5353 // to only flatten pairs with the same operator, that way we don't
5454 // necessarily need one line per sub-expression, but we don't do anything
5555 // too funny wrt precedence.
56- expr. flatten ( context , true )
56+ expr. flatten ( true )
5757 . and_then ( |list| rewrite_pairs_multiline ( list, shape, context) )
5858}
5959
@@ -83,33 +83,22 @@ fn rewrite_pairs_one_line<T: Rewrite>(
8383 result. push ( ' ' ) ;
8484 }
8585
86+ let prefix_len = result. len ( ) ;
8687 let last = list. list . last ( ) . unwrap ( ) ;
8788 let cur_shape = base_shape. offset_left ( last_line_width ( & result) ) ?;
88- let rewrite = last. rewrite ( context, cur_shape) ?;
89- result. push_str ( & rewrite ) ;
89+ let last_rewrite = last. rewrite ( context, cur_shape) ?;
90+ result. push_str ( & last_rewrite ) ;
9091
9192 if first_line_width ( & result) > shape. width {
9293 return None ;
9394 }
9495
95- // Check the last expression in the list. We let this expression go over
96- // multiple lines, but we check that if this is necessary, then we can't
97- // do better using multi-line formatting.
98- if !is_single_line ( & result) {
99- let multiline_shape = shape. offset_left ( list. separators . last ( ) . unwrap ( ) . len ( ) + 1 ) ?;
100- let multiline_list: PairList < T > = PairList {
101- list : vec ! [ last] ,
102- separators : vec ! [ ] ,
103- separator_place : list. separator_place ,
104- } ;
105- // Format as if we were multi-line.
106- if let Some ( rewrite) = rewrite_pairs_multiline ( multiline_list, multiline_shape, context) {
107- // Also, don't let expressions surrounded by parens go multi-line,
108- // this looks really bad.
109- if rewrite. starts_with ( '(' ) || is_single_line ( & rewrite) {
110- return None ;
111- }
112- }
96+ // Check the last expression in the list. We sometimes let this expression
97+ // go over multiple lines, but we check for some ugly conditions.
98+ if !( is_single_line ( & result) || last_rewrite. starts_with ( '{' ) )
99+ && ( last_rewrite. starts_with ( '(' ) || prefix_len > context. config . tab_spaces ( ) )
100+ {
101+ return None ;
113102 }
114103
115104 wrap_str ( result, context. config . max_width ( ) , shape)
@@ -215,11 +204,12 @@ where
215204 // If the length of the lhs is equal to or shorter than the tab width or
216205 // the rhs looks like block expression, we put the rhs on the same
217206 // line with the lhs even if the rhs is multi-lined.
218- let allow_same_line = lhs_result. len ( ) <= tab_spaces || rhs_result
219- . lines ( )
220- . next ( )
221- . map ( |first_line| first_line. ends_with ( '{' ) )
222- . unwrap_or ( false ) ;
207+ let allow_same_line = lhs_result. len ( ) <= tab_spaces
208+ || rhs_result
209+ . lines ( )
210+ . next ( )
211+ . map ( |first_line| first_line. ends_with ( '{' ) )
212+ . unwrap_or ( false ) ;
223213 if !rhs_result. contains ( '\n' ) || allow_same_line {
224214 let one_line_width = last_line_width ( & lhs_result)
225215 + pp. infix . len ( )
@@ -272,19 +262,18 @@ trait FlattenPair: Rewrite + Sized {
272262 // operator into the list. E.g,, if the source is `a * b + c`, if `_same_op`
273263 // is true, we make `[(a * b), c]` if `_same_op` is false, we make
274264 // `[a, b, c]`
275- fn flatten ( & self , _context : & RewriteContext , _same_op : bool ) -> Option < PairList < Self > > {
265+ fn flatten ( & self , _same_op : bool ) -> Option < PairList < Self > > {
276266 None
277267 }
278268}
279269
280270struct PairList < ' a , ' b , T : Rewrite + ' b > {
281271 list : Vec < & ' b T > ,
282272 separators : Vec < & ' a str > ,
283- separator_place : SeparatorPlace ,
284273}
285274
286275impl FlattenPair for ast:: Expr {
287- fn flatten ( & self , context : & RewriteContext , same_op : bool ) -> Option < PairList < ast:: Expr > > {
276+ fn flatten ( & self , same_op : bool ) -> Option < PairList < ast:: Expr > > {
288277 let top_op = match self . node {
289278 ast:: ExprKind :: Binary ( op, _, _) => op. node ,
290279 _ => return None ,
@@ -320,11 +309,7 @@ impl FlattenPair for ast::Expr {
320309 }
321310
322311 assert_eq ! ( list. len( ) - 1 , separators. len( ) ) ;
323- Some ( PairList {
324- list,
325- separators,
326- separator_place : context. config . binop_separator ( ) ,
327- } )
312+ Some ( PairList { list, separators } )
328313 }
329314}
330315
0 commit comments