Skip to content

Commit ffbc40f

Browse files
committed
rustdoc: show count of item contents when hidden
1 parent a84d1b2 commit ffbc40f

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/librustdoc/html/render/print_item.rs

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clean::AttributesExt;
22

33
use std::cmp::Ordering;
4+
use std::fmt;
45

56
use rustc_data_structures::fx::FxHashMap;
67
use rustc_hir as hir;
@@ -155,7 +156,7 @@ fn should_hide_fields(n_fields: usize) -> bool {
155156
n_fields > 12
156157
}
157158

158-
fn toggle_open(w: &mut Buffer, text: &str) {
159+
fn toggle_open(w: &mut Buffer, text: impl fmt::Display) {
159160
write!(
160161
w,
161162
"<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
481482
let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
482483
let required = t.items.iter().filter(|m| m.is_ty_method()).collect::<Vec<_>>();
483484
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();
484488

485489
// Output the trait definition
486490
wrap_into_docblock(w, |w| {
@@ -511,9 +515,12 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
511515
let mut toggle = false;
512516

513517
// If there are too many associated types, hide _everything_
514-
if should_hide_fields(types.len()) {
518+
if should_hide_fields(count_types) {
515519
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+
);
517524
}
518525
for t in &types {
519526
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
523530
// We also do this if the types + consts is large because otherwise we could
524531
// render a bunch of types and _then_ a bunch of consts just because both were
525532
// _just_ under the limit
526-
if !toggle && should_hide_fields(types.len() + consts.len()) {
533+
if !toggle && should_hide_fields(count_types + count_consts) {
527534
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+
);
529545
}
530546
if !types.is_empty() && !consts.is_empty() {
531547
w.write_str("\n");
@@ -534,9 +550,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
534550
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
535551
w.write_str(";\n");
536552
}
537-
if !toggle && should_hide_fields(required.len() + provided.len()) {
553+
if !toggle && should_hide_fields(count_methods) {
538554
toggle = true;
539-
toggle_open(w, "methods");
555+
toggle_open(w, format_args!("{} methods", count_methods));
540556
}
541557
if !consts.is_empty() && !required.is_empty() {
542558
w.write_str("\n");
@@ -924,9 +940,10 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
924940
w.write_str(" {}");
925941
} else {
926942
w.write_str(" {\n");
927-
let toggle = should_hide_fields(e.variants.len());
943+
let count_variants = e.variants.len();
944+
let toggle = should_hide_fields(count_variants);
928945
if toggle {
929-
toggle_open(w, "variants");
946+
toggle_open(w, format_args!("{} variants", count_variants));
930947
}
931948
for v in &e.variants {
932949
w.write_str(" ");
@@ -1003,7 +1020,8 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
10031020

10041021
use crate::clean::Variant;
10051022
if let clean::VariantItem(Variant::Struct(ref s)) = *variant.kind {
1006-
toggle_open(w, "fields");
1023+
let count_fields = s.fields.len();
1024+
toggle_open(w, format_args!("{} field{}", count_fields, pluralize(count_fields)));
10071025
let variant_id = cx.derive_id(format!(
10081026
"{}.{}.fields",
10091027
ItemType::Variant,
@@ -1376,7 +1394,7 @@ fn render_union(
13761394
fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
13771395
let toggle = should_hide_fields(count_fields);
13781396
if toggle {
1379-
toggle_open(w, "fields");
1397+
toggle_open(w, format_args!("{} fields", count_fields));
13801398
}
13811399

13821400
for field in fields {
@@ -1432,7 +1450,7 @@ fn render_struct(
14321450
let has_visible_fields = count_fields > 0;
14331451
let toggle = should_hide_fields(count_fields);
14341452
if toggle {
1435-
toggle_open(w, "fields");
1453+
toggle_open(w, format_args!("{} fields", count_fields));
14361454
}
14371455
for field in fields {
14381456
if let clean::StructFieldItem(ref ty) = *field.kind {
@@ -1609,3 +1627,7 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
16091627

16101628
writeln!(w, "</div>");
16111629
}
1630+
1631+
fn pluralize(count: usize) -> &'static str {
1632+
if count > 1 { "s" } else { "" }
1633+
}

0 commit comments

Comments
 (0)