@@ -41,22 +41,38 @@ pub struct UnsafetySpace(pub hir::Unsafety);
41
41
/// with a space after it.
42
42
#[ derive( Copy , Clone ) ]
43
43
pub struct ConstnessSpace ( pub hir:: Constness ) ;
44
- /// Wrapper struct for properly emitting a method declaration.
45
- pub struct Method < ' a > ( pub & ' a clean:: FnDecl , pub usize ) ;
46
44
/// Similar to VisSpace, but used for mutability
47
45
#[ derive( Copy , Clone ) ]
48
46
pub struct MutableSpace ( pub clean:: Mutability ) ;
49
47
/// Similar to VisSpace, but used for mutability
50
48
#[ derive( Copy , Clone ) ]
51
49
pub struct RawMutableSpace ( pub clean:: Mutability ) ;
52
- /// Wrapper struct for emitting a where clause from Generics.
53
- pub struct WhereClause < ' a > ( pub & ' a clean:: Generics , pub usize ) ;
54
50
/// Wrapper struct for emitting type parameter bounds.
55
51
pub struct TyParamBounds < ' a > ( pub & ' a [ clean:: TyParamBound ] ) ;
56
52
/// Wrapper struct for emitting a comma-separated list of items
57
53
pub struct CommaSep < ' a , T : ' a > ( pub & ' a [ T ] ) ;
58
54
pub struct AbiSpace ( pub Abi ) ;
59
55
56
+ /// Wrapper struct for properly emitting a method declaration.
57
+ pub struct Method < ' a > {
58
+ /// The declaration to emit.
59
+ pub decl : & ' a clean:: FnDecl ,
60
+ /// The length of the function's "name", used to determine line-wrapping.
61
+ pub name_len : usize ,
62
+ /// The number of spaces to indent each successive line with, if line-wrapping is necessary.
63
+ pub indent : usize ,
64
+ }
65
+
66
+ /// Wrapper struct for emitting a where clause from Generics.
67
+ pub struct WhereClause < ' a > {
68
+ /// The Generics from which to emit a where clause.
69
+ pub gens : & ' a clean:: Generics ,
70
+ /// The number of spaces to indent each line with.
71
+ pub indent : usize ,
72
+ /// Whether the where clause needs to add a comma and newline after the last bound.
73
+ pub end_newline : bool ,
74
+ }
75
+
60
76
pub struct HRef < ' a > {
61
77
pub did : DefId ,
62
78
pub text : & ' a str ,
@@ -167,24 +183,27 @@ impl fmt::Display for clean::Generics {
167
183
168
184
impl < ' a > fmt:: Display for WhereClause < ' a > {
169
185
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
170
- let & WhereClause ( gens, pad ) = self ;
186
+ let & WhereClause { gens, indent , end_newline } = self ;
171
187
if gens. where_predicates . is_empty ( ) {
172
188
return Ok ( ( ) ) ;
173
189
}
174
190
let mut clause = String :: new ( ) ;
175
191
if f. alternate ( ) {
176
- clause. push_str ( " where " ) ;
192
+ clause. push_str ( " where" ) ;
177
193
} else {
178
- clause. push_str ( " <span class=\" where fmt-newline\" >where " ) ;
194
+ if end_newline {
195
+ clause. push_str ( " <span class=\" where fmt-newline\" >where" ) ;
196
+ } else {
197
+ clause. push_str ( " <span class=\" where\" >where" ) ;
198
+ }
179
199
}
180
200
for ( i, pred) in gens. where_predicates . iter ( ) . enumerate ( ) {
181
- if i > 0 {
182
- if f. alternate ( ) {
183
- clause. push_str ( ", " ) ;
184
- } else {
185
- clause. push_str ( ",<br>" ) ;
186
- }
201
+ if f. alternate ( ) {
202
+ clause. push ( ' ' ) ;
203
+ } else {
204
+ clause. push_str ( "<br>" ) ;
187
205
}
206
+
188
207
match pred {
189
208
& clean:: WherePredicate :: BoundPredicate { ref ty, ref bounds } => {
190
209
let bounds = bounds;
@@ -213,21 +232,29 @@ impl<'a> fmt::Display for WhereClause<'a> {
213
232
}
214
233
}
215
234
}
235
+
236
+ if i < gens. where_predicates . len ( ) - 1 || end_newline {
237
+ clause. push ( ',' ) ;
238
+ }
216
239
}
240
+
241
+ if end_newline {
242
+ //add a space so stripping <br> tags and breaking spaces still renders properly
243
+ if f. alternate ( ) {
244
+ clause. push ( ' ' ) ;
245
+ } else {
246
+ clause. push_str ( " " ) ;
247
+ }
248
+ }
249
+
217
250
if !f. alternate ( ) {
218
251
clause. push_str ( "</span>" ) ;
219
- let plain = format ! ( "{:#}" , self ) ;
220
- if plain. len ( ) + pad > 80 {
221
- // break it onto its own line regardless, but make sure method impls and trait
222
- // blocks keep their fixed padding (2 and 9, respectively)
223
- let padding = if pad > 10 {
224
- repeat ( " " ) . take ( 8 ) . collect :: < String > ( )
225
- } else {
226
- repeat ( " " ) . take ( pad + 6 ) . collect :: < String > ( )
227
- } ;
228
- clause = clause. replace ( "<br>" , & format ! ( "<br>{}" , padding) ) ;
229
- } else {
230
- clause = clause. replace ( "<br>" , " " ) ;
252
+ let padding = repeat ( " " ) . take ( indent + 4 ) . collect :: < String > ( ) ;
253
+ clause = clause. replace ( "<br>" , & format ! ( "<br>{}" , padding) ) ;
254
+ clause. insert_str ( 0 , & repeat ( " " ) . take ( indent. saturating_sub ( 1 ) )
255
+ . collect :: < String > ( ) ) ;
256
+ if !end_newline {
257
+ clause. insert_str ( 0 , "<br>" ) ;
231
258
}
232
259
}
233
260
write ! ( f, "{}" , clause)
@@ -838,43 +865,35 @@ fn fmt_impl(i: &clean::Impl,
838
865
f : & mut fmt:: Formatter ,
839
866
link_trait : bool ,
840
867
use_absolute : bool ) -> fmt:: Result {
841
- let mut plain = String :: new ( ) ;
842
-
843
868
if f. alternate ( ) {
844
869
write ! ( f, "impl{:#} " , i. generics) ?;
845
870
} else {
846
871
write ! ( f, "impl{} " , i. generics) ?;
847
872
}
848
- plain. push_str ( & format ! ( "impl{:#} " , i. generics) ) ;
849
873
850
874
if let Some ( ref ty) = i. trait_ {
851
875
if i. polarity == Some ( clean:: ImplPolarity :: Negative ) {
852
876
write ! ( f, "!" ) ?;
853
- plain. push_str ( "!" ) ;
854
877
}
855
878
856
879
if link_trait {
857
880
fmt:: Display :: fmt ( ty, f) ?;
858
- plain. push_str ( & format ! ( "{:#}" , ty) ) ;
859
881
} else {
860
882
match * ty {
861
883
clean:: ResolvedPath { typarams : None , ref path, is_generic : false , .. } => {
862
884
let last = path. segments . last ( ) . unwrap ( ) ;
863
885
fmt:: Display :: fmt ( & last. name , f) ?;
864
886
fmt:: Display :: fmt ( & last. params , f) ?;
865
- plain. push_str ( & format ! ( "{:#}{:#}" , last. name, last. params) ) ;
866
887
}
867
888
_ => unreachable ! ( ) ,
868
889
}
869
890
}
870
891
write ! ( f, " for " ) ?;
871
- plain. push_str ( " for " ) ;
872
892
}
873
893
874
894
fmt_type ( & i. for_ , f, use_absolute, true ) ?;
875
- plain. push_str ( & format ! ( "{:#}" , i. for_) ) ;
876
895
877
- fmt:: Display :: fmt ( & WhereClause ( & i. generics , plain . len ( ) + 1 ) , f) ?;
896
+ fmt:: Display :: fmt ( & WhereClause { gens : & i. generics , indent : 0 , end_newline : true } , f) ?;
878
897
Ok ( ( ) )
879
898
}
880
899
@@ -939,12 +958,15 @@ impl fmt::Display for clean::FnDecl {
939
958
940
959
impl < ' a > fmt:: Display for Method < ' a > {
941
960
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
942
- let decl = self . 0 ;
943
- let indent = self . 1 ;
961
+ let & Method { decl, name_len, indent } = self ;
944
962
let amp = if f. alternate ( ) { "&" } else { "&" } ;
945
963
let mut args = String :: new ( ) ;
946
964
let mut args_plain = String :: new ( ) ;
947
965
for ( i, input) in decl. inputs . values . iter ( ) . enumerate ( ) {
966
+ if i == 0 {
967
+ args. push_str ( "<br>" ) ;
968
+ }
969
+
948
970
if let Some ( selfty) = input. to_self ( ) {
949
971
match selfty {
950
972
clean:: SelfValue => {
@@ -970,7 +992,7 @@ impl<'a> fmt::Display for Method<'a> {
970
992
}
971
993
} else {
972
994
if i > 0 {
973
- args. push_str ( "<br> " ) ;
995
+ args. push_str ( " <br>" ) ;
974
996
args_plain. push_str ( " " ) ;
975
997
}
976
998
if !input. name . is_empty ( ) {
@@ -986,8 +1008,8 @@ impl<'a> fmt::Display for Method<'a> {
986
1008
args_plain. push_str ( & format ! ( "{:#}" , input. type_) ) ;
987
1009
}
988
1010
if i + 1 < decl. inputs . values . len ( ) {
989
- args. push_str ( "," ) ;
990
- args_plain. push_str ( "," ) ;
1011
+ args. push ( ',' ) ;
1012
+ args_plain. push ( ',' ) ;
991
1013
}
992
1014
}
993
1015
@@ -1003,27 +1025,23 @@ impl<'a> fmt::Display for Method<'a> {
1003
1025
format ! ( "{}" , decl. output)
1004
1026
} ;
1005
1027
1006
- let mut output: String ;
1007
- let plain: String ;
1008
- let pad = repeat ( " " ) . take ( indent) . collect :: < String > ( ) ;
1009
- if arrow. is_empty ( ) {
1010
- output = format ! ( "({})" , args) ;
1011
- plain = format ! ( "{}({})" , pad, args_plain) ;
1028
+ let pad = repeat ( " " ) . take ( name_len) . collect :: < String > ( ) ;
1029
+ let plain = format ! ( "{pad}({args}){arrow}" ,
1030
+ pad = pad,
1031
+ args = args_plain,
1032
+ arrow = arrow_plain) ;
1033
+
1034
+ let output = if plain. len ( ) > 80 {
1035
+ let full_pad = format ! ( "<br>{}" , repeat( " " ) . take( indent + 4 ) . collect:: <String >( ) ) ;
1036
+ let close_pad = format ! ( "<br>{}" , repeat( " " ) . take( indent) . collect:: <String >( ) ) ;
1037
+ format ! ( "({args}{close}){arrow}" ,
1038
+ args = args. replace( "<br>" , & full_pad) ,
1039
+ close = close_pad,
1040
+ arrow = arrow)
1012
1041
} else {
1013
- output = format ! ( "({args})<br>{arrow}" , args = args, arrow = arrow) ;
1014
- plain = format ! ( "{pad}({args}){arrow}" ,
1015
- pad = pad,
1016
- args = args_plain,
1017
- arrow = arrow_plain) ;
1018
- }
1042
+ format ! ( "({args}){arrow}" , args = args. replace( "<br>" , "" ) , arrow = arrow)
1043
+ } ;
1019
1044
1020
- if plain. len ( ) > 80 {
1021
- let pad = repeat ( " " ) . take ( indent) . collect :: < String > ( ) ;
1022
- let pad = format ! ( "<br>{}" , pad) ;
1023
- output = output. replace ( "<br>" , & pad) ;
1024
- } else {
1025
- output = output. replace ( "<br>" , "" ) ;
1026
- }
1027
1045
if f. alternate ( ) {
1028
1046
write ! ( f, "{}" , output. replace( "<br>" , "\n " ) )
1029
1047
} else {
0 commit comments