@@ -18,7 +18,7 @@ use expr::rewrite_assign_rhs;
18
18
use comment:: FindUncommented ;
19
19
use visitor:: FmtVisitor ;
20
20
use rewrite:: Rewrite ;
21
- use config:: Config ;
21
+ use config:: { Config , BlockIndentStyle , Density } ;
22
22
23
23
use syntax:: { ast, abi} ;
24
24
use syntax:: codemap:: { self , Span , BytePos } ;
@@ -99,7 +99,7 @@ impl<'a> FmtVisitor<'a> {
99
99
vis : ast:: Visibility ,
100
100
span : Span )
101
101
-> String {
102
- let newline_brace = self . newline_for_brace ( & generics. where_clause ) ;
102
+ let mut newline_brace = self . newline_for_brace ( & generics. where_clause ) ;
103
103
104
104
let mut result = self . rewrite_fn_base ( indent,
105
105
ident,
@@ -113,6 +113,10 @@ impl<'a> FmtVisitor<'a> {
113
113
span,
114
114
newline_brace) ;
115
115
116
+ if self . config . fn_brace_style != BraceStyle :: AlwaysNextLine && !result. contains ( '\n' ) {
117
+ newline_brace = false ;
118
+ }
119
+
116
120
// Prepare for the function body by possibly adding a newline and
117
121
// indent.
118
122
// FIXME we'll miss anything between the end of the signature and the
@@ -196,6 +200,7 @@ impl<'a> FmtVisitor<'a> {
196
200
// Generics.
197
201
let generics_indent = indent + result. len ( ) ;
198
202
result. push_str ( & self . rewrite_generics ( generics,
203
+ indent,
199
204
generics_indent,
200
205
codemap:: mk_sp ( span. lo ,
201
206
span_for_return ( & fd. output ) . lo ) ) ) ;
@@ -237,6 +242,7 @@ impl<'a> FmtVisitor<'a> {
237
242
explicit_self,
238
243
one_line_budget,
239
244
multi_line_budget,
245
+ indent,
240
246
arg_indent,
241
247
args_span) ) ;
242
248
result. push ( ')' ) ;
@@ -279,10 +285,18 @@ impl<'a> FmtVisitor<'a> {
279
285
}
280
286
}
281
287
288
+ let where_density = if self . config . where_density == Density :: Compressed &&
289
+ !result. contains ( '\n' ) {
290
+ Density :: Compressed
291
+ } else {
292
+ Density :: Tall
293
+ } ;
294
+
282
295
// Where clause.
283
296
result. push_str ( & self . rewrite_where_clause ( where_clause,
284
297
self . config ,
285
298
indent,
299
+ where_density,
286
300
span. hi ) ) ;
287
301
288
302
result
@@ -293,6 +307,7 @@ impl<'a> FmtVisitor<'a> {
293
307
explicit_self : Option < & ast:: ExplicitSelf > ,
294
308
one_line_budget : usize ,
295
309
multi_line_budget : usize ,
310
+ indent : usize ,
296
311
arg_indent : usize ,
297
312
span : Span )
298
313
-> String {
@@ -341,11 +356,17 @@ impl<'a> FmtVisitor<'a> {
341
356
item. item = arg;
342
357
}
343
358
359
+ let indent = match self . config . fn_arg_indent {
360
+ BlockIndentStyle :: Inherit => indent,
361
+ BlockIndentStyle :: Tabbed => indent + self . config . tab_spaces ,
362
+ BlockIndentStyle :: Visual => arg_indent,
363
+ } ;
364
+
344
365
let fmt = ListFormatting {
345
- tactic : ListTactic :: HorizontalVertical ,
366
+ tactic : self . config . fn_args_layout . to_list_tactic ( ) ,
346
367
separator : "," ,
347
368
trailing_separator : SeparatorTactic :: Never ,
348
- indent : arg_indent ,
369
+ indent : indent ,
349
370
h_width : one_line_budget,
350
371
v_width : multi_line_budget,
351
372
ends_with_newline : false ,
@@ -424,6 +445,7 @@ impl<'a> FmtVisitor<'a> {
424
445
let body_start = span. lo + BytePos ( enum_snippet. find_uncommented ( "{" ) . unwrap ( ) as u32 + 1 ) ;
425
446
let generics_str = self . format_generics ( generics,
426
447
" {" ,
448
+ self . block_indent ,
427
449
self . block_indent + self . config . tab_spaces ,
428
450
codemap:: mk_sp ( span. lo ,
429
451
body_start) ) ;
@@ -565,6 +587,7 @@ impl<'a> FmtVisitor<'a> {
565
587
let generics_str = match generics {
566
588
Some ( g) => self . format_generics ( g,
567
589
opener,
590
+ offset,
568
591
offset + header_str. len ( ) ,
569
592
codemap:: mk_sp ( span. lo ,
570
593
struct_def. fields [ 0 ] . span . lo ) ) ,
@@ -662,14 +685,16 @@ impl<'a> FmtVisitor<'a> {
662
685
generics : & ast:: Generics ,
663
686
opener : & str ,
664
687
offset : usize ,
688
+ generics_offset : usize ,
665
689
span : Span )
666
690
-> String {
667
- let mut result = self . rewrite_generics ( generics, offset, span) ;
691
+ let mut result = self . rewrite_generics ( generics, offset, generics_offset , span) ;
668
692
669
693
if !generics. where_clause . predicates . is_empty ( ) || result. contains ( '\n' ) {
670
694
result. push_str ( & self . rewrite_where_clause ( & generics. where_clause ,
671
695
self . config ,
672
696
self . block_indent ,
697
+ Density :: Tall ,
673
698
span. hi ) ) ;
674
699
result. push_str ( & make_indent ( self . block_indent ) ) ;
675
700
result. push ( '\n' ) ;
@@ -714,7 +739,12 @@ impl<'a> FmtVisitor<'a> {
714
739
}
715
740
}
716
741
717
- fn rewrite_generics ( & self , generics : & ast:: Generics , offset : usize , span : Span ) -> String {
742
+ fn rewrite_generics ( & self ,
743
+ generics : & ast:: Generics ,
744
+ offset : usize ,
745
+ generics_offset : usize ,
746
+ span : Span )
747
+ -> String {
718
748
// FIXME convert bounds to where clauses where they get too big or if
719
749
// there is a where clause at all.
720
750
let lifetimes: & [ _ ] = & generics. lifetimes ;
@@ -723,18 +753,24 @@ impl<'a> FmtVisitor<'a> {
723
753
return String :: new ( ) ;
724
754
}
725
755
726
- let budget = self . config . max_width - offset - 2 ;
756
+ let offset = match self . config . generics_indent {
757
+ BlockIndentStyle :: Inherit => offset,
758
+ BlockIndentStyle :: Tabbed => offset + self . config . tab_spaces ,
759
+ // 1 = <
760
+ BlockIndentStyle :: Visual => generics_offset + 1 ,
761
+ } ;
762
+
763
+ let h_budget = self . config . max_width - generics_offset - 2 ;
727
764
// TODO might need to insert a newline if the generics are really long
728
765
729
766
// Strings for the generics.
730
- // 1 = <
731
767
let context = self . get_context ( ) ;
732
768
// FIXME: don't unwrap
733
769
let lt_strs = lifetimes. iter ( ) . map ( |lt| {
734
- lt. rewrite ( & context, budget , offset + 1 ) . unwrap ( )
770
+ lt. rewrite ( & context, h_budget , offset) . unwrap ( )
735
771
} ) ;
736
772
let ty_strs = tys. iter ( ) . map ( |ty_param| {
737
- ty_param. rewrite ( & context, budget , offset + 1 ) . unwrap ( )
773
+ ty_param. rewrite ( & context, h_budget , offset) . unwrap ( )
738
774
} ) ;
739
775
740
776
// Extract comments between generics.
@@ -762,7 +798,7 @@ impl<'a> FmtVisitor<'a> {
762
798
item. item = ty;
763
799
}
764
800
765
- let fmt = ListFormatting :: for_fn ( budget , offset + 1 ) ;
801
+ let fmt = ListFormatting :: for_fn ( h_budget , offset) ;
766
802
767
803
format ! ( "<{}>" , write_list( & items, & fmt) )
768
804
}
@@ -771,15 +807,29 @@ impl<'a> FmtVisitor<'a> {
771
807
where_clause : & ast:: WhereClause ,
772
808
config : & Config ,
773
809
indent : usize ,
810
+ density : Density ,
774
811
span_end : BytePos )
775
812
-> String {
776
813
if where_clause. predicates . is_empty ( ) {
777
814
return String :: new ( ) ;
778
815
}
779
816
817
+ let extra_indent = match self . config . where_indent {
818
+ BlockIndentStyle :: Inherit => 0 ,
819
+ BlockIndentStyle :: Tabbed | BlockIndentStyle :: Visual => config. tab_spaces ,
820
+ } ;
821
+
780
822
let context = self . get_context ( ) ;
781
- // 6 = "where ".len()
782
- let offset = indent + config. tab_spaces + 6 ;
823
+
824
+ let offset = match self . config . where_pred_indent {
825
+ BlockIndentStyle :: Inherit => indent + extra_indent,
826
+ BlockIndentStyle :: Tabbed => indent + extra_indent + config. tab_spaces ,
827
+ // 6 = "where ".len()
828
+ BlockIndentStyle :: Visual => indent + extra_indent + 6 ,
829
+ } ;
830
+ // FIXME: if where_pred_indent != Visual, then the budgets below might
831
+ // be out by a char or two.
832
+
783
833
let budget = self . config . ideal_width + self . config . leeway - offset;
784
834
let span_start = span_for_where_pred ( & where_clause. predicates [ 0 ] ) . lo ;
785
835
let items = itemize_list ( self . codemap ,
@@ -795,18 +845,25 @@ impl<'a> FmtVisitor<'a> {
795
845
span_end) ;
796
846
797
847
let fmt = ListFormatting {
798
- tactic : ListTactic :: Vertical ,
848
+ tactic : self . config . where_layout ,
799
849
separator : "," ,
800
850
trailing_separator : SeparatorTactic :: Never ,
801
851
indent : offset,
802
852
h_width : budget,
803
853
v_width : budget,
804
854
ends_with_newline : true ,
805
855
} ;
806
-
807
- format ! ( "\n {}where {}" ,
808
- make_indent( indent + config. tab_spaces) ,
809
- write_list( & items. collect:: <Vec <_>>( ) , & fmt) )
856
+ let preds_str = write_list ( & items. collect :: < Vec < _ > > ( ) , & fmt) ;
857
+
858
+ // 9 = " where ".len() + " {".len()
859
+ if density == Density :: Tall || preds_str. contains ( '\n' ) ||
860
+ indent + 9 + preds_str. len ( ) > self . config . max_width {
861
+ format ! ( "\n {}where {}" ,
862
+ make_indent( indent + extra_indent) ,
863
+ preds_str)
864
+ } else {
865
+ format ! ( " where {}" , preds_str)
866
+ }
810
867
}
811
868
812
869
fn rewrite_return ( & self , ret : & ast:: FunctionRetTy ) -> String {
0 commit comments