@@ -22,17 +22,19 @@ pub(crate) struct ListFormatting<'a> {
22
22
trailing_separator : SeparatorTactic ,
23
23
separator_place : SeparatorPlace ,
24
24
shape : Shape ,
25
- // Non-expressions, e.g., items, will have a new line at the end of the list.
26
- // Important for comment styles.
25
+ /// Non-expressions, e.g., items, will have a new line at the end of the list.
26
+ /// Important for comment styles.
27
27
ends_with_newline : bool ,
28
- // Remove newlines between list elements for expressions.
28
+ /// Remove newlines between list elements for expressions.
29
29
preserve_newline : bool ,
30
- // Nested import lists get some special handling for the "Mixed" list type
30
+ /// Nested import lists get some special handling for the "Mixed" list type.
31
31
nested : bool ,
32
- // Whether comments should be visually aligned.
32
+ /// Whether comments should be visually aligned.
33
33
align_comments : bool ,
34
34
config : & ' a Config ,
35
- item_on_newline : Vec < bool > ,
35
+ /// The decision of putting an item on a newline is determined by the caller.
36
+ custom_list_tactic : Vec < bool > ,
37
+ /// Whether whitespaces should be added around the separator.
36
38
padding : bool ,
37
39
}
38
40
@@ -49,7 +51,7 @@ impl<'a> ListFormatting<'a> {
49
51
nested : false ,
50
52
align_comments : true ,
51
53
config,
52
- item_on_newline : Vec :: new ( ) ,
54
+ custom_list_tactic : Vec :: new ( ) ,
53
55
padding : true ,
54
56
}
55
57
}
@@ -59,8 +61,8 @@ impl<'a> ListFormatting<'a> {
59
61
self
60
62
}
61
63
62
- pub ( crate ) fn item_on_newline ( mut self , item_on_newline : Vec < bool > ) -> Self {
63
- self . item_on_newline = item_on_newline ;
64
+ pub ( crate ) fn custom_list_tactic ( mut self , custom_list_tactic : Vec < bool > ) -> Self {
65
+ self . custom_list_tactic = custom_list_tactic ;
64
66
self
65
67
}
66
68
@@ -325,6 +327,19 @@ where
325
327
}
326
328
327
329
match tactic {
330
+ _ if !formatting. custom_list_tactic . is_empty ( ) => {
331
+ if * formatting
332
+ . custom_list_tactic
333
+ . get ( i)
334
+ . expect ( "invalid custom_list_tactic formatting option" )
335
+ {
336
+ result. push ( '\n' ) ;
337
+ result. push_str ( indent_str) ;
338
+ first_item_on_line = true ;
339
+ } else if formatting. padding && !first_item_on_line {
340
+ result. push ( ' ' ) ;
341
+ }
342
+ }
328
343
DefinitiveListTactic :: Horizontal if !first && formatting. padding => {
329
344
result. push ( ' ' ) ;
330
345
}
@@ -351,13 +366,10 @@ where
351
366
let total_width = total_item_width ( item) + item_sep_len;
352
367
353
368
// 1 is space between separator and item.
354
- if ( !formatting. item_on_newline . is_empty ( ) && formatting. item_on_newline [ i] )
355
- || formatting. item_on_newline . is_empty ( )
356
- && ( ( line_len > 0 && line_len + 1 + total_width > formatting. shape . width )
357
- || prev_item_had_post_comment
358
- || ( formatting. nested
359
- && ( prev_item_is_nested_import
360
- || ( !first && inner_item. contains ( "::" ) ) ) ) )
369
+ if ( line_len > 0 && line_len + 1 + total_width > formatting. shape . width )
370
+ || prev_item_had_post_comment
371
+ || ( formatting. nested
372
+ && ( prev_item_is_nested_import || ( !first && inner_item. contains ( "::" ) ) ) )
361
373
{
362
374
result. push ( '\n' ) ;
363
375
result. push_str ( indent_str) ;
@@ -950,7 +962,7 @@ pub(crate) fn struct_lit_formatting<'a>(
950
962
nested : false ,
951
963
align_comments : true ,
952
964
config : context. config ,
953
- item_on_newline : Vec :: new ( ) ,
965
+ custom_list_tactic : Vec :: new ( ) ,
954
966
padding : true ,
955
967
}
956
968
}
0 commit comments