Skip to content

Commit e918a58

Browse files
committed
rollup merge of rust-lang#20092: barosl/rustdoc-line-number-clickable
While talking on IRC, someone wanted to post a link to the Rust source code, but while the lines of the rendered source code do have anchors (`<span id="[line number]">`), there is no convenient way to make links as they are not clickable. This PR makes them clickable. Also, a minor fix of the FAQ is included.
2 parents b8e404f + 739f74b commit e918a58

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

src/doc/complement-lang-faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Some examples that demonstrate different aspects of the language:
1717
* [sprocketnes], an NES emulator with no GC, using modern Rust conventions
1818
* The language's general-purpose [hash] function, SipHash-2-4. Bit twiddling, OO, macros
1919
* The standard library's [HashMap], a sendable hash map in an OO style
20-
* The extra library's [json] module. Enums and pattern matching
20+
* The standard library's [json] module. Enums and pattern matching
2121

2222
[sprocketnes]: https://github.com/pcwalton/sprocketnes
2323
[hash]: https://github.com/rust-lang/rust/blob/master/src/libstd/hash/mod.rs

src/librustdoc/html/render.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2253,9 +2253,9 @@ impl<'a> fmt::Show for Source<'a> {
22532253
cols += 1;
22542254
tmp /= 10;
22552255
}
2256-
try!(write!(fmt, "<pre class='line-numbers'>"));
2256+
try!(write!(fmt, "<pre class=\"line-numbers\">"));
22572257
for i in range(1, lines + 1) {
2258-
try!(write!(fmt, "<span id='{0}'>{0:1$}</span>\n", i, cols));
2258+
try!(write!(fmt, "<span id=\"{0}\">{0:1$}</span>\n", i, cols));
22592259
}
22602260
try!(write!(fmt, "</pre>"));
22612261
try!(write!(fmt, "{}", highlight::highlight(s.as_slice(), None, None)));

src/librustdoc/html/static/main.css

+13-3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ nav.sub {
157157
left: 0;
158158
top: 0;
159159
min-height: 100%;
160+
z-index: -1;
160161
}
161162

162163
.content, nav { max-width: 960px; }
@@ -217,10 +218,18 @@ nav.sub {
217218
overflow: auto;
218219
padding-left: 0;
219220
}
220-
.content pre.line-numbers { float: left; border: none; }
221-
.line-numbers span { color: #c67e2d; }
221+
.content pre.line-numbers {
222+
float: left;
223+
border: none;
224+
225+
-webkit-user-select: none;
226+
-moz-user-select: none;
227+
-ms-user-select: none;
228+
user-select: none;
229+
}
230+
.line-numbers span { color: #c67e2d; cursor: pointer; }
222231
.line-numbers .line-highlighted {
223-
background-color: #f6fdb0;
232+
background-color: #f6fdb0 !important;
224233
}
225234

226235
.content .highlighted {
@@ -470,6 +479,7 @@ h1 .stability {
470479
.summary.Unmarked { background-color: #BBBBBB; }
471480

472481
:target { background: #FDFFD3; }
482+
.line-numbers :target { background-color: transparent; }
473483

474484
/* Code highlighting */
475485
pre.rust .kw { color: #8959A8; }

src/librustdoc/html/static/main.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
resizeShortBlocks();
5151
$(window).on('resize', resizeShortBlocks);
5252

53-
function highlightSourceLines() {
53+
function highlightSourceLines(ev) {
5454
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
5555
if (match) {
5656
from = parseInt(match[1], 10);
@@ -59,14 +59,14 @@
5959
if ($('#' + from).length === 0) {
6060
return;
6161
}
62-
$('#' + from)[0].scrollIntoView();
62+
if (ev === null) $('#' + from)[0].scrollIntoView();
6363
$('.line-numbers span').removeClass('line-highlighted');
6464
for (i = from; i <= to; ++i) {
6565
$('#' + i).addClass('line-highlighted');
6666
}
6767
}
6868
}
69-
highlightSourceLines();
69+
highlightSourceLines(null);
7070
$(window).on('hashchange', highlightSourceLines);
7171

7272
$(document).on('keyup', function(e) {
@@ -778,4 +778,35 @@
778778
$("#main > .docblock").before(wrapper);
779779
});
780780

781+
$('pre.line-numbers').on('click', 'span', function() {
782+
var prev_id = 0;
783+
784+
function set_fragment(name) {
785+
if (history.replaceState) {
786+
history.replaceState(null, null, '#' + name);
787+
$(window).trigger('hashchange');
788+
} else {
789+
location.replace('#' + name);
790+
}
791+
}
792+
793+
return function(ev) {
794+
var cur_id = parseInt(ev.target.id);
795+
796+
if (ev.shiftKey && prev_id) {
797+
if (prev_id > cur_id) {
798+
var tmp = prev_id;
799+
prev_id = cur_id;
800+
cur_id = tmp;
801+
}
802+
803+
set_fragment(prev_id + '-' + cur_id);
804+
} else {
805+
prev_id = cur_id;
806+
807+
set_fragment(cur_id);
808+
}
809+
};
810+
}());
811+
781812
}());

0 commit comments

Comments
 (0)