@@ -64,7 +64,8 @@ use config::IndentStyle;
64
64
use expr:: rewrite_call;
65
65
use macros:: convert_try_mac;
66
66
use rewrite:: { Rewrite , RewriteContext } ;
67
- use utils:: { first_line_width, last_line_extendable, last_line_width, mk_sp, wrap_str} ;
67
+ use utils:: { first_line_width, last_line_extendable, last_line_width, mk_sp,
68
+ trimmed_last_line_width, wrap_str} ;
68
69
69
70
use std:: cmp:: min;
70
71
use std:: iter;
@@ -125,7 +126,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
125
126
126
127
let first_child_shape = if extend {
127
128
let overhead = last_line_width ( & parent_rewrite) ;
128
- let offset = parent_rewrite . lines ( ) . rev ( ) . next ( ) . unwrap ( ) . trim ( ) . len ( ) ;
129
+ let offset = trimmed_last_line_width ( & parent_rewrite ) ;
129
130
match context. config . chain_indent ( ) {
130
131
IndentStyle :: Visual => parent_shape. offset_left ( overhead) ?,
131
132
IndentStyle :: Block => parent_shape. block ( ) . offset_left ( offset) ?,
@@ -156,7 +157,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
156
157
last_line_width ( & parent_rewrite)
157
158
} else {
158
159
rewrites. iter ( ) . fold ( 0 , |a, b| a + b. len ( ) ) + parent_rewrite. len ( )
159
- } ;
160
+ } + suffix_try_num ;
160
161
let one_line_budget = if rewrites. is_empty ( ) && !context. config . chain_split_single_child ( ) {
161
162
shape. width
162
163
} else {
@@ -165,25 +166,64 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
165
166
let all_in_one_line = !parent_rewrite_contains_newline
166
167
&& rewrites. iter ( ) . all ( |s| !s. contains ( '\n' ) )
167
168
&& almost_total < one_line_budget;
168
- let last_shape = if rewrites. is_empty ( ) {
169
- // We only have a single child.
170
- first_child_shape
171
- } else {
172
- match context. config . chain_indent ( ) {
173
- IndentStyle :: Visual => other_child_shape. sub_width ( shape. rhs_overhead ( context. config ) ) ?,
174
- IndentStyle :: Block => other_child_shape,
175
- }
169
+ let last_shape = match context. config . chain_indent ( ) {
170
+ IndentStyle :: Visual => other_child_shape. sub_width ( shape. rhs_overhead ( context. config ) ) ?,
171
+ IndentStyle :: Block => other_child_shape,
176
172
} ;
177
173
let last_shape = last_shape. sub_width ( suffix_try_num) ?;
174
+
175
+ // Rewrite the last child. The last child of a chain requires special treatment. We need to
176
+ // know whether 'overflowing' the last child make a better formatting:
177
+ //
178
+ // A chain with overflowing the last child:
179
+ // ```
180
+ // parent.child1.child2.last_child(
181
+ // a,
182
+ // b,
183
+ // c,
184
+ // )
185
+ // ```
186
+ //
187
+ // A chain without overflowing the last child (in vertical layout):
188
+ // ```
189
+ // parent
190
+ // .child1
191
+ // .child2
192
+ // .last_child(a, b, c)
193
+ // ```
194
+ //
195
+ // In particular, overflowing is effective when the last child is a method with a multi-lined
196
+ // block-like argument (e.g. closure):
197
+ // ```
198
+ // parent.child1.chlid2.last_child(|a, b, c| {
199
+ // let x = foo(a, b, c);
200
+ // let y = bar(a, b, c);
201
+ //
202
+ // // ...
203
+ //
204
+ // result
205
+ // })
206
+ // ```
207
+
208
+ // `rewrite_last` rewrites the last child on its own line. We use a closure here instead of
209
+ // directly calling `rewrite_chain_subexpr()` to avoid exponential blowup.
178
210
let rewrite_last = || rewrite_chain_subexpr ( last_subexpr, total_span, context, last_shape) ;
179
211
let ( last_subexpr_str, fits_single_line) = if all_in_one_line || extend_last_subexr {
212
+ // First we try to 'overflow' the last child and see if it looks better than using
213
+ // vertical layout.
180
214
parent_shape. offset_left ( almost_total) . map ( |shape| {
181
215
if let Some ( rw) = rewrite_chain_subexpr ( last_subexpr, total_span, context, shape) {
216
+ // We allow overflowing here only if both of the following conditions match:
217
+ // 1. The entire chain fits in a single line expect the last child.
218
+ // 2. `last_chlid_str.lines().count() >= 5`.
182
219
let line_count = rw. lines ( ) . count ( ) ;
183
220
let fits_single_line = almost_total + first_line_width ( & rw) <= one_line_budget;
184
- if fits_single_line && ( line_count >= 5 || extend_last_subexr ) {
221
+ if fits_single_line && line_count >= 5 {
185
222
( Some ( rw) , true )
186
223
} else {
224
+ // We could not know whether overflowing is better than using vertical layout,
225
+ // just by looking at the overflowed rewrite. Now we rewrite the last child
226
+ // on its own line, and compare two rewrites to choose which is better.
187
227
match rewrite_last ( ) {
188
228
Some ( ref new_rw) if !fits_single_line => ( Some ( new_rw. clone ( ) ) , false ) ,
189
229
Some ( ref new_rw) if new_rw. lines ( ) . count ( ) >= line_count => {
0 commit comments