diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index f631f627fc255caeab1105a64243f8f2dfb1151f..51392ca1191891a7dc3932f909bb4e5e9efa62eb 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -24,6 +24,7 @@ playground_button: Option<&str>, tooltip: Option<(Option, &str)>, edition: Edition, + extra_content: Option, ) { debug!("highlighting: ================\n{}\n==============", src); if let Some((edition_info, class)) = tooltip { @@ -39,13 +40,21 @@ ); } - write_header(out, class); + write_header(out, class, extra_content); write_code(out, &src, edition); write_footer(out, playground_button); } -fn write_header(out: &mut Buffer, class: Option<&str>) { - writeln!(out, "
", class.unwrap_or_default());
+fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option) {
+    write!(out, "
"); + if let Some(extra) = extra_content { + out.push_buffer(extra); + } + if let Some(class) = class { + writeln!(out, "
", class);
+    } else {
+        writeln!(out, "
");
+    }
 }
 
 fn write_code(out: &mut Buffer, src: &str, edition: Edition) {
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 509f1730557756ee7b1cc92d569eb45a4c1b1639..c2b40ab34e2551c1062525c3ce6d7f33c45705c4 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -315,6 +315,7 @@ fn dont_escape(c: u8) -> bool {
             playground_button.as_deref(),
             tooltip,
             edition,
+            None,
         );
         Some(Event::Html(s.into_inner().into()))
     }
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index a08a340e3a999a72559dc03ee5cac9bcd4572126..7ccc313cc5905ef6c6486f33af86d43f80dd38bb 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -1026,6 +1026,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
             None,
             None,
             it.span(cx.tcx()).inner().edition(),
+            None,
         );
     });
     document(w, cx, it, None)
diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs
index 14e2d65d94ef8e84df2462801d57eb655488875a..57c33f94918d71fd097ddf80609814ca0e406504 100644
--- a/src/librustdoc/html/sources.rs
+++ b/src/librustdoc/html/sources.rs
@@ -169,16 +169,17 @@ fn emit_source(&mut self, filename: &FileName) -> Result<(), Error> {
 /// adding line numbers to the left-hand side.
 fn print_src(buf: &mut Buffer, s: &str, edition: Edition) {
     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;
     }
-    buf.write_str("
");
+    line_numbers.write_str("
");
     for i in 1..=lines {
-        writeln!(buf, "{0:1$}", i, cols);
+        writeln!(line_numbers, "{0:1$}", i, cols);
     }
-    buf.write_str("
"); - highlight::render_with_highlighting(s, buf, None, None, None, edition); + line_numbers.write_str("
"); + highlight::render_with_highlighting(s, buf, None, None, None, edition, Some(line_numbers)); } diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 9e776f83c8d1b62654894eac71d4c8c88673ec39..aaa2525644f117fa2318e26f5fb4d1c09c39c36c 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -206,7 +206,6 @@ li { max-width: none; overflow: visible; margin-left: 0px; - min-width: 70em; } nav.sub { @@ -357,7 +356,7 @@ nav.sub { padding-left: 0; } -.rustdoc:not(.source) .example-wrap { +.rustdoc .example-wrap { display: inline-flex; margin-bottom: 10px; } @@ -370,8 +369,6 @@ nav.sub { .example-wrap > pre.line-number { overflow: initial; border: 1px solid; - border-top-left-radius: 5px; - border-bottom-left-radius: 5px; padding: 13px 8px; text-align: right; } @@ -381,7 +378,7 @@ nav.sub { overflow-x: auto; } -.rustdoc:not(.source) .example-wrap > pre { +.rustdoc .example-wrap > pre { margin: 0; } @@ -395,15 +392,14 @@ nav.sub { table-layout: fixed; } -.content pre.line-numbers { - float: left; - border: none; +.content > .example-wrap pre.line-numbers { position: relative; - -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; + border-top-left-radius: 5px; + border-bottom-left-radius: 5px; } .line-numbers span { cursor: pointer; diff --git a/src/librustdoc/html/static/themes/ayu.css b/src/librustdoc/html/static/themes/ayu.css index 63d42264f76cf95a71f63833614cca2c84f52827..aafb7f6300ea476aee1b7bc098401e68cd75eb18 100644 --- a/src/librustdoc/html/static/themes/ayu.css +++ b/src/librustdoc/html/static/themes/ayu.css @@ -53,7 +53,7 @@ span code { .docblock code, .docblock-short code { background-color: #191f26; } -pre { +pre, .rustdoc.source .example-wrap { color: #e6e1cf; background-color: #191f26; } diff --git a/src/librustdoc/html/static/themes/dark.css b/src/librustdoc/html/static/themes/dark.css index c23e95ce107a9431410efedb309d947a72225d65..715605d7b3785cfa0ea4dd962f40168915afb086 100644 --- a/src/librustdoc/html/static/themes/dark.css +++ b/src/librustdoc/html/static/themes/dark.css @@ -26,7 +26,7 @@ h4:not(.method):not(.type):not(.tymethod) { .docblock code, .docblock-short code { background-color: #2A2A2A; } -pre { +pre, .rustdoc.source .example-wrap { background-color: #2A2A2A; } diff --git a/src/librustdoc/html/static/themes/light.css b/src/librustdoc/html/static/themes/light.css index 9330972121073b31b29705c793f806748b94af38..60ed8898793875504b60ac54d027699f55c7473d 100644 --- a/src/librustdoc/html/static/themes/light.css +++ b/src/librustdoc/html/static/themes/light.css @@ -28,7 +28,7 @@ h4:not(.method):not(.type):not(.tymethod) { .docblock code, .docblock-short code { background-color: #F5F5F5; } -pre { +pre, .rustdoc.source .example-wrap { background-color: #F5F5F5; } diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml new file mode 100644 index 0000000000000000000000000000000000000000..f11c41e8bd552eb870377696b5abe25b5669ebaa --- /dev/null +++ b/src/test/rustdoc-gui/source-code-page.goml @@ -0,0 +1,13 @@ +goto: file://|DOC_PATH|/../src/test_docs/lib.rs.html +// Check that we can click on the line number. +click: (40, 224) // This is the position of the span for line 4. +// Unfortunately, "#4" isn't a valid query selector, so we have to go around that limitation +// by instead getting the nth span. +assert: (".line-numbers > span:nth-child(4)", "class", "line-highlighted") +// We now check that the good spans are highlighted +goto: file://|DOC_PATH|/../src/test_docs/lib.rs.html#4-6 +assert-false: (".line-numbers > span:nth-child(3)", "class", "line-highlighted") +assert: (".line-numbers > span:nth-child(4)", "class", "line-highlighted") +assert: (".line-numbers > span:nth-child(5)", "class", "line-highlighted") +assert: (".line-numbers > span:nth-child(6)", "class", "line-highlighted") +assert-false: (".line-numbers > span:nth-child(7)", "class", "line-highlighted")