Skip to content

Commit 77562f2

Browse files
authored
Rollup merge of #94696 - GuillaumeGomez:align-line-numbers-right, r=notriddle
Remove whitespaces and use CSS to align line numbers to the right instead Instead of generating whitespaces to create padding, we simply use the CSS rule: `text-align: right`. Nice side-effect: it reduces the generated HTML size from **75.004** to **74.828** (MegaBytes) on the std source pages (it's not much but it's always a nice plus 😆 ). There are no changes in the generated UI. r? `@notriddle`
2 parents 9d7166c + f23d6d3 commit 77562f2

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/librustdoc/html/sources.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,16 @@ crate fn print_src(
272272
) {
273273
let lines = s.lines().count();
274274
let mut line_numbers = Buffer::empty_from(buf);
275-
let mut cols = 0;
276-
let mut tmp = lines;
277-
while tmp > 0 {
278-
cols += 1;
279-
tmp /= 10;
280-
}
281275
line_numbers.write_str("<pre class=\"line-numbers\">");
282276
match source_context {
283277
SourceContext::Standalone => {
284278
for line in 1..=lines {
285-
writeln!(line_numbers, "<span id=\"{0}\">{0:1$}</span>", line, cols)
279+
writeln!(line_numbers, "<span id=\"{0}\">{0}</span>", line)
286280
}
287281
}
288282
SourceContext::Embedded { offset } => {
289283
for line in 1..=lines {
290-
writeln!(line_numbers, "<span>{0:1$}</span>", line + offset, cols)
284+
writeln!(line_numbers, "<span>{0}</span>", line + offset)
291285
}
292286
}
293287
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,9 @@ h2.location a {
541541
text-decoration: underline;
542542
}
543543

544+
.line-numbers {
545+
text-align: right;
546+
}
544547
.rustdoc:not(.source) .example-wrap > pre:not(.line-number) {
545548
width: 100%;
546549
overflow-x: auto;

src/test/rustdoc-gui/source-code-page.goml

+3
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ assert-attribute: (".line-numbers > span:nth-child(6)", {"class": "line-highligh
1414
assert-attribute-false: (".line-numbers > span:nth-child(7)", {"class": "line-highlighted"})
1515
// This is to ensure that the content is correctly align with the line numbers.
1616
compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))
17+
18+
// Assert that the line numbers text is aligned to the right.
19+
assert-css: (".line-numbers", {"text-align": "right"})

0 commit comments

Comments
 (0)