Skip to content

Commit 7a19fba

Browse files
authored
Rollup merge of #87024 - weihanglo:issue-85494, r=Manishearth
rustdoc: show count of item contents when hidden Resolves #85494
2 parents a3e1259 + 2b518ac commit 7a19fba

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
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");
@@ -933,9 +949,10 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
933949
w.write_str(" {}");
934950
} else {
935951
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);
937954
if toggle {
938-
toggle_open(w, "variants");
955+
toggle_open(w, format_args!("{} variants", count_variants));
939956
}
940957
for v in &e.variants {
941958
w.write_str(" ");
@@ -1012,7 +1029,8 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
10121029

10131030
use crate::clean::Variant;
10141031
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)));
10161034
let variant_id = cx.derive_id(format!(
10171035
"{}.{}.fields",
10181036
ItemType::Variant,
@@ -1385,7 +1403,7 @@ fn render_union(
13851403
fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
13861404
let toggle = should_hide_fields(count_fields);
13871405
if toggle {
1388-
toggle_open(w, "fields");
1406+
toggle_open(w, format_args!("{} fields", count_fields));
13891407
}
13901408

13911409
for field in fields {
@@ -1441,7 +1459,7 @@ fn render_struct(
14411459
let has_visible_fields = count_fields > 0;
14421460
let toggle = should_hide_fields(count_fields);
14431461
if toggle {
1444-
toggle_open(w, "fields");
1462+
toggle_open(w, format_args!("{} fields", count_fields));
14451463
}
14461464
for field in fields {
14471465
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) {
16181636

16191637
writeln!(w, "</div>");
16201638
}
1639+
1640+
fn pluralize(count: usize) -> &'static str {
1641+
if count > 1 { "s" } else { "" }
1642+
}

src/test/rustdoc/toggle-item-contents.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct PubStruct {
99

1010
// @has 'toggle_item_contents/struct.BigPubStruct.html'
1111
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
12-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
12+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 fields'
1313
pub struct BigPubStruct {
1414
pub a: usize,
1515
pub b: usize,
@@ -28,7 +28,7 @@ pub struct BigPubStruct {
2828

2929
// @has 'toggle_item_contents/union.BigUnion.html'
3030
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
31-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
31+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 fields'
3232
pub union BigUnion {
3333
pub a: usize,
3434
pub b: usize,
@@ -63,7 +63,7 @@ pub struct PrivStruct {
6363

6464
// @has 'toggle_item_contents/enum.Enum.html'
6565
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
66-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
66+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 2 fields'
6767
pub enum Enum {
6868
A, B, C,
6969
D {
@@ -72,9 +72,19 @@ pub enum Enum {
7272
}
7373
}
7474

75+
// @has 'toggle_item_contents/enum.EnumStructVariant.html'
76+
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
77+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 1 field'
78+
pub enum EnumStructVariant {
79+
A, B, C,
80+
D {
81+
a: u8,
82+
}
83+
}
84+
7585
// @has 'toggle_item_contents/enum.LargeEnum.html'
7686
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
77-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show variants'
87+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
7888
pub enum LargeEnum {
7989
A, B, C, D, E, F(u8), G, H, I, J, K, L, M
8090
}
@@ -90,7 +100,7 @@ pub trait Trait {
90100

91101
// @has 'toggle_item_contents/trait.GinormousTrait.html'
92102
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
93-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show associated items'
103+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 16 associated items'
94104
pub trait GinormousTrait {
95105
type A;
96106
type B;
@@ -113,7 +123,7 @@ pub trait GinormousTrait {
113123

114124
// @has 'toggle_item_contents/trait.HugeTrait.html'
115125
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
116-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show associated constants and methods'
126+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 12 associated constants and 2 methods'
117127
pub trait HugeTrait {
118128
type A;
119129
const M: usize = 1;
@@ -133,9 +143,30 @@ pub trait HugeTrait {
133143
fn bar();
134144
}
135145

146+
// @has 'toggle_item_contents/trait.GiganticTrait.html'
147+
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
148+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 1 associated constant and 1 method'
149+
pub trait GiganticTrait {
150+
type A;
151+
type B;
152+
type C;
153+
type D;
154+
type E;
155+
type F;
156+
type G;
157+
type H;
158+
type I;
159+
type J;
160+
type K;
161+
type L;
162+
const M: usize = 1;
163+
#[must_use]
164+
fn foo();
165+
}
166+
136167
// @has 'toggle_item_contents/trait.BigTrait.html'
137168
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
138-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show methods'
169+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 14 methods'
139170
pub trait BigTrait {
140171
type A;
141172
#[must_use]

0 commit comments

Comments
 (0)