rustdoc: hide fields of structs/unions > 5

This commit is contained in:
Manish Goregaokar 2021-03-20 19:11:49 -07:00
parent 71c52acf32
commit f146b9701d
2 changed files with 31 additions and 8 deletions

View File

@ -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);
@ -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!(
@ -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("}");
}
@ -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!(
@ -1341,7 +1362,6 @@ fn render_struct(
field.name.as_ref().unwrap(),
ty.print(cx.cache(), cx.tcx()),
);
has_visible_fields = true;
}
}
@ -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 => {

View File

@ -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;
}