Skip to content

Commit 173cbec

Browse files
committed
rustdoc: smartly hide associated items of traits if there are too many of them
1 parent f146b97 commit 173cbec

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,17 +443,36 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
443443
} else {
444444
// FIXME: we should be using a derived_id for the Anchors here
445445
w.write_str("{\n");
446+
let mut toggle = false;
447+
448+
// If there are too many associated types, hide _everything_
449+
if should_hide_fields(types.len()) {
450+
toggle = true;
451+
toggle_open(w, "associated items");
452+
}
446453
for t in &types {
447454
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
448455
w.write_str(";\n");
449456
}
457+
// If there are too many associated constants, hide everything after them
458+
// We also do this if the types + consts is large because otherwise we could
459+
// render a bunch of types and _then_ a bunch of consts just because both were
460+
// _just_ under the limit
461+
if !toggle & should_hide_fields(types.len() + consts.len()) {
462+
toggle = true;
463+
toggle_open(w, "associated constants and methods");
464+
}
450465
if !types.is_empty() && !consts.is_empty() {
451466
w.write_str("\n");
452467
}
453468
for t in &consts {
454469
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
455470
w.write_str(";\n");
456471
}
472+
if !toggle & should_hide_fields(required.len() + provided.len()) {
473+
toggle = true;
474+
toggle_open(w, "methods");
475+
}
457476
if !consts.is_empty() && !required.is_empty() {
458477
w.write_str("\n");
459478
}
@@ -484,6 +503,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
484503
w.write_str("<div class=\"item-spacer\"></div>");
485504
}
486505
}
506+
if toggle {
507+
toggle_close(w);
508+
}
487509
w.write_str("}");
488510
}
489511
w.write_str("</pre>")
@@ -1284,9 +1306,7 @@ fn render_union(
12841306
write!(w, " {{\n{}", tab);
12851307
let count_fields = fields
12861308
.iter()
1287-
.filter(
1288-
|f| if let clean::StructFieldItem(..) = *f.kind { true } else { false },
1289-
)
1309+
.filter(|f| if let clean::StructFieldItem(..) = *f.kind { true } else { false })
12901310
.count();
12911311
let toggle = should_hide_fields(count_fields);
12921312
if toggle {
@@ -1343,9 +1363,7 @@ fn render_struct(
13431363
w.write_str(" {");
13441364
let count_fields = fields
13451365
.iter()
1346-
.filter(
1347-
|f| if let clean::StructFieldItem(..) = *f.kind { true } else { false },
1348-
)
1366+
.filter(|f| if let clean::StructFieldItem(..) = *f.kind { true } else { false })
13491367
.count();
13501368
let has_visible_fields = count_fields > 0;
13511369
let toggle = should_hide_fields(count_fields);

src/librustdoc/html/static/rustdoc.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,8 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
10581058
margin-top: 3px;
10591059
}
10601060

1061-
/* for enum and struct fields */
1062-
.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock, .union > .toggle-wrapper + .docblock {
1061+
/* for hiding fields/variants/associated items */
1062+
.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock, .union > .toggle-wrapper + .docblock, .trait > .toggle-wrapper + .docblock {
10631063
margin-left: 0px;
10641064
}
10651065

@@ -1785,7 +1785,7 @@ div.name.expand::before {
17851785
.type-decl > pre > .docblock.attributes.top-attr {
17861786
margin-left: 1.8em !important;
17871787
}
1788-
.type-decl > pre > .toggle-attributes {
1788+
.type-decl > pre .toggle-attributes {
17891789
margin-left: 2.2em;
17901790
}
17911791
.type-decl > pre > .docblock.attributes {

src/librustdoc/html/static/themes/ayu.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ a {
218218
color: #c5c5c5;
219219
}
220220

221-
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
221+
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow):not(.collapse-toggle),
222222
.docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
223223
#help a {
224224
color: #39AFD7;

src/librustdoc/html/static/themes/dark.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ a {
176176
color: #ddd;
177177
}
178178

179-
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
179+
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow):not(.collapse-toggle),
180180
.docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
181181
#help a {
182182
color: #D2991D;

src/librustdoc/html/static/themes/light.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ a {
174174
color: #000;
175175
}
176176

177-
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
177+
.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow):not(.collapse-toggle),
178178
.docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
179179
#help a {
180180
color: #3873AD;

0 commit comments

Comments
 (0)