Skip to content

Commit ecbc00f

Browse files
authored
Rollup merge of #102118 - notriddle:notriddle/line-numbers, r=GuillaumeGomez
rustdoc: clean up line numbers on code examples * First commit switches from `display: inline-flex; width: 100%` to `display: flex`. `display: inline-flex` was used as part of e961d39, the original commit that added these line numbers. Does anyone know why it was done this way? * Second commit makes it so that toggling this checkbox will update the page in real time, just like changing themes does. Preview: https://notriddle.com/notriddle-rustdoc-test/line-numbers/std/vec/struct.Vec.html
2 parents 41ad726 + 8b4c0d9 commit ecbc00f

File tree

5 files changed

+56
-17
lines changed

5 files changed

+56
-17
lines changed

src/librustdoc/html/static/css/rustdoc.css

+1-5
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,9 @@ h2.location a {
577577
}
578578

579579
.rustdoc .example-wrap {
580-
display: inline-flex;
580+
display: flex;
581581
margin-bottom: 10px;
582-
}
583-
584-
.example-wrap {
585582
position: relative;
586-
width: 100%;
587583
}
588584

589585
.example-wrap > pre.line-number {

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

+30-11
Original file line numberDiff line numberDiff line change
@@ -697,20 +697,39 @@ function loadCss(cssFileName) {
697697
}
698698
}());
699699

700+
window.rustdoc_add_line_numbers_to_examples = () => {
701+
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
702+
const parent = x.parentNode;
703+
const line_numbers = parent.querySelectorAll(".line-number");
704+
if (line_numbers.length > 0) {
705+
return;
706+
}
707+
const count = x.textContent.split("\n").length;
708+
const elems = [];
709+
for (let i = 0; i < count; ++i) {
710+
elems.push(i + 1);
711+
}
712+
const node = document.createElement("pre");
713+
addClass(node, "line-number");
714+
node.innerHTML = elems.join("\n");
715+
parent.insertBefore(node, x);
716+
});
717+
};
718+
719+
window.rustdoc_remove_line_numbers_from_examples = () => {
720+
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
721+
const parent = x.parentNode;
722+
const line_numbers = parent.querySelectorAll(".line-number");
723+
for (const node of line_numbers) {
724+
parent.removeChild(node);
725+
}
726+
});
727+
};
728+
700729
(function() {
701730
// To avoid checking on "rustdoc-line-numbers" value on every loop...
702731
if (getSettingValue("line-numbers") === "true") {
703-
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
704-
const count = x.textContent.split("\n").length;
705-
const elems = [];
706-
for (let i = 0; i < count; ++i) {
707-
elems.push(i + 1);
708-
}
709-
const node = document.createElement("pre");
710-
addClass(node, "line-number");
711-
node.innerHTML = elems.join("\n");
712-
x.parentNode.insertBefore(node, x);
713-
});
732+
window.rustdoc_add_line_numbers_to_examples();
714733
}
715734
}());
716735

src/librustdoc/html/static/js/settings.js

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
updateSystemTheme();
2020
updateLightAndDark();
2121
break;
22+
case "line-numbers":
23+
if (value === true) {
24+
window.rustdoc_add_line_numbers_to_examples();
25+
} else {
26+
window.rustdoc_remove_line_numbers_from_examples();
27+
}
28+
break;
2229
}
2330
}
2431

src/test/rustdoc-gui/docblock-code-block-line-number.goml

+17
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@ assert-css: ("pre.line-number", {
2020
})
2121
// The first code block has two lines so let's check its `<pre>` elements lists both of them.
2222
assert-text: ("pre.line-number", "1\n2")
23+
24+
// Now, try changing the setting dynamically. We'll turn it off, using the settings menu,
25+
// and make sure it goes away.
26+
27+
// First, open the settings menu.
28+
click: "#settings-menu"
29+
wait-for: "#settings"
30+
assert-css: ("#settings", {"display": "block"})
31+
32+
// Then, click the toggle button.
33+
click: "input#line-numbers + .slider"
34+
wait-for: 100 // wait-for-false does not exist
35+
assert-false: "pre.line-number"
36+
37+
// Finally, turn it on again.
38+
click: "input#line-numbers + .slider"
39+
wait-for: "pre.line-number"

src/test/rustdoc-gui/source-anchor-scroll.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ assert-property: ("html", {"scrollTop": "0"})
1010
click: '//a[text() = "barbar"]'
1111
assert-property: ("html", {"scrollTop": "125"})
1212
click: '//a[text() = "bar"]'
13-
assert-property: ("html", {"scrollTop": "166"})
13+
assert-property: ("html", {"scrollTop": "156"})
1414
click: '//a[text() = "sub_fn"]'
1515
assert-property: ("html", {"scrollTop": "53"})
1616

0 commit comments

Comments
 (0)