Skip to content

Commit

Permalink
rustdoc: hide variants of enums > 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 12, 2021
1 parent c96f86d commit 71c52ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/librustdoc/html/render/print_item.rs
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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>")
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/html/static/main.js
Expand Up @@ -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 = "";
Expand Down
13 changes: 6 additions & 7 deletions src/librustdoc/html/static/rustdoc.css
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit 71c52ac

Please sign in to comment.