Skip to content

Commit 7d1165c

Browse files
committed
Rollup merge of rust-lang#54921 - GuillaumeGomez:line-numbers, r=QuietMisdreavus
Add line numbers option to rustdoc Fixes rust-lang#22878.
2 parents 8419761 + e961d39 commit 7d1165c

File tree

6 files changed

+68
-14
lines changed

6 files changed

+68
-14
lines changed

src/librustdoc/html/highlight.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ use syntax::parse;
2828
use syntax_pos::{Span, FileName};
2929

3030
/// Highlights `src`, returning the HTML output.
31-
pub fn render_with_highlighting(src: &str, class: Option<&str>,
32-
extension: Option<&str>,
33-
tooltip: Option<(&str, &str)>) -> String {
31+
pub fn render_with_highlighting(
32+
src: &str,
33+
class: Option<&str>,
34+
extension: Option<&str>,
35+
tooltip: Option<(&str, &str)>,
36+
) -> String {
3437
debug!("highlighting: ================\n{}\n==============", src);
3538
let sess = parse::ParseSess::new(FilePathMapping::empty());
3639
let fm = sess.source_map().new_source_file(FileName::Custom("stdin".to_string()),
@@ -384,9 +387,9 @@ impl Class {
384387
}
385388

386389
fn write_header(class: Option<&str>, out: &mut dyn Write) -> io::Result<()> {
387-
write!(out, "<pre class=\"rust {}\">\n", class.unwrap_or(""))
390+
write!(out, "<div class=\"example-wrap\"><pre class=\"rust {}\">\n", class.unwrap_or(""))
388391
}
389392

390393
fn write_footer(out: &mut dyn Write) -> io::Result<()> {
391-
write!(out, "</pre>\n")
394+
write!(out, "</pre></div>\n")
392395
}

src/librustdoc/html/render.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,7 @@ impl<'a> Settings<'a> {
17091709
("method-docs", "Auto-hide item methods' documentation", false),
17101710
("go-to-only-result", "Directly go to item in search if there is only one result",
17111711
false),
1712+
("line-numbers", "Show line numbers on code examples", false),
17121713
],
17131714
root_path,
17141715
suffix,

src/librustdoc/html/static/main.js

+33-9
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@
20932093
return wrapper;
20942094
}
20952095

2096+
var hideItemDeclarations = getCurrentValue('rustdoc-item-declarations') === "false";
20962097
onEach(document.getElementsByClassName('docblock'), function(e) {
20972098
if (hasClass(e, 'autohide')) {
20982099
var wrap = e.previousElementSibling;
@@ -2116,16 +2117,14 @@
21162117
}
21172118
}
21182119
if (e.parentNode.id === "main") {
2119-
var otherMessage;
2120+
var otherMessage = '';
21202121
var fontSize;
21212122
var extraClass;
2122-
var show = true;
21232123

21242124
if (hasClass(e, "type-decl")) {
21252125
fontSize = "20px";
21262126
otherMessage = '&nbsp;Show&nbsp;declaration';
2127-
show = getCurrentValue('rustdoc-item-declarations') === "false";
2128-
if (!show) {
2127+
if (hideItemDeclarations === false) {
21292128
extraClass = 'collapsed';
21302129
}
21312130
} else if (hasClass(e, "non-exhaustive")) {
@@ -2142,8 +2141,12 @@
21422141
extraClass = "marg-left";
21432142
}
21442143

2145-
e.parentNode.insertBefore(createToggle(otherMessage, fontSize, extraClass, show), e);
2146-
if (otherMessage && show) {
2144+
e.parentNode.insertBefore(createToggle(otherMessage,
2145+
fontSize,
2146+
extraClass,
2147+
hideItemDeclarations),
2148+
e);
2149+
if (otherMessage.length > 0 && hideItemDeclarations === true) {
21472150
collapseDocs(e.previousSibling.childNodes[0], "toggle");
21482151
}
21492152
}
@@ -2186,13 +2189,33 @@
21862189
});
21872190
}
21882191

2192+
// To avoid checking on "rustdoc-item-attributes" value on every loop...
2193+
var itemAttributesFunc = function() {};
2194+
if (getCurrentValue("rustdoc-item-attributes") !== "false") {
2195+
itemAttributesFunc = function(x) {
2196+
collapseDocs(x.previousSibling.childNodes[0], "toggle");
2197+
};
2198+
}
21892199
onEach(document.getElementById('main').getElementsByClassName('attributes'), function(i_e) {
21902200
i_e.parentNode.insertBefore(createToggleWrapper(toggle.cloneNode(true)), i_e);
2191-
if (getCurrentValue("rustdoc-item-attributes") !== "false") {
2192-
collapseDocs(i_e.previousSibling.childNodes[0], "toggle");
2193-
}
2201+
itemAttributesFunc(i_e);
21942202
});
21952203

2204+
// To avoid checking on "rustdoc-line-numbers" value on every loop...
2205+
var lineNumbersFunc = function() {};
2206+
if (getCurrentValue("rustdoc-line-numbers") === "true") {
2207+
lineNumbersFunc = function(x) {
2208+
var count = x.textContent.split('\n').length;
2209+
var elems = [];
2210+
for (var i = 0; i < count; ++i) {
2211+
elems.push(i + 1);
2212+
}
2213+
var node = document.createElement('pre');
2214+
addClass(node, 'line-number');
2215+
node.innerHTML = elems.join('\n');
2216+
x.parentNode.insertBefore(node, x);
2217+
};
2218+
}
21962219
onEach(document.getElementsByClassName('rust-example-rendered'), function(e) {
21972220
if (hasClass(e, 'compile_fail')) {
21982221
e.addEventListener("mouseover", function(event) {
@@ -2209,6 +2232,7 @@
22092232
e.previousElementSibling.childNodes[0].style.color = '';
22102233
});
22112234
}
2235+
lineNumbersFunc(e);
22122236
});
22132237

22142238
function showModal(content) {

src/librustdoc/html/static/rustdoc.css

+18
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ nav.sub {
282282
padding-left: 0;
283283
}
284284

285+
.example-wrap {
286+
display: inline-flex;
287+
width: 100%;
288+
}
289+
290+
.example-wrap > pre.line-number {
291+
overflow: initial;
292+
border: 1px solid;
293+
border-top-left-radius: 5px;
294+
border-bottom-left-radius: 5px;
295+
padding: 13px 8px;
296+
text-align: right;
297+
}
298+
299+
.example-wrap > pre.rust {
300+
width: 100%;
301+
}
302+
285303
#search {
286304
margin-left: 230px;
287305
position: relative;

src/librustdoc/html/static/themes/dark.css

+4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ pre.rust .question-mark {
233233
color: #ff9011;
234234
}
235235

236+
.example-wrap > pre.line-number {
237+
border-color: #4a4949;
238+
}
239+
236240
a.test-arrow {
237241
background-color: rgba(78, 139, 202, 0.2);
238242
}

src/librustdoc/html/static/themes/light.css

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ pre.rust .question-mark {
227227
color: #ff9011;
228228
}
229229

230+
.example-wrap > pre.line-number {
231+
border-color: #c7c7c7;
232+
}
233+
230234
a.test-arrow {
231235
background-color: rgba(78, 139, 202, 0.2);
232236
}

0 commit comments

Comments
 (0)