about summary refs log tree commit diff
path: root/src/librustdoc/html/static
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-04-16 14:08:30 +0200
committerGitHub <noreply@github.com>2021-04-16 14:08:30 +0200
commita5c68d7908dc970f2d51c0b2e3d2dd8f943f3357 (patch)
treebf756c1a549dd1ab86d0627894c990f25e6a3bea /src/librustdoc/html/static
parentd4bc912c4816a660b052dd7c8dffbc63fd4dffdf (diff)
parent55b29443db473d49c1c64d469da5f6a50dc6beb7 (diff)
downloadrust-a5c68d7908dc970f2d51c0b2e3d2dd8f943f3357.tar.gz
rust-a5c68d7908dc970f2d51c0b2e3d2dd8f943f3357.zip
Rollup merge of #83337 - Manishearth:item-hide, r=GuillaumeGomez
rustdoc: Hide item contents, not items

This tweaks rustdoc to hide item contents instead of items, and only when there are too many of them.

This means that users will _always_ see the type parameters, and will _often_ see fields/etc as long as they are small. Traits have some heuristics for hiding only the methods or only the methods and the consts, since the associated types are super important.

I'm happy to play around with the heuristics here; we could potentially make it so that structs/enums/etc are always hidden but traits will try really hard to show type aliases.

This needs a test, but you can see it rendered at https://manishearth.net/sand/doc_render/bar/

<details>

<summary> Code example </summary>

```rust
pub struct PubStruct {
    pub a: usize,
    pub b: usize,
}

pub struct BigPubStruct {
    pub a: usize,
    pub b: usize,
    pub c: usize,
    pub d: usize,
    pub e: usize,
    pub f: usize,
}

pub union BigUnion {
    pub a: usize,
    pub b: usize,
    pub c: usize,
    pub d: usize,
    pub e: usize,
    pub f: usize,
}

pub union Union {
    pub a: usize,
    pub b: usize,
    pub c: usize,
}

pub struct PrivStruct {
    a: usize,
    b: usize,
}

pub enum Enum {
    A, B, C,
    D {
        a: u8,
        b: u8
    }
}

pub enum LargeEnum {
    A, B, C, D, E, F, G, H, I, J
}

pub trait Trait {
    type A;
    #[must_use]
    fn foo();
    fn bar();
}

pub trait GinormousTrait {
    type A;
    type B;
    type C;
    type D;
    type E;
    type F;
    const N: usize = 1;
    #[must_use]
    fn foo();
    fn bar();
}

pub trait HugeTrait {
    type A;
    const M: usize = 1;
    const N: usize = 1;
    const O: usize = 1;
    const P: usize = 1;
    const Q: usize = 1;
    #[must_use]
    fn foo();
    fn bar();
}

pub trait BigTrait {
    type A;
    #[must_use]
    fn foo();
    fn bar();
    fn baz();
    fn quux();
    fn frob();
    fn greeble();
}

#[macro_export]
macro_rules! foo {
    (a) => {a};
}
```

</details>

