1
1
use clean:: AttributesExt ;
2
2
3
3
use std:: cmp:: Ordering ;
4
+ use std:: fmt;
4
5
5
6
use rustc_data_structures:: fx:: FxHashMap ;
6
7
use rustc_hir as hir;
@@ -155,7 +156,7 @@ fn should_hide_fields(n_fields: usize) -> bool {
155
156
n_fields > 12
156
157
}
157
158
158
- fn toggle_open ( w : & mut Buffer , text : & str ) {
159
+ fn toggle_open ( w : & mut Buffer , text : impl fmt :: Display ) {
159
160
write ! (
160
161
w,
161
162
"<details class=\" rustdoc-toggle type-contents-toggle\" >\
@@ -481,6 +482,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
481
482
let consts = t. items . iter ( ) . filter ( |m| m. is_associated_const ( ) ) . collect :: < Vec < _ > > ( ) ;
482
483
let required = t. items . iter ( ) . filter ( |m| m. is_ty_method ( ) ) . collect :: < Vec < _ > > ( ) ;
483
484
let provided = t. items . iter ( ) . filter ( |m| m. is_method ( ) ) . collect :: < Vec < _ > > ( ) ;
485
+ let count_types = types. len ( ) ;
486
+ let count_consts = consts. len ( ) ;
487
+ let count_methods = required. len ( ) + provided. len ( ) ;
484
488
485
489
// Output the trait definition
486
490
wrap_into_docblock ( w, |w| {
@@ -511,9 +515,12 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
511
515
let mut toggle = false ;
512
516
513
517
// If there are too many associated types, hide _everything_
514
- if should_hide_fields ( types . len ( ) ) {
518
+ if should_hide_fields ( count_types ) {
515
519
toggle = true ;
516
- toggle_open ( w, "associated items" ) ;
520
+ toggle_open (
521
+ w,
522
+ format_args ! ( "{} associated items" , count_types + count_consts + count_methods) ,
523
+ ) ;
517
524
}
518
525
for t in & types {
519
526
render_assoc_item ( w, t, AssocItemLink :: Anchor ( None ) , ItemType :: Trait , cx) ;
@@ -523,9 +530,18 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
523
530
// We also do this if the types + consts is large because otherwise we could
524
531
// render a bunch of types and _then_ a bunch of consts just because both were
525
532
// _just_ under the limit
526
- if !toggle && should_hide_fields ( types . len ( ) + consts . len ( ) ) {
533
+ if !toggle && should_hide_fields ( count_types + count_consts ) {
527
534
toggle = true ;
528
- toggle_open ( w, "associated constants and methods" ) ;
535
+ toggle_open (
536
+ w,
537
+ format_args ! (
538
+ "{} associated constant{} and {} method{}" ,
539
+ count_consts,
540
+ pluralize( count_consts) ,
541
+ count_methods,
542
+ pluralize( count_methods) ,
543
+ ) ,
544
+ ) ;
529
545
}
530
546
if !types. is_empty ( ) && !consts. is_empty ( ) {
531
547
w. write_str ( "\n " ) ;
@@ -534,9 +550,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
534
550
render_assoc_item ( w, t, AssocItemLink :: Anchor ( None ) , ItemType :: Trait , cx) ;
535
551
w. write_str ( ";\n " ) ;
536
552
}
537
- if !toggle && should_hide_fields ( required . len ( ) + provided . len ( ) ) {
553
+ if !toggle && should_hide_fields ( count_methods ) {
538
554
toggle = true ;
539
- toggle_open ( w, " methods") ;
555
+ toggle_open ( w, format_args ! ( "{} methods", count_methods ) ) ;
540
556
}
541
557
if !consts. is_empty ( ) && !required. is_empty ( ) {
542
558
w. write_str ( "\n " ) ;
@@ -933,9 +949,10 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
933
949
w. write_str ( " {}" ) ;
934
950
} else {
935
951
w. write_str ( " {\n " ) ;
936
- let toggle = should_hide_fields ( e. variants . len ( ) ) ;
952
+ let count_variants = e. variants . len ( ) ;
953
+ let toggle = should_hide_fields ( count_variants) ;
937
954
if toggle {
938
- toggle_open ( w, " variants") ;
955
+ toggle_open ( w, format_args ! ( "{} variants", count_variants ) ) ;
939
956
}
940
957
for v in & e. variants {
941
958
w. write_str ( " " ) ;
@@ -1012,7 +1029,8 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
1012
1029
1013
1030
use crate :: clean:: Variant ;
1014
1031
if let clean:: VariantItem ( Variant :: Struct ( ref s) ) = * variant. kind {
1015
- toggle_open ( w, "fields" ) ;
1032
+ let count_fields = s. fields . len ( ) ;
1033
+ toggle_open ( w, format_args ! ( "{} field{}" , count_fields, pluralize( count_fields) ) ) ;
1016
1034
let variant_id = cx. derive_id ( format ! (
1017
1035
"{}.{}.fields" ,
1018
1036
ItemType :: Variant ,
@@ -1385,7 +1403,7 @@ fn render_union(
1385
1403
fields. iter ( ) . filter ( |f| matches ! ( * f. kind, clean:: StructFieldItem ( ..) ) ) . count ( ) ;
1386
1404
let toggle = should_hide_fields ( count_fields) ;
1387
1405
if toggle {
1388
- toggle_open ( w, " fields") ;
1406
+ toggle_open ( w, format_args ! ( "{} fields", count_fields ) ) ;
1389
1407
}
1390
1408
1391
1409
for field in fields {
@@ -1441,7 +1459,7 @@ fn render_struct(
1441
1459
let has_visible_fields = count_fields > 0 ;
1442
1460
let toggle = should_hide_fields ( count_fields) ;
1443
1461
if toggle {
1444
- toggle_open ( w, " fields") ;
1462
+ toggle_open ( w, format_args ! ( "{} fields", count_fields ) ) ;
1445
1463
}
1446
1464
for field in fields {
1447
1465
if let clean:: StructFieldItem ( ref ty) = * field. kind {
@@ -1618,3 +1636,7 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1618
1636
1619
1637
writeln ! ( w, "</div>" ) ;
1620
1638
}
1639
+
1640
+ fn pluralize ( count : usize ) -> & ' static str {
1641
+ if count > 1 { "s" } else { "" }
1642
+ }
0 commit comments