@@ -481,13 +481,14 @@ impl<'comments> Formatter<'comments> {
481
481
. map ( |segment| bit_array_segment ( segment, |e| self . const_expr ( e) ) )
482
482
. collect_vec ( ) ;
483
483
484
- self . bit_array (
485
- segment_docs,
486
- segments
487
- . iter ( )
488
- . all ( |s| s. value . can_have_multiple_per_line ( ) ) ,
489
- location,
490
- )
484
+ let packing = self . items_sequence_packing (
485
+ segments,
486
+ None ,
487
+ |segment| segment. value . can_have_multiple_per_line ( ) ,
488
+ * location,
489
+ ) ;
490
+
491
+ self . bit_array ( segment_docs, packing, location)
491
492
}
492
493
493
494
Constant :: Record {
@@ -586,17 +587,15 @@ impl<'comments> Formatter<'comments> {
586
587
} ;
587
588
}
588
589
589
- let list_packing = self . list_items_packing (
590
+ let list_packing = self . items_sequence_packing (
590
591
elements,
591
592
None ,
592
593
|element| element. can_have_multiple_per_line ( ) ,
593
594
* location,
594
595
) ;
595
596
let comma = match list_packing {
596
- ListItemsPacking :: FitMultiplePerLine => flex_break ( "," , ", " ) ,
597
- ListItemsPacking :: FitOnePerLine | ListItemsPacking :: BreakOnePerLine => {
598
- break_ ( "," , ", " )
599
- }
597
+ ItemsPacking :: FitMultiplePerLine => flex_break ( "," , ", " ) ,
598
+ ItemsPacking :: FitOnePerLine | ItemsPacking :: BreakOnePerLine => break_ ( "," , ", " ) ,
600
599
} ;
601
600
602
601
let mut elements_doc = nil ( ) ;
@@ -645,8 +644,8 @@ impl<'comments> Formatter<'comments> {
645
644
} ;
646
645
647
646
match list_packing {
648
- ListItemsPacking :: FitOnePerLine | ListItemsPacking :: FitMultiplePerLine => doc. group ( ) ,
649
- ListItemsPacking :: BreakOnePerLine => doc. force_break ( ) ,
647
+ ItemsPacking :: FitOnePerLine | ItemsPacking :: FitMultiplePerLine => doc. group ( ) ,
648
+ ItemsPacking :: BreakOnePerLine => doc. force_break ( ) ,
650
649
}
651
650
}
652
651
@@ -1084,13 +1083,14 @@ impl<'comments> Formatter<'comments> {
1084
1083
. map ( |segment| bit_array_segment ( segment, |e| self . bit_array_segment_expr ( e) ) )
1085
1084
. collect_vec ( ) ;
1086
1085
1087
- self . bit_array (
1088
- segment_docs,
1089
- segments
1090
- . iter ( )
1091
- . all ( |s| s. value . can_have_multiple_per_line ( ) ) ,
1092
- location,
1093
- )
1086
+ let packing = self . items_sequence_packing (
1087
+ segments,
1088
+ None ,
1089
+ |segment| segment. value . can_have_multiple_per_line ( ) ,
1090
+ * location,
1091
+ ) ;
1092
+
1093
+ self . bit_array ( segment_docs, packing, location)
1094
1094
}
1095
1095
UntypedExpr :: RecordUpdate {
1096
1096
constructor,
@@ -2028,18 +2028,16 @@ impl<'comments> Formatter<'comments> {
2028
2028
} ;
2029
2029
}
2030
2030
2031
- let list_packing = self . list_items_packing (
2031
+ let list_packing = self . items_sequence_packing (
2032
2032
elements,
2033
2033
tail,
2034
2034
UntypedExpr :: can_have_multiple_per_line,
2035
2035
* location,
2036
2036
) ;
2037
2037
2038
2038
let comma = match list_packing {
2039
- ListItemsPacking :: FitMultiplePerLine => flex_break ( "," , ", " ) ,
2040
- ListItemsPacking :: FitOnePerLine | ListItemsPacking :: BreakOnePerLine => {
2041
- break_ ( "," , ", " )
2042
- }
2039
+ ItemsPacking :: FitMultiplePerLine => flex_break ( "," , ", " ) ,
2040
+ ItemsPacking :: FitOnePerLine | ItemsPacking :: BreakOnePerLine => break_ ( "," , ", " ) ,
2043
2041
} ;
2044
2042
2045
2043
let list_size = elements. len ( )
@@ -2110,18 +2108,18 @@ impl<'comments> Formatter<'comments> {
2110
2108
} ;
2111
2109
2112
2110
match list_packing {
2113
- ListItemsPacking :: FitOnePerLine | ListItemsPacking :: FitMultiplePerLine => doc. group ( ) ,
2114
- ListItemsPacking :: BreakOnePerLine => doc. force_break ( ) ,
2111
+ ItemsPacking :: FitOnePerLine | ItemsPacking :: FitMultiplePerLine => doc. group ( ) ,
2112
+ ItemsPacking :: BreakOnePerLine => doc. force_break ( ) ,
2115
2113
}
2116
2114
}
2117
2115
2118
- fn list_items_packing < ' a , T : HasLocation > (
2116
+ fn items_sequence_packing < ' a , T : HasLocation > (
2119
2117
& self ,
2120
2118
items : & ' a [ T ] ,
2121
2119
tail : Option < & ' a T > ,
2122
2120
can_have_multiple_per_line : impl Fn ( & ' a T ) -> bool ,
2123
2121
list_location : SrcSpan ,
2124
- ) -> ListItemsPacking {
2122
+ ) -> ItemsPacking {
2125
2123
let ends_with_trailing_comma = tail
2126
2124
. map ( |tail| tail. location ( ) . end )
2127
2125
. or_else ( || items. last ( ) . map ( |last| last. location ( ) . end ) )
@@ -2143,12 +2141,12 @@ impl<'comments> Formatter<'comments> {
2143
2141
// If there's any empty line between elements we want to force each
2144
2142
// item onto its own line to preserve the empty lines that were
2145
2143
// intentionally added.
2146
- ListItemsPacking :: BreakOnePerLine
2144
+ ItemsPacking :: BreakOnePerLine
2147
2145
} else if !ends_with_trailing_comma {
2148
2146
// If the list doesn't end with a trailing comma we try and pack it in
2149
2147
// a single line; if we can't we'll put one item per line, no matter
2150
2148
// the content of the list.
2151
- ListItemsPacking :: FitOnePerLine
2149
+ ItemsPacking :: FitOnePerLine
2152
2150
} else if tail. is_none ( )
2153
2151
&& items. iter ( ) . all ( can_have_multiple_per_line)
2154
2152
&& has_multiple_elements_per_line
@@ -2183,11 +2181,11 @@ impl<'comments> Formatter<'comments> {
2183
2181
// The first item is broken, meaning that once we get to the flex
2184
2182
// space separating it from the following one the formatter is not
2185
2183
// going to break it since there's enough space in the current line!
2186
- ListItemsPacking :: FitMultiplePerLine
2184
+ ItemsPacking :: FitMultiplePerLine
2187
2185
} else {
2188
2186
// If it ends with a trailing comma we will force the list on
2189
2187
// multiple lines, with one item per line.
2190
- ListItemsPacking :: BreakOnePerLine
2188
+ ItemsPacking :: BreakOnePerLine
2191
2189
}
2192
2190
}
2193
2191
@@ -2288,7 +2286,7 @@ impl<'comments> Formatter<'comments> {
2288
2286
. map ( |segment| bit_array_segment ( segment, |pattern| self . pattern ( pattern) ) )
2289
2287
. collect_vec ( ) ;
2290
2288
2291
- self . bit_array ( segment_docs, false , location)
2289
+ self . bit_array ( segment_docs, ItemsPacking :: FitOnePerLine , location)
2292
2290
}
2293
2291
2294
2292
Pattern :: StringPrefix {
@@ -2588,7 +2586,7 @@ impl<'comments> Formatter<'comments> {
2588
2586
fn bit_array < ' a > (
2589
2587
& mut self ,
2590
2588
segments : Vec < Document < ' a > > ,
2591
- can_have_multiple_per_line : bool ,
2589
+ packing : ItemsPacking ,
2592
2590
location : & SrcSpan ,
2593
2591
) -> Document < ' a > {
2594
2592
let comments = self . pop_comments ( location. end ) ;
@@ -2614,19 +2612,19 @@ impl<'comments> Formatter<'comments> {
2614
2612
. force_break ( ) ,
2615
2613
} ;
2616
2614
}
2617
- let comma = if can_have_multiple_per_line {
2618
- flex_break ( "," , ", " )
2619
- } else {
2620
- break_ ( "," , ", " )
2615
+
2616
+ let comma = match packing {
2617
+ ItemsPacking :: FitMultiplePerLine => flex_break ( "," , ", " ) ,
2618
+ ItemsPacking :: FitOnePerLine | ItemsPacking :: BreakOnePerLine => break_ ( "," , ", " ) ,
2621
2619
} ;
2622
2620
2623
2621
let last_break = break_ ( "," , "" ) ;
2624
2622
let doc = break_ ( "<<" , "<<" )
2625
2623
. append ( join ( segments, comma) )
2626
2624
. nest ( INDENT ) ;
2627
2625
2628
- match comments_doc {
2629
- None => doc. append ( last_break) . append ( ">>" ) . group ( ) ,
2626
+ let doc = match comments_doc {
2627
+ None => doc. append ( last_break) . append ( ">>" ) ,
2630
2628
Some ( comments) => doc
2631
2629
. append ( last_break. nest ( INDENT ) )
2632
2630
// ^ Notice how in this case we nest the final break before
@@ -2635,8 +2633,12 @@ impl<'comments> Formatter<'comments> {
2635
2633
. append ( comments. nest ( INDENT ) )
2636
2634
. append ( line ( ) )
2637
2635
. append ( ">>" )
2638
- . force_break ( )
2639
- . group ( ) ,
2636
+ . force_break ( ) ,
2637
+ } ;
2638
+
2639
+ match packing {
2640
+ ItemsPacking :: FitOnePerLine | ItemsPacking :: FitMultiplePerLine => doc. group ( ) ,
2641
+ ItemsPacking :: BreakOnePerLine => doc. force_break ( ) ,
2640
2642
}
2641
2643
}
2642
2644
@@ -3059,7 +3061,10 @@ impl<'a> Documentable<'a> for &'a BinOp {
3059
3061
}
3060
3062
3061
3063
#[ allow( clippy:: enum_variant_names) ]
3062
- enum ListItemsPacking {
3064
+ /// This is used to determine how to fit the items of a list, or the segments of
3065
+ /// a bit array in a line.
3066
+ ///
3067
+ enum ItemsPacking {
3063
3068
/// Try and fit everything on a single line; if the items don't fit, break
3064
3069
/// the list putting each item into its own line.
3065
3070
///
0 commit comments