@@ -43,7 +43,7 @@ pub(crate) fn rewrite_all_pairs(
43
43
context : & RewriteContext ,
44
44
) -> Option < String > {
45
45
// First we try formatting on one line.
46
- if let Some ( list) = expr. flatten ( context , false ) {
46
+ if let Some ( list) = expr. flatten ( false ) {
47
47
if let Some ( r) = rewrite_pairs_one_line ( & list, shape, context) {
48
48
return Some ( r) ;
49
49
}
@@ -53,7 +53,7 @@ pub(crate) fn rewrite_all_pairs(
53
53
// to only flatten pairs with the same operator, that way we don't
54
54
// necessarily need one line per sub-expression, but we don't do anything
55
55
// too funny wrt precedence.
56
- expr. flatten ( context , true )
56
+ expr. flatten ( true )
57
57
. and_then ( |list| rewrite_pairs_multiline ( list, shape, context) )
58
58
}
59
59
@@ -83,33 +83,22 @@ fn rewrite_pairs_one_line<T: Rewrite>(
83
83
result. push ( ' ' ) ;
84
84
}
85
85
86
+ let prefix_len = result. len ( ) ;
86
87
let last = list. list . last ( ) . unwrap ( ) ;
87
88
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 ) ;
90
91
91
92
if first_line_width ( & result) > shape. width {
92
93
return None ;
93
94
}
94
95
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 ;
113
102
}
114
103
115
104
wrap_str ( result, context. config . max_width ( ) , shape)
@@ -272,19 +261,18 @@ trait FlattenPair: Rewrite + Sized {
272
261
// operator into the list. E.g,, if the source is `a * b + c`, if `_same_op`
273
262
// is true, we make `[(a * b), c]` if `_same_op` is false, we make
274
263
// `[a, b, c]`
275
- fn flatten ( & self , _context : & RewriteContext , _same_op : bool ) -> Option < PairList < Self > > {
264
+ fn flatten ( & self , _same_op : bool ) -> Option < PairList < Self > > {
276
265
None
277
266
}
278
267
}
279
268
280
269
struct PairList < ' a , ' b , T : Rewrite + ' b > {
281
270
list : Vec < & ' b T > ,
282
271
separators : Vec < & ' a str > ,
283
- separator_place : SeparatorPlace ,
284
272
}
285
273
286
274
impl FlattenPair for ast:: Expr {
287
- fn flatten ( & self , context : & RewriteContext , same_op : bool ) -> Option < PairList < ast:: Expr > > {
275
+ fn flatten ( & self , same_op : bool ) -> Option < PairList < ast:: Expr > > {
288
276
let top_op = match self . node {
289
277
ast:: ExprKind :: Binary ( op, _, _) => op. node ,
290
278
_ => return None ,
@@ -323,7 +311,6 @@ impl FlattenPair for ast::Expr {
323
311
Some ( PairList {
324
312
list,
325
313
separators,
326
- separator_place : context. config . binop_separator ( ) ,
327
314
} )
328
315
}
329
316
}
0 commit comments