Skip to content

Commit

Permalink
Rollup merge of rust-lang#85148 - GuillaumeGomez:source-code-line-num…
Browse files Browse the repository at this point in the history
…ber, r=jsha

Fix source code line number display and make it clickable again

Fixes rust-lang#85119.

I used the same logic we're using for other codeblocks: putting the line number `<span>`s into the `example-wrap` directly and then add `display: inline-flex` on `example-wrap`.

r? `@jsha`
  • Loading branch information
GuillaumeGomez committed May 10, 2021
2 parents c7e7de4 + 4e3fb68 commit 3962541
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 19 deletions.
15 changes: 12 additions & 3 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ crate fn render_with_highlighting(
playground_button: Option<&str>,
tooltip: Option<(Option<Edition>, &str)>,
edition: Edition,
extra_content: Option<Buffer>,
) {
debug!("highlighting: ================\n{}\n==============", src);
if let Some((edition_info, class)) = tooltip {
Expand All @@ -39,13 +40,21 @@ crate fn render_with_highlighting(
);
}

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, "<div class=\"example-wrap\"><pre class=\"rust {}\">", class.unwrap_or_default());
fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buffer>) {
write!(out, "<div class=\"example-wrap\">");
if let Some(extra) = extra_content {
out.push_buffer(extra);
}
if let Some(class) = class {
writeln!(out, "<pre class=\"rust {}\">", class);
} else {
writeln!(out, "<pre class=\"rust\">");
}
}

fn write_code(out: &mut Buffer, src: &str, edition: Edition) {
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
playground_button.as_deref(),
tooltip,
edition,
None,
);
Some(Event::Html(s.into_inner().into()))
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,17 @@ where
/// 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("<pre class=\"line-numbers\">");
line_numbers.write_str("<pre class=\"line-numbers\">");
for i in 1..=lines {
writeln!(buf, "<span id=\"{0}\">{0:1$}</span>", i, cols);
writeln!(line_numbers, "<span id=\"{0}\">{0:1$}</span>", i, cols);
}
buf.write_str("</pre>");
highlight::render_with_highlighting(s, buf, None, None, None, edition);
line_numbers.write_str("</pre>");
highlight::render_with_highlighting(s, buf, None, None, None, edition, Some(line_numbers));
}
14 changes: 5 additions & 9 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ li {
max-width: none;
overflow: visible;
margin-left: 0px;
min-width: 70em;
}

nav.sub {
Expand Down Expand Up @@ -357,7 +356,7 @@ nav.sub {
padding-left: 0;
}

.rustdoc:not(.source) .example-wrap {
.rustdoc .example-wrap {
display: inline-flex;
margin-bottom: 10px;
}
Expand All @@ -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;
}
Expand All @@ -381,7 +378,7 @@ nav.sub {
overflow-x: auto;
}

.rustdoc:not(.source) .example-wrap > pre {
.rustdoc .example-wrap > pre {
margin: 0;
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/rustdoc-gui/source-code-page.goml
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 3962541

Please sign in to comment.