Skip to content

Commit

Permalink
rustdoc: hide fields of structs/unions > 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 12, 2021
1 parent 71c52ac commit f146b97
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
37 changes: 30 additions & 7 deletions src/librustdoc/html/render/print_item.rs
Expand Up @@ -137,17 +137,14 @@ fn should_hide_fields(n_fields: usize) -> bool {
n_fields > 5
}

fn toggle_open(w: &mut Buffer, text: &str)
{
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)
{
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 @@ -1285,6 +1282,17 @@ fn render_union(
}

write!(w, " {{\n{}", tab);
let count_fields = fields
.iter()
.filter(
|f| if let clean::StructFieldItem(..) = *f.kind { true } else { false },
)
.count();
let toggle = should_hide_fields(count_fields);
if toggle {
toggle_open(w, "fields");
}

for field in fields {
if let clean::StructFieldItem(ref ty) = *field.kind {
write!(
Expand All @@ -1301,6 +1309,9 @@ fn render_union(
if it.has_stripped_fields().unwrap() {
write!(w, " // some fields omitted\n{}", tab);
}
if toggle {
toggle_close(w);
}
w.write_str("}");
}

Expand Down Expand Up @@ -1329,8 +1340,18 @@ fn render_struct(
if let Some(g) = g {
write!(w, "{}", print_where_clause(g, cx.cache(), cx.tcx(), 0, true),)
}
let mut has_visible_fields = false;
w.write_str(" {");
let count_fields = fields
.iter()
.filter(
|f| if let clean::StructFieldItem(..) = *f.kind { true } else { false },
)
.count();
let has_visible_fields = count_fields > 0;
let toggle = should_hide_fields(count_fields);
if toggle {
toggle_open(w, "fields");
}
for field in fields {
if let clean::StructFieldItem(ref ty) = *field.kind {
write!(
Expand All @@ -1341,7 +1362,6 @@ fn render_struct(
field.name.as_ref().unwrap(),
ty.print(cx.cache(), cx.tcx()),
);
has_visible_fields = true;
}
}

Expand All @@ -1355,6 +1375,9 @@ fn render_struct(
// `{ /* fields omitted */ }` to save space.
write!(w, " /* fields omitted */ ");
}
if toggle {
toggle_close(w);
}
w.write_str("}");
}
CtorKind::Fn => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/rustdoc.css
Expand Up @@ -1059,7 +1059,7 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
}

/* for enum and struct fields */
.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock {
.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock, .union > .toggle-wrapper + .docblock {
margin-left: 0px;
}

Expand Down

0 comments on commit f146b97

Please sign in to comment.