rustdoc: hide variants of enums > 5

This commit is contained in:
Manish Goregaokar 2021-03-20 19:02:25 -07:00
parent c96f86de30
commit 71c52acf32
3 changed files with 34 additions and 7 deletions

View file

@ -131,6 +131,23 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer)
}
}
/// For large structs, enums, unions, etc, determine whether to hide their fields
fn should_hide_fields(n_fields: usize) -> bool {
// todo: figure out what this should be
n_fields > 5
}
fn toggle_open(w: &mut Buffer, text: &str)
{
write!(w, "<div class=\"docblock type-contents-toggle\" data-toggle-text=\"{}\">", text);
}
fn toggle_close(w: &mut Buffer)
{
w.write_str("</div>");
}
fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) {
document(w, cx, item, None);
@ -816,6 +833,10 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
w.write_str(" {}");
} else {
w.write_str(" {\n");
let toggle = should_hide_fields(e.variants.len());
if toggle {
toggle_open(w, "variants");
}
for v in &e.variants {
w.write_str(" ");
let name = v.name.as_ref().unwrap();
@ -844,6 +865,9 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
if e.variants_stripped {
w.write_str(" // some variants omitted\n");
}
if toggle {
toggle_close(w);
}
w.write_str("}");
}
w.write_str("</pre>")

View file

@ -2703,6 +2703,10 @@ function hideThemeButtonState() {
}
});
}
} else if (hasClass(e, "type-contents-toggle")) {
let text = e.getAttribute("data-toggle-text");
let tog = createToggle(toggle, `Show ${text}`, null, "", true);
e.parentNode.insertBefore(tog, e);
}
if (e.parentNode.id === "main") {
var otherMessage = "";

View file

@ -970,6 +970,10 @@ a.test-arrow:hover{
position: absolute;
left: -23px;
top: 0;
/* The click event for this is defined on the document,
so bubbling does not work. See https://github.com/rust-lang/rust/issues/83332 */
z-index: 10;
}
h3 > .collapse-toggle, h4 > .collapse-toggle {
@ -1054,10 +1058,9 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
margin-top: 3px;
}
/* for enum and struct fields */
.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock {
margin-left: 30px;
margin-bottom: 20px;
margin-top: 5px;
margin-left: 0px;
}
.docblock > .section-header:first-child {
@ -1069,10 +1072,6 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
left: -10px;
}
.enum > .collapsed, .struct > .collapsed {
margin-bottom: 25px;
}
#main > .variant, #main > .structfield {
display: block;
}