From e89efb8634a1ffc8e7573b6f4cf53aa9b36d92ae Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 7 Mar 2022 12:04:51 +0100 Subject: [PATCH 1/2] Remove unneeded whitespace generation and use CSS instead instead to align line numbers to the right --- src/librustdoc/html/sources.rs | 10 ++-------- src/librustdoc/html/static/css/rustdoc.css | 3 +++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index bae04f2095a3d..9fba6e9116282 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("
");
     match source_context {
         SourceContext::Standalone => {
             for line in 1..=lines {
-                writeln!(line_numbers, "{0:1$}", line, cols)
+                writeln!(line_numbers, "{0}", line)
             }
         }
         SourceContext::Embedded { offset } => {
             for line in 1..=lines {
-                writeln!(line_numbers, "{0:1$}", line + offset, cols)
+                writeln!(line_numbers, "{0}", line + offset)
             }
         }
     }
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css
index f1e0a89883ab8..9a4b382a304ff 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;

From f23d6d3a47c15bc0fc3fbf2abdd318c3a2c09c7c Mon Sep 17 00:00:00 2001
From: Guillaume Gomez 
Date: Mon, 7 Mar 2022 12:08:14 +0100
Subject: [PATCH 2/2] Add GUI test to ensure that line numbers text is aligned
 to the right

---
 src/test/rustdoc-gui/source-code-page.goml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml
index 375ff4878e525..ad7080c39b842 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"})