@@ -71,7 +71,10 @@ use macros::convert_try_mac;
71
71
use rewrite:: { Rewrite , RewriteContext } ;
72
72
use shape:: Shape ;
73
73
use spanned:: Spanned ;
74
- use utils:: { first_line_width, last_line_extendable, last_line_width, mk_sp, wrap_str} ;
74
+ use utils:: {
75
+ first_line_width, last_line_extendable, last_line_width, mk_sp, trimmed_last_line_width,
76
+ wrap_str,
77
+ } ;
75
78
76
79
use std:: borrow:: Cow ;
77
80
use std:: cmp:: min;
@@ -395,10 +398,11 @@ impl<'a> ChainFormatterShared<'a> {
395
398
let last = & self . children [ 0 ] ;
396
399
let extendable =
397
400
may_extend && last_line_extendable ( & self . rewrites [ self . rewrites . len ( ) - 1 ] ) ;
401
+ let prev_last_line_width = last_line_width ( & self . rewrites [ self . rewrites . len ( ) - 1 ] ) ;
398
402
399
403
// Total of all items excluding the last.
400
404
let almost_total = if extendable {
401
- last_line_width ( & self . rewrites [ self . rewrites . len ( ) - 1 ] )
405
+ prev_last_line_width
402
406
} else {
403
407
self . rewrites . iter ( ) . fold ( 0 , |a, b| a + b. len ( ) )
404
408
} + last. tries ;
@@ -410,7 +414,7 @@ impl<'a> ChainFormatterShared<'a> {
410
414
411
415
let all_in_one_line =
412
416
self . rewrites . iter ( ) . all ( |s| !s. contains ( '\n' ) ) && one_line_budget > 0 ;
413
- let last_shape = if all_in_one_line {
417
+ let last_shape = if all_in_one_line || extendable {
414
418
shape. sub_width ( last. tries ) ?
415
419
} else {
416
420
child_shape. sub_width ( shape. rhs_overhead ( context. config ) + last. tries ) ?
@@ -508,6 +512,37 @@ impl<'a> ChainFormatterBlock<'a> {
508
512
is_block_like : Vec :: with_capacity ( chain. children . len ( ) + 1 ) ,
509
513
}
510
514
}
515
+
516
+ // States whether an expression's last line exclusively consists of closing
517
+ // parens, braces, and brackets in its idiomatic formatting.
518
+ fn is_block_expr ( context : & RewriteContext , expr : & ast:: Expr , repr : & str ) -> bool {
519
+ match expr. node {
520
+ ast:: ExprKind :: Mac ( ..)
521
+ | ast:: ExprKind :: Call ( ..)
522
+ | ast:: ExprKind :: MethodCall ( ..)
523
+ | ast:: ExprKind :: Struct ( ..)
524
+ | ast:: ExprKind :: While ( ..)
525
+ | ast:: ExprKind :: WhileLet ( ..)
526
+ | ast:: ExprKind :: If ( ..)
527
+ | ast:: ExprKind :: IfLet ( ..)
528
+ | ast:: ExprKind :: Block ( ..)
529
+ | ast:: ExprKind :: Loop ( ..)
530
+ | ast:: ExprKind :: ForLoop ( ..)
531
+ | ast:: ExprKind :: Match ( ..) => repr. contains ( '\n' ) ,
532
+ ast:: ExprKind :: Paren ( ref expr)
533
+ | ast:: ExprKind :: Binary ( _, _, ref expr)
534
+ | ast:: ExprKind :: Index ( _, ref expr)
535
+ | ast:: ExprKind :: Unary ( _, ref expr)
536
+ | ast:: ExprKind :: Closure ( _, _, _, _, ref expr, _)
537
+ | ast:: ExprKind :: Try ( ref expr)
538
+ | ast:: ExprKind :: Yield ( Some ( ref expr) ) => Self :: is_block_expr ( context, expr, repr) ,
539
+ // This can only be a string lit
540
+ ast:: ExprKind :: Lit ( _) => {
541
+ repr. contains ( '\n' ) && trimmed_last_line_width ( repr) <= context. config . tab_spaces ( )
542
+ }
543
+ _ => false ,
544
+ }
545
+ }
511
546
}
512
547
513
548
impl < ' a > ChainFormatter for ChainFormatterBlock < ' a > {
@@ -519,7 +554,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
519
554
) -> Option < ( ) > {
520
555
let mut root_rewrite: String = parent. rewrite ( context, shape) ?;
521
556
522
- let mut root_ends_with_block = is_block_expr ( context, & parent. expr , & root_rewrite) ;
557
+ let mut root_ends_with_block = Self :: is_block_expr ( context, & parent. expr , & root_rewrite) ;
523
558
let tab_width = context. config . tab_spaces ( ) . saturating_sub ( shape. offset ) ;
524
559
525
560
while root_rewrite. len ( ) <= tab_width && !root_rewrite. contains ( '\n' ) {
@@ -530,7 +565,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
530
565
None => break ,
531
566
}
532
567
533
- root_ends_with_block = is_block_expr ( context, & item. expr , & root_rewrite) ;
568
+ root_ends_with_block = Self :: is_block_expr ( context, & item. expr , & root_rewrite) ;
534
569
535
570
self . shared . children = & self . shared . children [ ..self . shared . children . len ( ) - 1 ] ;
536
571
if self . shared . children . is_empty ( ) {
@@ -554,7 +589,7 @@ impl<'a> ChainFormatter for ChainFormatterBlock<'a> {
554
589
for item in self . shared . children [ 1 ..] . iter ( ) . rev ( ) {
555
590
let rewrite = item. rewrite_postfix ( context, child_shape) ?;
556
591
self . is_block_like
557
- . push ( is_block_expr ( context, & item. expr , & rewrite) ) ;
592
+ . push ( Self :: is_block_expr ( context, & item. expr , & rewrite) ) ;
558
593
self . shared . rewrites . push ( rewrite) ;
559
594
}
560
595
Some ( ( ) )
@@ -608,12 +643,7 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
608
643
}
609
644
}
610
645
611
- // Parent is the first item in the chain, e.g., `foo` in `foo.bar.baz()`.
612
- let parent_shape = if is_block_expr ( context, & parent. expr , "\n " ) {
613
- shape. visual_indent ( 0 )
614
- } else {
615
- shape
616
- } ;
646
+ let parent_shape = shape. visual_indent ( 0 ) ;
617
647
let mut root_rewrite = parent. rewrite ( context, parent_shape) ?;
618
648
619
649
if !root_rewrite. contains ( '\n' ) && is_continuable ( & parent. expr ) {
@@ -661,30 +691,3 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
661
691
self . shared . pure_root ( )
662
692
}
663
693
}
664
-
665
- // States whether an expression's last line exclusively consists of closing
666
- // parens, braces, and brackets in its idiomatic formatting.
667
- fn is_block_expr ( context : & RewriteContext , expr : & ast:: Expr , repr : & str ) -> bool {
668
- match expr. node {
669
- ast:: ExprKind :: Mac ( ..) | ast:: ExprKind :: Call ( ..) | ast:: ExprKind :: MethodCall ( ..) => {
670
- context. use_block_indent ( ) && repr. contains ( '\n' )
671
- }
672
- ast:: ExprKind :: Struct ( ..)
673
- | ast:: ExprKind :: While ( ..)
674
- | ast:: ExprKind :: WhileLet ( ..)
675
- | ast:: ExprKind :: If ( ..)
676
- | ast:: ExprKind :: IfLet ( ..)
677
- | ast:: ExprKind :: Block ( ..)
678
- | ast:: ExprKind :: Loop ( ..)
679
- | ast:: ExprKind :: ForLoop ( ..)
680
- | ast:: ExprKind :: Match ( ..) => repr. contains ( '\n' ) ,
681
- ast:: ExprKind :: Paren ( ref expr)
682
- | ast:: ExprKind :: Binary ( _, _, ref expr)
683
- | ast:: ExprKind :: Index ( _, ref expr)
684
- | ast:: ExprKind :: Unary ( _, ref expr)
685
- | ast:: ExprKind :: Closure ( _, _, _, _, ref expr, _)
686
- | ast:: ExprKind :: Try ( ref expr)
687
- | ast:: ExprKind :: Yield ( Some ( ref expr) ) => is_block_expr ( context, expr, repr) ,
688
- _ => false ,
689
- }
690
- }
0 commit comments