about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-03-07 18:39:03 +0100
committerGitHub <noreply@github.com>2022-03-07 18:39:03 +0100
commit77562f2350c4dbe2d19a253f70046fb3edd9baa6 (patch)
tree063d5758a165688040a6314398ffe8f2ac48f75d
parent9d7166c66f7d2cd7d2e51abae3e42a520fb359e0 (diff)
parentf23d6d3a47c15bc0fc3fbf2abdd318c3a2c09c7c (diff)
downloadrust-77562f2350c4dbe2d19a253f70046fb3edd9baa6.tar.gz
rust-77562f2350c4dbe2d19a253f70046fb3edd9baa6.zip
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 :laughing: ).

There are no changes in the generated UI.

r? `@notriddle`
-rw-r--r--src/librustdoc/html/sources.rs10
-rw-r--r--src/librustdoc/html/static/css/rustdoc.css3
-rw-r--r--src/test/rustdoc-gui/source-code-page.goml3
3 files changed, 8 insertions, 8 deletions
diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs
index bae04f2095a..9fba6e91162 100644
--- a/src/librustdoc/html/sources.rs
+++ b/src/librustdoc/html/sources.rs
@@ -272,22 +272,16 @@ crate fn print_src(
 ) {
     let lines = s.lines().count();
     let mut line_numbers = Buffer::empty_from(buf);
-    let mut cols = 0;
-    let mut tmp = lines;
-    while tmp > 0 {
-        cols += 1;
-        tmp /= 10;
-    }
     line_numbers.write_str("<pre class=\"line-numbers\">");
     match source_context {
         SourceContext::Standalone => {
             for line in 1..=lines {
-                writeln!(line_numbers, "<span id=\"{0}\">{0:1$}</span>", line, cols)
+                writeln!(line_numbers, "<span id=\"{0}\">{0}</span>", line)
             }
         }
         SourceContext::Embedded { offset } => {
             for line in 1..=lines {
-                writeln!(line_numbers, "<span>{0:1$}</span>", line + offset, cols)
+                writeln!(line_numbers, "<span>{0}</span>", line + offset)
             }
         }
     }
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css
index f1e0a89883a..9a4b382a304 100644
--- a/src/librustdoc/html/static/css/rustdoc.css
+++ b/src/librustdoc/html/static/css/rustdoc.css
@@ -541,6 +541,9 @@ h2.location a {
 	text-decoration: underline;
 }
 
+.line-numbers {
+	text-align: right;
+}
 .rustdoc:not(.source) .example-wrap > pre:not(.line-number) {
 	width: 100%;
 	overflow-x: auto;
diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml
index 375ff4878e5..ad7080c39b8 100644
--- a/src/test/rustdoc-gui/source-code-page.goml
+++ b/src/test/rustdoc-gui/source-code-page.goml
@@ -14,3 +14,6 @@ assert-attribute: (".line-numbers > span:nth-child(6)", {"class": "line-highligh
 assert-attribute-false: (".line-numbers > span:nth-child(7)", {"class": "line-highlighted"})
 // This is to ensure that the content is correctly align with the line numbers.
 compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))
+
+// Assert that the line numbers text is aligned to the right.
+assert-css: (".line-numbers", {"text-align": "right"})