]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: smartly hide associated items of traits if there are too many of them
authorManish Goregaokar <manishsmail@gmail.com>
Sun, 21 Mar 2021 02:39:29 +0000 (19:39 -0700)
committerManish Goregaokar <manishsmail@gmail.com>
Mon, 12 Apr 2021 15:45:45 +0000 (08:45 -0700)
src/librustdoc/html/render/print_item.rs
src/librustdoc/html/static/rustdoc.css
src/librustdoc/html/static/themes/ayu.css
src/librustdoc/html/static/themes/dark.css
src/librustdoc/html/static/themes/light.css

index b6e9a8e244a97b096f93a3cf91d35545dc1d010b..8db78e7718dc1c1fc73398afd591b60a08dbcd67 100644 (file)
@@ -443,10 +443,25 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
         } else {
             // FIXME: we should be using a derived_id for the Anchors here
             w.write_str("{\n");
+            let mut toggle = false;
+
+            // If there are too many associated types, hide _everything_
+            if should_hide_fields(types.len()) {
+                toggle = true;
+                toggle_open(w, "associated items");
+            }
             for t in &types {
                 render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
                 w.write_str(";\n");
             }
+            // If there are too many associated constants, hide everything after them
+            // We also do this if the types + consts is large because otherwise we could
+            // render a bunch of types and _then_ a bunch of consts just because both were
+            // _just_ under the limit
+            if !toggle & should_hide_fields(types.len() + consts.len()) {
+                toggle = true;
+                toggle_open(w, "associated constants and methods");
+            }
             if !types.is_empty() && !consts.is_empty() {
                 w.write_str("\n");
             }
@@ -454,6 +469,10 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
                 render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
                 w.write_str(";\n");
             }
+            if !toggle & should_hide_fields(required.len() + provided.len()) {
+                toggle = true;
+                toggle_open(w, "methods");
+            }
             if !consts.is_empty() && !required.is_empty() {
                 w.write_str("\n");
             }
@@ -484,6 +503,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
                     w.write_str("<div class=\"item-spacer\"></div>");
                 }
             }
+            if toggle {
+                toggle_close(w);
+            }
             w.write_str("}");
         }
         w.write_str("</pre>")
@@ -1284,9 +1306,7 @@ fn render_union(
     write!(w, " {{\n{}", tab);
     let count_fields = fields
         .iter()
-        .filter(
-            |f| if let clean::StructFieldItem(..) = *f.kind { true } else { false },
-        )
+        .filter(|f| if let clean::StructFieldItem(..) = *f.kind { true } else { false })
         .count();
     let toggle = should_hide_fields(count_fields);
     if toggle {
@@ -1343,9 +1363,7 @@ fn render_struct(
             w.write_str(" {");
             let count_fields = fields
                 .iter()
-                .filter(
-                    |f| if let clean::StructFieldItem(..) = *f.kind { true } else { false },
-                )
+                .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);
index 2cdacbe0b06abc2984df72187d6f635df9ac8f2d..309e55670b18a3bfc6a458a7aa85321168bffcdf 100644 (file)
@@ -1058,8 +1058,8 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
        margin-top: 3px;
 }
 
-/* for enum and struct fields */
-.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock, .union > .toggle-wrapper + .docblock {
+/* for hiding fields/variants/associated items */
+.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock, .union > .toggle-wrapper + .docblock, .trait > .toggle-wrapper + .docblock {
        margin-left: 0px;
 }
 
@@ -1785,7 +1785,7 @@ div.name.expand::before {
 .type-decl > pre > .docblock.attributes.top-attr {
        margin-left: 1.8em !important;
 }
-.type-decl > pre .toggle-attributes {
+.type-decl > pre .toggle-attributes {
        margin-left: 2.2em;
 }
 .type-decl > pre > .docblock.attributes {
index b24f4035ca868d6056109970f5f65fca1e0b36b7..0404fa50b99f2f18c299a4aac82db114f3cd9d46 100644 (file)
@@ -218,7 +218,7 @@ a {
        color: #c5c5c5;
 }
 
-.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
+.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow):not(.collapse-toggle),
 .docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
 #help a {
        color: #39AFD7;
index e863ed03f515696172151eccc67d1dd624713480..15b485a966fdaec3bd9bf4f45ecfeffcce87b1c2 100644 (file)
@@ -176,7 +176,7 @@ a {
        color: #ddd;
 }
 
-.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
+.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow):not(.collapse-toggle),
 .docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
 #help a {
        color: #D2991D;
index 9335dd96d299af9e4d69858d6a1380f64c0a8f68..4c83879e234b78a4ec1b3d5f0e79f90aabe9a41c 100644 (file)
@@ -174,7 +174,7 @@ a {
        color: #000;
 }
 
-.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow),
+.docblock:not(.type-decl) a:not(.srclink):not(.test-arrow):not(.collapse-toggle),
 .docblock-short a:not(.srclink):not(.test-arrow), .item-info a,
 #help a {
        color: #3873AD;