@@ -23,7 +23,7 @@ use string::{StringFormat, rewrite_string};
23
23
use utils:: { CodeMapSpanUtils , extra_offset, last_line_width, wrap_str, binary_search,
24
24
first_line_width, semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr} ;
25
25
use visitor:: FmtVisitor ;
26
- use config:: { Config , StructLitStyle , MultilineStyle } ;
26
+ use config:: { Config , StructLitStyle , MultilineStyle , ElseIfBraceStyle , ControlBraceStyle } ;
27
27
use comment:: { FindUncommented , rewrite_comment, contains_comment, recover_comment_removed} ;
28
28
use types:: rewrite_path;
29
29
use items:: { span_lo_for_arg, span_hi_for_arg} ;
@@ -648,14 +648,20 @@ impl<'a> Rewrite for Loop<'a> {
648
648
None => String :: new ( ) ,
649
649
} ;
650
650
651
+ let alt_block_sep = String :: from ( "\n " ) + & context. block_indent . to_string ( context. config ) ;
652
+ let block_sep = match context. config . control_brace_style {
653
+ ControlBraceStyle :: AlwaysNextLine => alt_block_sep. as_str ( ) ,
654
+ ControlBraceStyle :: AlwaysSameLine => " " ,
655
+ } ;
651
656
// FIXME: this drops any comment between "loop" and the block.
652
657
self . block
653
658
. rewrite ( context, width, offset)
654
659
. map ( |result| {
655
- format ! ( "{}{}{} {}" ,
660
+ format ! ( "{}{}{}{} {}" ,
656
661
label_string,
657
662
self . keyword,
658
663
pat_expr_string,
664
+ block_sep,
659
665
result)
660
666
} )
661
667
}
@@ -701,12 +707,16 @@ fn rewrite_if_else(context: &RewriteContext,
701
707
allow_single_line : bool )
702
708
-> Option < String > {
703
709
// 3 = "if ", 2 = " {"
710
+ let pat_penalty = match context. config . else_if_brace_style {
711
+ ElseIfBraceStyle :: AlwaysNextLine => 3 ,
712
+ _ => 3 + 2 ,
713
+ } ;
704
714
let pat_expr_string = try_opt ! ( rewrite_pat_expr( context,
705
715
pat,
706
716
cond,
707
717
"let " ,
708
718
" =" ,
709
- try_opt!( width. checked_sub( 3 + 2 ) ) ,
719
+ try_opt!( width. checked_sub( pat_penalty ) ) ,
710
720
offset + 3 ) ) ;
711
721
712
722
// Try to format if-else on single line.
@@ -731,13 +741,19 @@ fn rewrite_if_else(context: &RewriteContext,
731
741
offset,
732
742
width) ;
733
743
744
+ let alt_block_sep = String :: from ( "\n " ) + & context. block_indent . to_string ( context. config ) ;
745
+ let after_sep = match context. config . else_if_brace_style {
746
+ ElseIfBraceStyle :: AlwaysNextLine => alt_block_sep. as_str ( ) ,
747
+ _ => " " ,
748
+ } ;
734
749
let mut result = format ! ( "if{}{}{}{}" ,
735
750
between_if_cond_comment. as_ref( ) . map_or( " " , |str | & * * str ) ,
736
751
pat_expr_string,
737
- after_cond_comment. as_ref( ) . map_or( " " , |str | & * * str ) ,
752
+ after_cond_comment. as_ref( ) . map_or( after_sep , |str | & * * str ) ,
738
753
if_block_string) ;
739
754
740
755
if let Some ( else_block) = else_block_opt {
756
+ let mut last_in_chain = false ;
741
757
let rewrite = match else_block. node {
742
758
// If the else expression is another if-else expression, prevent it
743
759
// from being formatted on a single line.
@@ -763,7 +779,10 @@ fn rewrite_if_else(context: &RewriteContext,
763
779
offset,
764
780
false )
765
781
}
766
- _ => else_block. rewrite ( context, width, offset) ,
782
+ _ => {
783
+ last_in_chain = true ;
784
+ else_block. rewrite ( context, width, offset)
785
+ }
767
786
} ;
768
787
769
788
let between_if_else_block = mk_sp ( if_block. span . hi ,
@@ -781,10 +800,20 @@ fn rewrite_if_else(context: &RewriteContext,
781
800
else_block. span . lo ) ;
782
801
let after_else_comment = extract_comment ( after_else, & context, offset, width) ;
783
802
803
+ let between_sep = match context. config . else_if_brace_style {
804
+ ElseIfBraceStyle :: AlwaysNextLine |
805
+ ElseIfBraceStyle :: ClosingNextLine => alt_block_sep. as_str ( ) ,
806
+ ElseIfBraceStyle :: AlwaysSameLine => " " ,
807
+ } ;
808
+ let after_sep = match context. config . else_if_brace_style {
809
+ ElseIfBraceStyle :: AlwaysNextLine if last_in_chain => alt_block_sep. as_str ( ) ,
810
+ _ => " " ,
811
+ } ;
784
812
try_opt ! ( write!( & mut result,
785
813
"{}else{}" ,
786
- between_if_else_block_comment. as_ref( ) . map_or( " " , |str | & * * str ) ,
787
- after_else_comment. as_ref( ) . map_or( " " , |str | & * * str ) )
814
+ between_if_else_block_comment. as_ref( )
815
+ . map_or( between_sep, |str | & * * str ) ,
816
+ after_else_comment. as_ref( ) . map_or( after_sep, |str | & * * str ) )
788
817
. ok( ) ) ;
789
818
result. push_str ( & & try_opt ! ( rewrite) ) ;
790
819
}
@@ -917,7 +946,12 @@ fn rewrite_match(context: &RewriteContext,
917
946
// `match `cond` {`
918
947
let cond_budget = try_opt ! ( width. checked_sub( 8 ) ) ;
919
948
let cond_str = try_opt ! ( cond. rewrite( context, cond_budget, offset + 6 ) ) ;
920
- let mut result = format ! ( "match {} {{" , cond_str) ;
949
+ let alt_block_sep = String :: from ( "\n " ) + & context. block_indent . to_string ( context. config ) ;
950
+ let block_sep = match context. config . control_brace_style {
951
+ ControlBraceStyle :: AlwaysSameLine => " " ,
952
+ ControlBraceStyle :: AlwaysNextLine => alt_block_sep. as_str ( ) ,
953
+ } ;
954
+ let mut result = format ! ( "match {}{}{{" , cond_str, block_sep) ;
921
955
922
956
let nested_context = context. nested_context ( ) ;
923
957
let arm_indent = nested_context. block_indent ;
@@ -1076,6 +1110,7 @@ impl Rewrite for ast::Arm {
1076
1110
} ;
1077
1111
1078
1112
let comma = arm_comma ( & context. config , self , body) ;
1113
+ let alt_block_sep = String :: from ( "\n " ) + & context. block_indent . to_string ( context. config ) ;
1079
1114
1080
1115
// Let's try and get the arm body on the same line as the condition.
1081
1116
// 4 = ` => `.len()
@@ -1089,12 +1124,17 @@ impl Rewrite for ast::Arm {
1089
1124
false
1090
1125
} ;
1091
1126
1127
+ let block_sep = match context. config . control_brace_style {
1128
+ ControlBraceStyle :: AlwaysNextLine if is_block => alt_block_sep. as_str ( ) ,
1129
+ _ => " " ,
1130
+ } ;
1092
1131
match rewrite {
1093
1132
Some ( ref body_str) if !body_str. contains ( '\n' ) || !context. config . wrap_match_arms ||
1094
1133
is_block => {
1095
- return Some ( format ! ( "{}{} => {}{}" ,
1134
+ return Some ( format ! ( "{}{} =>{} {}{}" ,
1096
1135
attr_str. trim_left( ) ,
1097
1136
pats_str,
1137
+ block_sep,
1098
1138
body_str,
1099
1139
comma) ) ;
1100
1140
}
@@ -1122,10 +1162,14 @@ impl Rewrite for ast::Arm {
1122
1162
( "" , "" )
1123
1163
} ;
1124
1164
1125
- Some ( format ! ( "{}{} =>{}\n {}{}\n {}{}" ,
1165
+ let block_sep = match context. config . control_brace_style {
1166
+ ControlBraceStyle :: AlwaysNextLine => alt_block_sep,
1167
+ ControlBraceStyle :: AlwaysSameLine => String :: from ( body_prefix) + "\n " ,
1168
+ } ;
1169
+ Some ( format ! ( "{}{} =>{}{}{}\n {}{}" ,
1126
1170
attr_str. trim_left( ) ,
1127
1171
pats_str,
1128
- body_prefix ,
1172
+ block_sep ,
1129
1173
indent_str,
1130
1174
next_line_body,
1131
1175
offset. to_string( context. config) ,
0 commit comments