Skip to content

rustdoc: add ways of collapsing all impl blocks #141663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,11 @@ function preLoadCss(cssUrl) {
break;
case "-":
ev.preventDefault();
collapseAllDocs();
collapseAllDocs(false);
break;
case "_":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think adding a new key to handle it is a good idea. What you suggested with shift+minus sounds like a better approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't shift + minus send _ on a standard US keyboard? or is there something I don't know about JS events?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, yes. getVirtualKey uses event.key, which sends the printable representation of the text.

ev.preventDefault();
collapseAllDocs(true);
break;

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

function collapseAllDocs() {
/**
* @param {boolean} collapseImpls - also collapse impl blocks if set to true
*/
function collapseAllDocs(collapseImpls) {
const innerToggle = document.getElementById(toggleAllDocsId);
addClass(innerToggle, "will-expand");
onEachLazy(document.getElementsByClassName("toggle"), e => {
if (e.parentNode.id !== "implementations-list" ||
if ((collapseImpls || e.parentNode.id !== "implementations-list") ||
(!hasClass(e, "implementors-toggle") &&
!hasClass(e, "type-contents-toggle"))
) {
Expand All @@ -1053,15 +1060,18 @@ function preLoadCss(cssUrl) {
innerToggle.children[0].innerText = "Show all";
}

function toggleAllDocs() {
/**
* @param {MouseEvent=} ev
*/
function toggleAllDocs(ev) {
const innerToggle = document.getElementById(toggleAllDocsId);
if (!innerToggle) {
return;
}
if (hasClass(innerToggle, "will-expand")) {
expandAllDocs();
} else {
collapseAllDocs();
collapseAllDocs(ev !== undefined && ev.shiftKey);
}
}

Expand Down Expand Up @@ -1519,6 +1529,10 @@ function preLoadCss(cssUrl) {
["⏎", "Go to active search result"],
["+", "Expand all sections"],
["-", "Collapse all sections"],
// for the sake of brevity, we don't say "inherint impl blocks",
// although that would be more correct,
// since trait impl blocks are collapsed by -
["_", "Collapse all sections, including impl blocks"],
].map(x => "<dt>" +
x[0].split(" ")
.map((y, index) => ((index & 1) === 0 ? "<kbd>" + y + "</kbd>" : " " + y + " "))
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ class RustdocToolbarElement extends HTMLElement {
<div id="help-button" tabindex="-1">
<a href="${rootPath}help.html"><span class="label">Help</span></a>
</div>
<button id="toggle-all-docs"><span class="label">Summary</span></button>`;
<button id="toggle-all-docs"
title="Collapse sections (shift-click to also collapse impl blocks)"><span
class="label">Summary</span></button>`;
}
}
window.customElements.define("rustdoc-toolbar", RustdocToolbarElement);
Loading