Fixes https://github.com/rust-lang/rust/issues/82114
Diffstat (limited to 'src/librustdoc/html/static')
-rw-r--r--src/librustdoc/html/static/main.js72
-rw-r--r--src/librustdoc/html/static/rustdoc.css82
-rw-r--r--src/librustdoc/html/static/storage.js1
-rw-r--r--src/librustdoc/html/static/themes/ayu.css4
-rw-r--r--src/librustdoc/html/static/themes/dark.css4
-rw-r--r--src/librustdoc/html/static/themes/light.css4
6 files changed, 75 insertions, 92 deletions
diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js
index 80dc6b923f6..d71cc15a457 100644
--- a/src/librustdoc/html/static/main.js
+++ b/src/librustdoc/html/static/main.js
@@ -2316,6 +2316,9 @@ function hideThemeButtonState() {
         if (hasClass(innerToggle, "will-expand")) {
             updateLocalStorage("rustdoc-collapse", "false");
             removeClass(innerToggle, "will-expand");
+            onEachLazy(document.getElementsByTagName("details"), function(e) {
+                e.open = true;
+            });
             onEveryMatchingChild(innerToggle, "inner", function(e) {
                 e.innerHTML = labelForToggleButton(false);
             });
@@ -2328,6 +2331,9 @@ function hideThemeButtonState() {
         } else {
             updateLocalStorage("rustdoc-collapse", "true");
             addClass(innerToggle, "will-expand");
+            onEachLazy(document.getElementsByTagName("details"), function(e) {
+                e.open = false;
+            });
             onEveryMatchingChild(innerToggle, "inner", function(e) {
                 var parent = e.parentNode;
                 var superParent = null;
@@ -2569,6 +2575,7 @@ function hideThemeButtonState() {
         var toggle = createSimpleToggle(false);
         var hideMethodDocs = getSettingValue("auto-hide-method-docs") === "true";
         var hideImplementors = getSettingValue("auto-collapse-implementors") !== "false";
+        var hideLargeItemContents = getSettingValue("auto-hide-large-items") !== "false";
 
         var func = function(e) {
             var next = e.nextElementSibling;
@@ -2615,6 +2622,14 @@ function hideThemeButtonState() {
         onEachLazy(document.getElementsByClassName("associatedconstant"), func);
         onEachLazy(document.getElementsByClassName("impl"), funcImpl);
         var impl_call = function() {};
+        // Large items are hidden by default in the HTML. If the setting overrides that, show 'em.
+        if (!hideLargeItemContents) {
+            onEachLazy(document.getElementsByTagName("details"), function (e) {
+                if (hasClass(e, "type-contents-toggle")) {
+                    e.open = true;
+                }
+            });
+        }
         if (hideMethodDocs === true) {
             impl_call = function(e, newToggle) {
                 if (e.id.match(/^impl(?:-\d+)?$/) === null) {
@@ -2683,18 +2698,7 @@ function hideThemeButtonState() {
                 });
             }
         }
-        var showItemDeclarations = getSettingValue("auto-hide-" + className);
-        if (showItemDeclarations === null) {
-            if (className === "enum" || className === "macro") {
-                showItemDeclarations = "false";
-            } else if (className === "struct" || className === "union" || className === "trait") {
-                showItemDeclarations = "true";
-            } else {
-                // In case we found an unknown type, we just use the "parent" value.
-                showItemDeclarations = getSettingValue("auto-hide-declarations");
-            }
-        }
-        showItemDeclarations = showItemDeclarations === "false";
+
         function buildToggleWrapper(e) {
             if (hasClass(e, "autohide")) {
                 var wrap = e.previousElementSibling;
@@ -2721,11 +2725,8 @@ function hideThemeButtonState() {
                 var extraClass;
 
                 if (hasClass(e, "type-decl")) {
-                    fontSize = "20px";
-                    otherMessage = "&nbsp;Show&nbsp;declaration";
-                    if (showItemDeclarations === false) {
-                        extraClass = "collapsed";
-                    }
+                    // We do something special for these
+                    return;
                 } else if (hasClass(e, "sub-variant")) {
                     otherMessage = "&nbsp;Show&nbsp;fields";
                 } else if (hasClass(e, "non-exhaustive")) {
@@ -2750,11 +2751,8 @@ function hideThemeButtonState() {
                         otherMessage,
                         fontSize,
                         extraClass,
-                        hasClass(e, "type-decl") === false || showItemDeclarations === true),
+                        true),
                     e);
-                if (hasClass(e, "type-decl") === true && showItemDeclarations === true) {
-                    collapseDocs(e.previousSibling.childNodes[0], "toggle");
-                }
                 if (hasClass(e, "non-exhaustive") === true) {
                     collapseDocs(e.previousSibling.childNodes[0], "toggle");
                 }
@@ -2772,38 +2770,6 @@ function hideThemeButtonState() {
         }
     }());
 
-    function createToggleWrapper(tog) {
-        var span = document.createElement("span");
-        span.className = "toggle-label";
-        span.style.display = "none";
-        span.innerHTML = "&nbsp;Expand&nbsp;attributes";
-        tog.appendChild(span);
-
-        var wrapper = document.createElement("div");
-        wrapper.className = "toggle-wrapper toggle-attributes";
-        wrapper.appendChild(tog);
-        return wrapper;
-    }
-
-    (function() {
-        // To avoid checking on "rustdoc-item-attributes" value on every loop...
-        var itemAttributesFunc = function() {};
-        if (getSettingValue("auto-hide-attributes") !== "false") {
-            itemAttributesFunc = function(x) {
-                collapseDocs(x.previousSibling.childNodes[0], "toggle");
-            };
-        }
-        var attributesToggle = createToggleWrapper(createSimpleToggle(false));
-        onEachLazy(main.getElementsByClassName("attributes"), function(i_e) {
-            var attr_tog = attributesToggle.cloneNode(true);
-            if (hasClass(i_e, "top-attr") === true) {
-                addClass(attr_tog, "top-attr");
-            }
-            i_e.parentNode.insertBefore(attr_tog, i_e);
-            itemAttributesFunc(i_e);
-        });
-    }());
-
     (function() {
         // To avoid checking on "rustdoc-line-numbers" value on every loop...
         var lineNumbersFunc = function() {};
diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css
index 705ae17f3eb..427564cd779 100644
--- a/src/librustdoc/html/static/rustdoc.css
+++ b/src/librustdoc/html/static/rustdoc.css
@@ -1056,12 +1056,6 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
 	margin-top: 3px;
 }
 
-.enum > .toggle-wrapper + .docblock, .struct > .toggle-wrapper + .docblock {
-	margin-left: 30px;
-	margin-bottom: 20px;
-	margin-top: 5px;
-}
-
 .docblock > .section-header:first-child {
 	margin-left: 15px;
 	margin-top: 0;
@@ -1071,30 +1065,10 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
 	left: -10px;
 }
 
-.enum > .collapsed, .struct > .collapsed {
-	margin-bottom: 25px;
-}
-
 #main > .variant, #main > .structfield {
 	display: block;
 }
 
-.attributes {
-	display: block;
-	margin-top: 0px !important;
-	margin-right: 0px;
-	margin-bottom: 0px !important;
-	margin-left: 30px;
-}
-.toggle-attributes.collapsed {
-	margin-bottom: 0;
-}
-.impl-items > .toggle-attributes {
-	margin-left: 20px;
-}
-.impl-items .attributes {
-	font-weight: 500;
-}
 
 :target > code {
 	opacity: 1;
@@ -1781,16 +1755,54 @@ div.name.expand::before {
 	top: 2px;
 }
 
-/* This part is to fix the "Expand attributes" part in the type declaration. */
-.type-decl > pre > .toggle-wrapper.toggle-attributes.top-attr {
-	margin-left: 0 !important;
+/* The hideme class is used on summary tags that contain a span with
+	placeholder text shown only when the toggle is closed. For instance,
+	"Expand description" or "Show methods". */
+details.rustdoc-toggle > summary.hideme {
+	cursor: pointer;
+}
+
+details.rustdoc-toggle > summary::-webkit-details-marker {
+	display: none;
+}
+
+details.rustdoc-toggle > summary.hideme > span {
+	margin-left: 9px;
 }
-.type-decl > pre > .docblock.attributes.top-attr {
-	margin-left: 1.8em !important;
+
+details.rustdoc-toggle > summary::before {
+	content: "[+]";
+	font-weight: 300;
+	font-size: 0.8em;
+	letter-spacing: 1px;
+}
+
+details.rustdoc-toggle > summary.hideme::before {
+	position: relative;
 }
-.type-decl > pre > .toggle-attributes {
-	margin-left: 2.2em;
+
+details.rustdoc-toggle > summary:not(.hideme)::before {
+	float: left;
 }
-.type-decl > pre > .docblock.attributes {
-	margin-left: 4em;
+
+/* When a "hideme" summary is open and the "Expand description" or "Show
+	methods" text is hidden, we want the [-] toggle that remains to not
+	affect the layout of the items to its right. To do that, we use
+	absolute positioning. Note that we also set position: relative
+	on the parent <details> to make this work properly. */
+details.rustdoc-toggle[open] > summary.hideme {
+	position: absolute;
+}
+
+details.rustdoc-toggle[open] {
+	position: relative;
+}
+
+details.rustdoc-toggle[open] > summary.hideme > span {
+	display: none;
+}
+
+details.rustdoc-toggle[open] > summary::before {
+	content: "[−]";
+	display: inline;
 }
diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js
index c68128516d2..2ed87fdedae 100644
--- a/src/librustdoc/html/static/storage.js
+++ b/src/librustdoc/html/static/storage.js
@@ -1,6 +1,5 @@
 // From rust:
 /* global resourcesSuffix */
-
 var darkThemes = ["dark", "ayu"];
 window.currentTheme = document.getElementById("themeStyle");
 window.mainTheme = document.getElementById("mainThemeStyle");
diff --git a/src/librustdoc/html/static/themes/ayu.css b/src/librustdoc/html/static/themes/ayu.css
index b24f4035ca8..5f6f3d66e57 100644
--- a/src/librustdoc/html/static/themes/ayu.css
+++ b/src/librustdoc/html/static/themes/ayu.css
@@ -224,7 +224,9 @@ a {
 	color: #39AFD7;
 }
 
-.collapse-toggle {
+.collapse-toggle,
+details.rustdoc-toggle > summary.hideme > span,
+details.rustdoc-toggle > summary::before {
 	color: #999;
 }
 
diff --git a/src/librustdoc/html/static/themes/dark.css b/src/librustdoc/html/static/themes/dark.css
index e863ed03f51..2ce6cf4cc45 100644
--- a/src/librustdoc/html/static/themes/dark.css
+++ b/src/librustdoc/html/static/themes/dark.css
@@ -186,7 +186,9 @@ a.test-arrow {
 	color: #dedede;
 }
 
-.collapse-toggle {
+.collapse-toggle,
+details.rustdoc-toggle > summary.hideme > span,
+details.rustdoc-toggle > summary::before {
 	color: #999;
 }
 
diff --git a/src/librustdoc/html/static/themes/light.css b/src/librustdoc/html/static/themes/light.css
index 9335dd96d29..31b3562cfcb 100644
--- a/src/librustdoc/html/static/themes/light.css
+++ b/src/librustdoc/html/static/themes/light.css
@@ -184,7 +184,9 @@ a.test-arrow {
 	color: #f5f5f5;
 }
 
-.collapse-toggle {
+.collapse-toggle,
+details.rustdoc-toggle > summary.hideme > span,
+details.rustdoc-toggle > summary::before {
 	color: #999;
 }