Skip to content

Commit b676f55

Browse files
rustdoc: add ways of collapsing all impl blocks
either shift+click the Summary button, or use the `_` key. this collapses everything, including (inherent) impl blocks. no need for a special "expand all impl blocks" method, as impl blocks are expanded during regular "expand all". doing "expand all" -> "collapse all" will always result in only impl blocks being expaned. some of the html is split up a bit awkwardly to try to avoid introducing new whitespaces nodes, which could affect display. Co-authored-by: Guillaume Gomez <[email protected]>
1 parent 3bc767e commit b676f55

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/librustdoc/html/static/js/main.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,11 @@ function preLoadCss(cssUrl) {
568568
break;
569569
case "-":
570570
ev.preventDefault();
571-
collapseAllDocs();
571+
collapseAllDocs(false);
572+
break;
573+
case "_":
574+
ev.preventDefault();
575+
collapseAllDocs(true);
572576
break;
573577

574578
case "?":
@@ -1038,11 +1042,14 @@ function preLoadCss(cssUrl) {
10381042
innerToggle.children[0].innerText = "Summary";
10391043
}
10401044

1041-
function collapseAllDocs() {
1045+
/**
1046+
* @param {boolean} collapseImpls - also collapse impl blocks if set to true
1047+
*/
1048+
function collapseAllDocs(collapseImpls) {
10421049
const innerToggle = document.getElementById(toggleAllDocsId);
10431050
addClass(innerToggle, "will-expand");
10441051
onEachLazy(document.getElementsByClassName("toggle"), e => {
1045-
if (e.parentNode.id !== "implementations-list" ||
1052+
if ((collapseImpls || e.parentNode.id !== "implementations-list") ||
10461053
(!hasClass(e, "implementors-toggle") &&
10471054
!hasClass(e, "type-contents-toggle"))
10481055
) {
@@ -1053,15 +1060,18 @@ function preLoadCss(cssUrl) {
10531060
innerToggle.children[0].innerText = "Show all";
10541061
}
10551062

1056-
function toggleAllDocs() {
1063+
/**
1064+
* @param {MouseEvent=} ev
1065+
*/
1066+
function toggleAllDocs(ev) {
10571067
const innerToggle = document.getElementById(toggleAllDocsId);
10581068
if (!innerToggle) {
10591069
return;
10601070
}
10611071
if (hasClass(innerToggle, "will-expand")) {
10621072
expandAllDocs();
10631073
} else {
1064-
collapseAllDocs();
1074+
collapseAllDocs(ev !== undefined && ev.shiftKey);
10651075
}
10661076
}
10671077

@@ -1519,6 +1529,10 @@ function preLoadCss(cssUrl) {
15191529
["&#9166;", "Go to active search result"],
15201530
["+", "Expand all sections"],
15211531
["-", "Collapse all sections"],
1532+
// for the sake of brevity, we don't say "inherint impl blocks",
1533+
// although that would be more correct,
1534+
// since trait impl blocks are collapsed by -
1535+
["_", "Collapse all sections, including impl blocks"],
15221536
].map(x => "<dt>" +
15231537
x[0].split(" ")
15241538
.map((y, index) => ((index & 1) === 0 ? "<kbd>" + y + "</kbd>" : " " + y + " "))

src/librustdoc/html/static/js/storage.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@ class RustdocToolbarElement extends HTMLElement {
418418
<div id="help-button" tabindex="-1">
419419
<a href="${rootPath}help.html"><span class="label">Help</span></a>
420420
</div>
421-
<button id="toggle-all-docs"><span class="label">Summary</span></button>`;
421+
<button id="toggle-all-docs"
422+
title="Collapse sections (shift-click to also collapse impl blocks)"><span
423+
class="label">Summary</span></button>`;
422424
}
423425
}
424426
window.customElements.define("rustdoc-toolbar", RustdocToolbarElement);

0 commit comments

Comments
 (0)