Skip to content

rustdoc: clean up line numbers on code examples #102118

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

Merged
merged 3 commits into from
Sep 23, 2022
Merged
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
6 changes: 1 addition & 5 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,9 @@ h2.location a {
}

.rustdoc .example-wrap {
display: inline-flex;
display: flex;
margin-bottom: 10px;
}

.example-wrap {
position: relative;
width: 100%;
}

.example-wrap > pre.line-number {
Expand Down
41 changes: 30 additions & 11 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,20 +697,39 @@ function loadCss(cssFileName) {
}
}());

window.rustdoc_add_line_numbers_to_examples = () => {
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
const parent = x.parentNode;
const line_numbers = parent.querySelectorAll(".line-number");
if (line_numbers.length > 0) {
return;
}
const count = x.textContent.split("\n").length;
const elems = [];
for (let i = 0; i < count; ++i) {
elems.push(i + 1);
}
const node = document.createElement("pre");
addClass(node, "line-number");
node.innerHTML = elems.join("\n");
parent.insertBefore(node, x);
});
};

window.rustdoc_remove_line_numbers_from_examples = () => {
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
const parent = x.parentNode;
const line_numbers = parent.querySelectorAll(".line-number");
for (const node of line_numbers) {
parent.removeChild(node);
}
});
};

(function() {
// To avoid checking on "rustdoc-line-numbers" value on every loop...
if (getSettingValue("line-numbers") === "true") {
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
const count = x.textContent.split("\n").length;
const elems = [];
for (let i = 0; i < count; ++i) {
elems.push(i + 1);
}
const node = document.createElement("pre");
addClass(node, "line-number");
node.innerHTML = elems.join("\n");
x.parentNode.insertBefore(node, x);
});
window.rustdoc_add_line_numbers_to_examples();
}
}());

Expand Down
7 changes: 7 additions & 0 deletions src/librustdoc/html/static/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
updateSystemTheme();
updateLightAndDark();
break;
case "line-numbers":
if (value === true) {
window.rustdoc_add_line_numbers_to_examples();
} else {
window.rustdoc_remove_line_numbers_from_examples();
}
break;
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/rustdoc-gui/docblock-code-block-line-number.goml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ assert-css: ("pre.line-number", {
})
// The first code block has two lines so let's check its `<pre>` elements lists both of them.
assert-text: ("pre.line-number", "1\n2")

// Now, try changing the setting dynamically. We'll turn it off, using the settings menu,
// and make sure it goes away.

// First, open the settings menu.
click: "#settings-menu"
wait-for: "#settings"
assert-css: ("#settings", {"display": "block"})

// Then, click the toggle button.
click: "input#line-numbers + .slider"
wait-for: 100 // wait-for-false does not exist
assert-false: "pre.line-number"

// Finally, turn it on again.
click: "input#line-numbers + .slider"
wait-for: "pre.line-number"
2 changes: 1 addition & 1 deletion src/test/rustdoc-gui/source-anchor-scroll.goml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ assert-property: ("html", {"scrollTop": "0"})
click: '//a[text() = "barbar"]'
assert-property: ("html", {"scrollTop": "125"})
click: '//a[text() = "bar"]'
assert-property: ("html", {"scrollTop": "166"})
assert-property: ("html", {"scrollTop": "156"})
Copy link
Member

Choose a reason for hiding this comment

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

Why did this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Line wrapping changed because of the slightly wider header box.

Copy link
Member

Choose a reason for hiding this comment

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

Well I guess it's a good thing then!

click: '//a[text() = "sub_fn"]'
assert-property: ("html", {"scrollTop": "53"})

Expand Down