@@ -23,13 +23,14 @@ use codemap::{LineRangeUtils, SpanUtils};
23
23
use comment:: { combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
24
24
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented } ;
25
25
use config:: { BraceStyle , Config , Density , IndentStyle } ;
26
- use expr:: { format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, ExprType } ;
26
+ use expr:: { format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
27
+ rewrite_assign_rhs_with, ExprType , RhsTactics } ;
27
28
use lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , ListItem , Separator } ;
28
29
use rewrite:: { Rewrite , RewriteContext } ;
29
30
use overflow;
30
31
use shape:: { Indent , Shape } ;
31
32
use spanned:: Spanned ;
32
- use types:: join_bounds ;
33
+ use types:: TraitTyParamBounds ;
33
34
use utils:: { colon_spaces, contains_skip, first_line_width, format_abi, format_constness,
34
35
format_defaultness, format_mutability, format_unsafety, format_visibility,
35
36
is_attributes_extendable, last_line_contains_single_line_comment,
@@ -919,60 +920,55 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
919
920
return None ;
920
921
}
921
922
}
922
- let trait_bound_str = rewrite_trait_bounds (
923
- context,
924
- type_param_bounds,
925
- Shape :: indented ( offset, context. config ) ,
926
- ) ?;
927
- // If the trait, generics, and trait bound cannot fit on the same line,
928
- // put the trait bounds on an indented new line
929
- if offset. width ( ) + last_line_width ( & result) + trait_bound_str. len ( )
930
- > context. config . comment_width ( )
931
- {
932
- let trait_indent = offset. block_only ( ) . block_indent ( context. config ) ;
933
- result. push_str ( & trait_indent. to_string_with_newline ( context. config ) ) ;
923
+ if !type_param_bounds. is_empty ( ) {
924
+ result = rewrite_assign_rhs_with (
925
+ context,
926
+ result + ":" ,
927
+ & TraitTyParamBounds :: new ( type_param_bounds) ,
928
+ shape,
929
+ RhsTactics :: ForceNextLine ,
930
+ ) ?;
934
931
}
935
- result. push_str ( & trait_bound_str) ;
936
932
937
- let where_density =
938
- if context. config . indent_style ( ) == IndentStyle :: Block && result. is_empty ( ) {
933
+ // Rewrite where clause.
934
+ if !generics. where_clause . predicates . is_empty ( ) {
935
+ let where_density = if context. config . indent_style ( ) == IndentStyle :: Block {
939
936
Density :: Compressed
940
937
} else {
941
938
Density :: Tall
942
939
} ;
943
940
944
- let where_budget = context. budget ( last_line_width ( & result) ) ;
945
- let pos_before_where = if type_param_bounds. is_empty ( ) {
946
- generics. where_clause . span . lo ( )
941
+ let where_budget = context. budget ( last_line_width ( & result) ) ;
942
+ let pos_before_where = if type_param_bounds. is_empty ( ) {
943
+ generics. where_clause . span . lo ( )
944
+ } else {
945
+ type_param_bounds[ type_param_bounds. len ( ) - 1 ] . span ( ) . hi ( )
946
+ } ;
947
+ let option = WhereClauseOption :: snuggled ( & generics_str) ;
948
+ let where_clause_str = rewrite_where_clause (
949
+ context,
950
+ & generics. where_clause ,
951
+ context. config . brace_style ( ) ,
952
+ Shape :: legacy ( where_budget, offset. block_only ( ) ) ,
953
+ where_density,
954
+ "{" ,
955
+ None ,
956
+ pos_before_where,
957
+ option,
958
+ false ,
959
+ ) ?;
960
+ // If the where clause cannot fit on the same line,
961
+ // put the where clause on a new line
962
+ if !where_clause_str. contains ( '\n' )
963
+ && last_line_width ( & result) + where_clause_str. len ( ) + offset. width ( )
964
+ > context. config . comment_width ( )
965
+ {
966
+ let width = offset. block_indent + context. config . tab_spaces ( ) - 1 ;
967
+ let where_indent = Indent :: new ( 0 , width) ;
968
+ result. push_str ( & where_indent. to_string_with_newline ( context. config ) ) ;
969
+ }
970
+ result. push_str ( & where_clause_str) ;
947
971
} else {
948
- type_param_bounds[ type_param_bounds. len ( ) - 1 ] . span ( ) . hi ( )
949
- } ;
950
- let option = WhereClauseOption :: snuggled ( & generics_str) ;
951
- let where_clause_str = rewrite_where_clause (
952
- context,
953
- & generics. where_clause ,
954
- context. config . brace_style ( ) ,
955
- Shape :: legacy ( where_budget, offset. block_only ( ) ) ,
956
- where_density,
957
- "{" ,
958
- None ,
959
- pos_before_where,
960
- option,
961
- false ,
962
- ) ?;
963
- // If the where clause cannot fit on the same line,
964
- // put the where clause on a new line
965
- if !where_clause_str. contains ( '\n' )
966
- && last_line_width ( & result) + where_clause_str. len ( ) + offset. width ( )
967
- > context. config . comment_width ( )
968
- {
969
- let width = offset. block_indent + context. config . tab_spaces ( ) - 1 ;
970
- let where_indent = Indent :: new ( 0 , width) ;
971
- result. push_str ( & where_indent. to_string_with_newline ( context. config ) ) ;
972
- }
973
- result. push_str ( & where_clause_str) ;
974
-
975
- if generics. where_clause . predicates . is_empty ( ) {
976
972
let item_snippet = context. snippet ( item. span ) ;
977
973
if let Some ( lo) = item_snippet. chars ( ) . position ( |c| c == '/' ) {
978
974
// 1 = `{`
@@ -995,16 +991,18 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
995
991
}
996
992
997
993
match context. config . brace_style ( ) {
998
- _ if last_line_contains_single_line_comment ( & result) => {
994
+ _ if last_line_contains_single_line_comment ( & result)
995
+ || last_line_width ( & result) + 2 > context. budget ( offset. width ( ) ) =>
996
+ {
999
997
result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1000
998
}
1001
999
BraceStyle :: AlwaysNextLine => {
1002
1000
result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1003
1001
}
1004
1002
BraceStyle :: PreferSameLine => result. push ( ' ' ) ,
1005
1003
BraceStyle :: SameLineWhere => {
1006
- if !where_clause_str . is_empty ( )
1007
- && ( !trait_items . is_empty ( ) || result . contains ( '\n' ) )
1004
+ if result . contains ( '\n' )
1005
+ || ( !generics . where_clause . predicates . is_empty ( ) && !trait_items . is_empty ( ) )
1008
1006
{
1009
1007
result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1010
1008
} else {
@@ -1585,16 +1583,12 @@ pub fn rewrite_associated_type(
1585
1583
let prefix = format ! ( "type {}" , ident) ;
1586
1584
1587
1585
let type_bounds_str = if let Some ( bounds) = ty_param_bounds_opt {
1588
- // 2 = ": ".len()
1589
- let shape = Shape :: indented ( indent, context. config ) . offset_left ( prefix. len ( ) + 2 ) ?;
1590
- let bound_str = bounds
1591
- . iter ( )
1592
- . map ( |ty_bound| ty_bound. rewrite ( context, shape) )
1593
- . collect :: < Option < Vec < _ > > > ( ) ?;
1594
- if !bounds. is_empty ( ) {
1595
- format ! ( ": {}" , join_bounds( context, shape, & bound_str) )
1596
- } else {
1586
+ if bounds. is_empty ( ) {
1597
1587
String :: new ( )
1588
+ } else {
1589
+ // 2 = ": ".len()
1590
+ let shape = Shape :: indented ( indent, context. config ) . offset_left ( prefix. len ( ) + 2 ) ?;
1591
+ bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1598
1592
}
1599
1593
} else {
1600
1594
String :: new ( )
@@ -2329,21 +2323,6 @@ pub fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize)
2329
2323
}
2330
2324
}
2331
2325
2332
- fn rewrite_trait_bounds (
2333
- context : & RewriteContext ,
2334
- bounds : & [ ast:: TyParamBound ] ,
2335
- shape : Shape ,
2336
- ) -> Option < String > {
2337
- if bounds. is_empty ( ) {
2338
- return Some ( String :: new ( ) ) ;
2339
- }
2340
- let bound_str = bounds
2341
- . iter ( )
2342
- . map ( |ty_bound| ty_bound. rewrite ( context, shape) )
2343
- . collect :: < Option < Vec < _ > > > ( ) ?;
2344
- Some ( format ! ( ": {}" , join_bounds( context, shape, & bound_str) ) )
2345
- }
2346
-
2347
2326
fn rewrite_where_clause_rfc_style (
2348
2327
context : & RewriteContext ,
2349
2328
where_clause : & ast:: WhereClause ,
0 commit comments