@@ -42,15 +42,15 @@ pub struct UnsafetySpace(pub hir::Unsafety);
42
42
#[ derive( Copy , Clone ) ]
43
43
pub struct ConstnessSpace ( pub hir:: Constness ) ;
44
44
/// Wrapper struct for properly emitting a method declaration.
45
- pub struct Method < ' a > ( pub & ' a clean:: FnDecl , pub & ' a str ) ;
45
+ pub struct Method < ' a > ( pub & ' a clean:: FnDecl , pub usize ) ;
46
46
/// Similar to VisSpace, but used for mutability
47
47
#[ derive( Copy , Clone ) ]
48
48
pub struct MutableSpace ( pub clean:: Mutability ) ;
49
49
/// Similar to VisSpace, but used for mutability
50
50
#[ derive( Copy , Clone ) ]
51
51
pub struct RawMutableSpace ( pub clean:: Mutability ) ;
52
52
/// Wrapper struct for emitting a where clause from Generics.
53
- pub struct WhereClause < ' a > ( pub & ' a clean:: Generics ) ;
53
+ pub struct WhereClause < ' a > ( pub & ' a clean:: Generics , pub usize ) ;
54
54
/// Wrapper struct for emitting type parameter bounds.
55
55
pub struct TyParamBounds < ' a > ( pub & ' a [ clean:: TyParamBound ] ) ;
56
56
/// Wrapper struct for emitting a comma-separated list of items
@@ -157,52 +157,71 @@ impl fmt::Display for clean::Generics {
157
157
158
158
impl < ' a > fmt:: Display for WhereClause < ' a > {
159
159
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
160
- let & WhereClause ( gens) = self ;
160
+ let & WhereClause ( gens, pad ) = self ;
161
161
if gens. where_predicates . is_empty ( ) {
162
162
return Ok ( ( ) ) ;
163
163
}
164
+ let mut clause = String :: new ( ) ;
164
165
if f. alternate ( ) {
165
- f . write_str ( " " ) ? ;
166
+ clause . push_str ( " where " ) ;
166
167
} else {
167
- f . write_str ( " <span class='where'>where " ) ? ;
168
+ clause . push_str ( " <span class='where'>where " ) ;
168
169
}
169
170
for ( i, pred) in gens. where_predicates . iter ( ) . enumerate ( ) {
170
171
if i > 0 {
171
- f. write_str ( ", " ) ?;
172
+ if f. alternate ( ) {
173
+ clause. push_str ( ", " ) ;
174
+ } else {
175
+ clause. push_str ( ",<br>" ) ;
176
+ }
172
177
}
173
178
match pred {
174
179
& clean:: WherePredicate :: BoundPredicate { ref ty, ref bounds } => {
175
180
let bounds = bounds;
176
181
if f. alternate ( ) {
177
- write ! ( f , "{:#}: {:#}" , ty, TyParamBounds ( bounds) ) ? ;
182
+ clause . push_str ( & format ! ( "{:#}: {:#}" , ty, TyParamBounds ( bounds) ) ) ;
178
183
} else {
179
- write ! ( f , "{}: {}" , ty, TyParamBounds ( bounds) ) ? ;
184
+ clause . push_str ( & format ! ( "{}: {}" , ty, TyParamBounds ( bounds) ) ) ;
180
185
}
181
186
}
182
187
& clean:: WherePredicate :: RegionPredicate { ref lifetime,
183
188
ref bounds } => {
184
- write ! ( f , "{}: " , lifetime) ? ;
189
+ clause . push_str ( & format ! ( "{}: " , lifetime) ) ;
185
190
for ( i, lifetime) in bounds. iter ( ) . enumerate ( ) {
186
191
if i > 0 {
187
- f . write_str ( " + " ) ? ;
192
+ clause . push_str ( " + " ) ;
188
193
}
189
194
190
- write ! ( f , "{}" , lifetime) ? ;
195
+ clause . push_str ( & format ! ( "{}" , lifetime) ) ;
191
196
}
192
197
}
193
198
& clean:: WherePredicate :: EqPredicate { ref lhs, ref rhs } => {
194
199
if f. alternate ( ) {
195
- write ! ( f , "{:#} == {:#}" , lhs, rhs) ? ;
200
+ clause . push_str ( & format ! ( "{:#} == {:#}" , lhs, rhs) ) ;
196
201
} else {
197
- write ! ( f , "{} == {}" , lhs, rhs) ? ;
202
+ clause . push_str ( & format ! ( "{} == {}" , lhs, rhs) ) ;
198
203
}
199
204
}
200
205
}
201
206
}
202
207
if !f. alternate ( ) {
203
- f. write_str ( "</span>" ) ?;
208
+ clause. push_str ( "</span>" ) ;
209
+ let plain = format ! ( "{:#}" , self ) ;
210
+ if plain. len ( ) > 80 {
211
+ //break it onto its own line regardless, but make sure method impls and trait
212
+ //blocks keep their fixed padding (2 and 9, respectively)
213
+ let padding = if pad > 10 {
214
+ clause = clause. replace ( "class='where'" , "class='where fmt-newline'" ) ;
215
+ repeat ( " " ) . take ( 8 ) . collect :: < String > ( )
216
+ } else {
217
+ repeat ( " " ) . take ( pad + 6 ) . collect :: < String > ( )
218
+ } ;
219
+ clause = clause. replace ( "<br>" , & format ! ( "<br>{}" , padding) ) ;
220
+ } else {
221
+ clause = clause. replace ( "<br>" , " " ) ;
222
+ }
204
223
}
205
- Ok ( ( ) )
224
+ write ! ( f , "{}" , clause )
206
225
}
207
226
}
208
227
@@ -718,30 +737,43 @@ impl fmt::Display for clean::Type {
718
737
}
719
738
720
739
fn fmt_impl ( i : & clean:: Impl , f : & mut fmt:: Formatter , link_trait : bool ) -> fmt:: Result {
740
+ let mut plain = String :: new ( ) ;
741
+
721
742
if f. alternate ( ) {
722
743
write ! ( f, "impl{:#} " , i. generics) ?;
723
744
} else {
724
745
write ! ( f, "impl{} " , i. generics) ?;
725
746
}
747
+ plain. push_str ( & format ! ( "impl{:#} " , i. generics) ) ;
748
+
726
749
if let Some ( ref ty) = i. trait_ {
727
- write ! ( f, "{}" ,
728
- if i. polarity == Some ( clean:: ImplPolarity :: Negative ) { "!" } else { "" } ) ?;
750
+ if i. polarity == Some ( clean:: ImplPolarity :: Negative ) {
751
+ write ! ( f, "!" ) ?;
752
+ plain. push_str ( "!" ) ;
753
+ }
754
+
729
755
if link_trait {
730
756
fmt:: Display :: fmt ( ty, f) ?;
757
+ plain. push_str ( & format ! ( "{:#}" , ty) ) ;
731
758
} else {
732
759
match * ty {
733
760
clean:: ResolvedPath { typarams : None , ref path, is_generic : false , .. } => {
734
761
let last = path. segments . last ( ) . unwrap ( ) ;
735
762
fmt:: Display :: fmt ( & last. name , f) ?;
736
763
fmt:: Display :: fmt ( & last. params , f) ?;
764
+ plain. push_str ( & format ! ( "{:#}{:#}" , last. name, last. params) ) ;
737
765
}
738
766
_ => unreachable ! ( ) ,
739
767
}
740
768
}
741
769
write ! ( f, " for " ) ?;
770
+ plain. push_str ( " for " ) ;
742
771
}
772
+
743
773
fmt:: Display :: fmt ( & i. for_ , f) ?;
744
- fmt:: Display :: fmt ( & WhereClause ( & i. generics ) , f) ?;
774
+ plain. push_str ( & format ! ( "{:#}" , i. for_) ) ;
775
+
776
+ fmt:: Display :: fmt ( & WhereClause ( & i. generics , plain. len ( ) + 1 ) , f) ?;
745
777
Ok ( ( ) )
746
778
}
747
779
@@ -870,24 +902,30 @@ impl<'a> fmt::Display for Method<'a> {
870
902
871
903
let mut output: String ;
872
904
let plain: String ;
905
+ let pad = repeat ( " " ) . take ( indent) . collect :: < String > ( ) ;
873
906
if arrow. is_empty ( ) {
874
907
output = format ! ( "({})" , args) ;
875
- plain = format ! ( "{}({})" , indent . replace ( " " , " " ) , args_plain) ;
908
+ plain = format ! ( "{}({})" , pad , args_plain) ;
876
909
} else {
877
910
output = format ! ( "({args})<br>{arrow}" , args = args, arrow = arrow) ;
878
- plain = format ! ( "{indent }({args}){arrow}" ,
879
- indent = indent . replace ( " " , " " ) ,
911
+ plain = format ! ( "{pad }({args}){arrow}" ,
912
+ pad = pad ,
880
913
args = args_plain,
881
914
arrow = arrow_plain) ;
882
915
}
883
916
884
917
if plain. len ( ) > 80 {
885
- let pad = format ! ( "<br>{}" , indent) ;
918
+ let pad = repeat ( " " ) . take ( indent) . collect :: < String > ( ) ;
919
+ let pad = format ! ( "<br>{}" , pad) ;
886
920
output = output. replace ( "<br>" , & pad) ;
887
921
} else {
888
922
output = output. replace ( "<br>" , "" ) ;
889
923
}
890
- write ! ( f, "{}" , output)
924
+ if f. alternate ( ) {
925
+ write ! ( f, "{}" , output. replace( "<br>" , "\n " ) )
926
+ } else {
927
+ write ! ( f, "{}" , output)
928
+ }
891
929
}
892
930
}
893
931
0 commit comments