@@ -276,32 +276,39 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
276
276
indent : usize ,
277
277
ending : Ending ,
278
278
) -> impl fmt:: Display + ' a + Captures < ' tcx > {
279
+ let where_indent = 3 ;
280
+ let padding_amount = if ending == Ending :: Newline {
281
+ indent + 4
282
+ } else if indent == 0 {
283
+ 4
284
+ } else {
285
+ indent + where_indent + "where " . len ( )
286
+ } ;
287
+
279
288
display_fn ( move |f| {
280
- let mut where_predicates = gens. where_predicates . iter ( ) . filter ( |pred| {
281
- !matches ! ( pred, clean:: WherePredicate :: BoundPredicate { bounds, .. } if bounds. is_empty( ) )
282
- } ) . map ( |pred|
283
- print_where_pred ( cx, pred)
284
- ) . peekable ( ) ;
289
+ let mut where_predicates = gens
290
+ . where_predicates
291
+ . iter ( )
292
+ . filter ( |pred| {
293
+ !matches ! ( pred, clean:: WherePredicate :: BoundPredicate { bounds, .. } if bounds. is_empty( ) )
294
+ } )
295
+ . enumerate ( )
296
+ . map ( |( i, pred) | {
297
+ let ( padding_amount, add_newline) = if i == 0 && ending == Ending :: NoNewline && indent > 0 {
298
+ // put the first one on the same line as the 'where' keyword
299
+ ( 1 , false )
300
+ } else {
301
+ ( padding_amount, true )
302
+ } ;
303
+ print_where_pred_helper ( cx, pred, padding_amount, add_newline)
304
+ } )
305
+ . peekable ( ) ;
285
306
286
307
if where_predicates. peek ( ) . is_none ( ) {
287
308
return Ok ( ( ) ) ;
288
309
}
289
310
290
- let where_preds = comma_sep ( where_predicates, false ) ;
291
- let mut br_with_padding = String :: with_capacity ( 6 * indent + 28 ) ;
292
- br_with_padding. push ( '\n' ) ;
293
- let where_indent = 3 ;
294
- let padding_amount = if ending == Ending :: Newline {
295
- indent + 4
296
- } else if indent == 0 {
297
- 4
298
- } else {
299
- indent + where_indent + "where " . len ( )
300
- } ;
301
- for _ in 0 ..padding_amount {
302
- br_with_padding. push ( ' ' ) ;
303
- }
304
- let where_preds = where_preds. to_string ( ) . replace ( '\n' , & br_with_padding) ;
311
+ let where_preds = comma_sep ( where_predicates, false ) . to_string ( ) ;
305
312
let clause = if ending == Ending :: Newline {
306
313
let mut clause = " " . repeat ( indent. saturating_sub ( 1 ) ) ;
307
314
write ! ( clause, "<span class=\" where fmt-newline\" >where{where_preds},</span>" ) ?;
@@ -311,23 +318,26 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
311
318
if indent == 0 {
312
319
format ! ( "\n <span class=\" where\" >where{where_preds}</span>" )
313
320
} else {
314
- // put the first one on the same line as the 'where' keyword
315
- let where_preds = where_preds. replacen ( & br_with_padding, " " , 1 ) ;
316
-
317
- write ! ( br_with_padding, "<span class=\" where\" >where{where_preds}</span>" ) ?;
318
- br_with_padding
321
+ let padding_str: String =
322
+ iter:: once ( "\n " ) . chain ( iter:: repeat ( " " ) . take ( padding_amount) ) . collect ( ) ;
323
+ format ! ( "\n {padding_str}<span class=\" where\" >where{where_preds}</span>" )
319
324
}
320
325
} ;
321
326
write ! ( f, "{clause}" )
322
327
} )
323
328
}
324
329
325
- fn print_where_pred < ' a , ' tcx : ' a > (
330
+ fn print_where_pred_helper < ' a , ' tcx : ' a > (
326
331
cx : & ' a Context < ' tcx > ,
327
332
pred : & ' a clean:: WherePredicate ,
333
+ indent_len : usize ,
334
+ add_newline : bool ,
328
335
) -> impl Display + Captures < ' a > + Captures < ' tcx > {
329
336
display_fn ( move |f| {
330
- f. write_str ( "\n " ) ?;
337
+ if add_newline {
338
+ f. write_str ( "\n " ) ?;
339
+ }
340
+ f. write_str ( & " " . repeat ( indent_len) ) ?;
331
341
332
342
match pred {
333
343
clean:: WherePredicate :: BoundPredicate { ty, bounds, bound_params } => {
0 commit comments