Skip to content

Commit d4b266f

Browse files
authored
Unrolled build for rust-lang#130664
Rollup merge of rust-lang#130664 - GuillaumeGomez:generate-line-numbers-on-non-rust, r=notriddle Generate line numbers for non-rust code examples as well Currently, the "enable line numbers" setting only generated it for rust code examples. Found this limitation a bit strange so I decided to remove it. You can test it [here](https://rustdoc.crud.net/imperio/generate-line-number-non-rust/doc/lib2/sub_mod/struct.Foo.html). r? ``@notriddle``
2 parents 1f9a018 + f451a41 commit d4b266f

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

Diff for: src/librustdoc/html/markdown.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
261261
</pre>\
262262
</div>",
263263
added_classes = added_classes.join(" "),
264-
text = Escape(&original_text),
264+
text = Escape(
265+
original_text.strip_suffix('\n').unwrap_or(&original_text)
266+
),
265267
)
266268
.into(),
267269
));

Diff for: src/librustdoc/html/markdown/tests.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,13 @@ fn test_ascii_with_prepending_hashtag() {
524524
####.###..#....#....#..#.
525525
#..#.#....#....#....#..#.
526526
#..#.#....#....#....#..#.
527-
#..#.####.####.####..##..
528-
</code></pre></div>",
527+
#..#.####.####.####..##..</code></pre></div>",
529528
);
530529
t(
531530
r#"```markdown
532531
# hello
533532
```"#,
534533
"<div class=\"example-wrap\"><pre class=\"language-markdown\"><code>\
535-
# hello
536-
</code></pre></div>",
534+
# hello</code></pre></div>",
537535
);
538536
}

Diff for: src/librustdoc/html/static/js/main.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,13 @@ function preLoadCss(cssUrl) {
986986
}());
987987

988988
window.rustdoc_add_line_numbers_to_examples = () => {
989-
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
989+
if (document.querySelector(".rustdoc.src")) {
990+
// We are in the source code page, nothing to be done here!
991+
return;
992+
}
993+
onEachLazy(document.querySelectorAll(
994+
":not(.scraped-example) > .example-wrap > pre:not(.example-line-numbers)",
995+
), x => {
990996
const parent = x.parentNode;
991997
const line_numbers = parent.querySelectorAll(".example-line-numbers");
992998
if (line_numbers.length > 0) {
@@ -1005,12 +1011,8 @@ function preLoadCss(cssUrl) {
10051011
};
10061012

10071013
window.rustdoc_remove_line_numbers_from_examples = () => {
1008-
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
1009-
const parent = x.parentNode;
1010-
const line_numbers = parent.querySelectorAll(".example-line-numbers");
1011-
for (const node of line_numbers) {
1012-
parent.removeChild(node);
1013-
}
1014+
onEachLazy(document.querySelectorAll(".example-wrap > .example-line-numbers"), x => {
1015+
x.parentNode.removeChild(x);
10141016
});
10151017
};
10161018

Diff for: tests/rustdoc-gui/docblock-code-block-line-number.goml

+60-7
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ assert-css: ("#settings", {"display": "block"})
8787

8888
// Then, click the toggle button.
8989
click: "input#line-numbers"
90-
wait-for: 100 // wait-for-false does not exist
90+
wait-for: 100 // FIXME: `wait-for-false` does not exist
9191
assert-false: "pre.example-line-numbers"
9292
assert-local-storage: {"rustdoc-line-numbers": "false" }
9393

@@ -107,6 +107,8 @@ assert-css: (
107107
click: "input#line-numbers"
108108
wait-for: "pre.example-line-numbers"
109109
assert-local-storage: {"rustdoc-line-numbers": "true" }
110+
wait-for: 100 // FIXME: `wait-for-false` does not exist
111+
assert: "pre.example-line-numbers"
110112

111113
// Same check with scraped examples line numbers.
112114
go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html"
@@ -145,9 +147,6 @@ assert-css: (
145147
ALL,
146148
)
147149

148-
// Checking line numbers on scraped code examples.
149-
go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html"
150-
151150
define-function: (
152151
"check-padding",
153152
[path, padding_bottom],
@@ -157,19 +156,19 @@ define-function: (
157156
"padding-bottom": "0px",
158157
"padding-left": "0px",
159158
"padding-right": "0px",
160-
})
159+
}, ALL)
161160
assert-css: (|path| + " .src-line-numbers > pre", {
162161
"padding-top": "14px",
163162
"padding-bottom": |padding_bottom|,
164163
"padding-left": "0px",
165164
"padding-right": "0px",
166-
})
165+
}, ALL)
167166
assert-css: (|path| + " .src-line-numbers > pre > span", {
168167
"padding-top": "0px",
169168
"padding-bottom": "0px",
170169
"padding-left": "8px",
171170
"padding-right": "8px",
172-
})
171+
}, ALL)
173172
},
174173
)
175174

@@ -188,6 +187,35 @@ call-function: ("check-padding", {
188187
"padding_bottom": "14px",
189188
})
190189

190+
define-function: ("check-line-numbers-existence", [], block {
191+
assert-local-storage: {"rustdoc-line-numbers": "true" }
192+
assert-false: ".example-line-numbers"
193+
click: "#settings-menu"
194+
wait-for: "#settings"
195+
196+
// Then, click the toggle button.
197+
click: "input#line-numbers"
198+
wait-for: 100 // FIXME: `wait-for-false` does not exist
199+
assert-local-storage-false: {"rustdoc-line-numbers": "true" }
200+
assert-false: ".example-line-numbers"
201+
// Line numbers should still be there.
202+
assert: ".src-line-numbers"
203+
// Now disabling the setting.
204+
click: "input#line-numbers"
205+
wait-for: 100 // FIXME: `wait-for-false` does not exist
206+
assert-local-storage: {"rustdoc-line-numbers": "true" }
207+
assert-false: ".example-line-numbers"
208+
// Line numbers should still be there.
209+
assert: ".src-line-numbers"
210+
// Closing settings menu.
211+
click: "#settings-menu"
212+
wait-for-css: ("#settings", {"display": "none"})
213+
})
214+
215+
// Checking that turning off the line numbers setting won't remove line numbers from scraped
216+
// examples.
217+
call-function: ("check-line-numbers-existence", {})
218+
191219
// Now checking the line numbers in the source code page.
192220
click: ".src"
193221
assert-css: (".src-line-numbers", {
@@ -202,3 +230,28 @@ assert-css: (".src-line-numbers > a", {
202230
"padding-left": "8px",
203231
"padding-right": "8px",
204232
})
233+
// Checking that turning off the line numbers setting won't remove line numbers.
234+
call-function: ("check-line-numbers-existence", {})
235+
236+
// Now checking that even non-rust code blocks have line numbers generated.
237+
go-to: "file://" + |DOC_PATH| + "/lib2/sub_mod/struct.Foo.html"
238+
assert-local-storage: {"rustdoc-line-numbers": "true" }
239+
assert: ".example-wrap > pre.language-txt"
240+
assert: ".example-wrap > pre.rust"
241+
assert-count: (".example-wrap", 2)
242+
assert-count: (".example-wrap > pre.example-line-numbers", 2)
243+
244+
click: "#settings-menu"
245+
wait-for: "#settings"
246+
247+
// Then, click the toggle button.
248+
click: "input#line-numbers"
249+
wait-for: 100 // FIXME: `wait-for-false` does not exist
250+
assert-local-storage-false: {"rustdoc-line-numbers": "true" }
251+
assert-count: (".example-wrap > pre.example-line-numbers", 0)
252+
253+
// Now turning off the setting.
254+
click: "input#line-numbers"
255+
wait-for: 100 // FIXME: `wait-for-false` does not exist
256+
assert-local-storage: {"rustdoc-line-numbers": "true" }
257+
assert-count: (".example-wrap > pre.example-line-numbers", 2)

0 commit comments

Comments
 (0)