@@ -14,7 +14,7 @@ use Indent;
14
14
use utils:: { CodeMapSpanUtils , format_mutability, format_visibility, contains_skip, end_typaram,
15
15
wrap_str, last_line_width, semicolon_for_expr, format_unsafety, trim_newlines} ;
16
16
use lists:: { write_list, itemize_list, ListItem , ListFormatting , SeparatorTactic ,
17
- DefinitiveListTactic , definitive_tactic, format_item_list} ;
17
+ DefinitiveListTactic , ListTactic , definitive_tactic, format_item_list} ;
18
18
use expr:: { is_empty_block, is_simple_block_stmt, rewrite_assign_rhs} ;
19
19
use comment:: { FindUncommented , contains_comment} ;
20
20
use visitor:: FmtVisitor ;
@@ -419,7 +419,8 @@ impl<'a> FmtVisitor<'a> {
419
419
& field. node . data ,
420
420
None ,
421
421
field. span ,
422
- indent)
422
+ indent,
423
+ Some ( self . config . struct_variant_width ) )
423
424
}
424
425
ast:: VariantData :: Unit ( ..) => {
425
426
let tag = if let Some ( ref expr) = field. node . disr_expr {
@@ -588,7 +589,8 @@ pub fn format_struct(context: &RewriteContext,
588
589
struct_def : & ast:: VariantData ,
589
590
generics : Option < & ast:: Generics > ,
590
591
span : Span ,
591
- offset : Indent )
592
+ offset : Indent ,
593
+ one_line_width : Option < usize > )
592
594
-> Option < String > {
593
595
match * struct_def {
594
596
ast:: VariantData :: Unit ( ..) => format_unit_struct ( item_name, ident, vis) ,
@@ -610,7 +612,8 @@ pub fn format_struct(context: &RewriteContext,
610
612
fields,
611
613
generics,
612
614
span,
613
- offset)
615
+ offset,
616
+ one_line_width)
614
617
}
615
618
}
616
619
}
@@ -758,7 +761,8 @@ fn format_struct_struct(context: &RewriteContext,
758
761
fields : & [ ast:: StructField ] ,
759
762
generics : Option < & ast:: Generics > ,
760
763
span : Span ,
761
- offset : Indent )
764
+ offset : Indent ,
765
+ one_line_width : Option < usize > )
762
766
-> Option < String > {
763
767
let mut result = String :: with_capacity ( 1024 ) ;
764
768
@@ -813,23 +817,35 @@ fn format_struct_struct(context: &RewriteContext,
813
817
|field| field. ty . span . hi ,
814
818
|field| field. rewrite ( context, item_budget, item_indent) ,
815
819
context. codemap . span_after ( span, "{" ) ,
816
- span. hi ) ;
820
+ span. hi )
821
+ . collect :: < Vec < _ > > ( ) ;
817
822
// 1 = ,
818
823
let budget = context. config . max_width - offset. width ( ) + context. config . tab_spaces - 1 ;
824
+
825
+ let tactic = match one_line_width {
826
+ Some ( w) => definitive_tactic ( & items, ListTactic :: LimitedHorizontalVertical ( w) , budget) ,
827
+ None => DefinitiveListTactic :: Vertical ,
828
+ } ;
829
+
819
830
let fmt = ListFormatting {
820
- tactic : DefinitiveListTactic :: Vertical ,
831
+ tactic : tactic ,
821
832
separator : "," ,
822
833
trailing_separator : context. config . struct_trailing_comma ,
823
834
indent : item_indent,
824
835
width : budget,
825
836
ends_with_newline : true ,
826
837
config : context. config ,
827
838
} ;
828
- Some ( format ! ( "{}\n {}{}\n {}}}" ,
829
- result,
830
- offset. block_indent( context. config) . to_string( context. config) ,
831
- try_opt!( write_list( items, & fmt) ) ,
832
- offset. to_string( context. config) ) )
839
+ let items_str = try_opt ! ( write_list( & items, & fmt) ) ;
840
+ if one_line_width. is_some ( ) && !items_str. contains ( '\n' ) {
841
+ Some ( format ! ( "{} {} }}" , result, items_str) )
842
+ } else {
843
+ Some ( format ! ( "{}\n {}{}\n {}}}" ,
844
+ result,
845
+ offset. block_indent( context. config) . to_string( context. config) ,
846
+ items_str,
847
+ offset. to_string( context. config) ) )
848
+ }
833
849
}
834
850
835
851
fn format_tuple_struct ( context : & RewriteContext ,
0 commit comments