Make the line numbers of the source code clickable
This commit is contained in:
parent
9f244dc97a
commit
739f74bb15
3 changed files with 49 additions and 8 deletions
|
@ -2249,9 +2249,9 @@ impl<'a> fmt::Show for Source<'a> {
|
||||||
cols += 1;
|
cols += 1;
|
||||||
tmp /= 10;
|
tmp /= 10;
|
||||||
}
|
}
|
||||||
try!(write!(fmt, "<pre class='line-numbers'>"));
|
try!(write!(fmt, "<pre class=\"line-numbers\">"));
|
||||||
for i in range(1, lines + 1) {
|
for i in range(1, lines + 1) {
|
||||||
try!(write!(fmt, "<span id='{0}'>{0:1$}</span>\n", i, cols));
|
try!(write!(fmt, "<span id=\"{0}\">{0:1$}</span>\n", i, cols));
|
||||||
}
|
}
|
||||||
try!(write!(fmt, "</pre>"));
|
try!(write!(fmt, "</pre>"));
|
||||||
try!(write!(fmt, "{}", highlight::highlight(s.as_slice(), None, None)));
|
try!(write!(fmt, "{}", highlight::highlight(s.as_slice(), None, None)));
|
||||||
|
|
|
@ -157,6 +157,7 @@ nav.sub {
|
||||||
left: 0;
|
left: 0;
|
||||||
top: 0;
|
top: 0;
|
||||||
min-height: 100%;
|
min-height: 100%;
|
||||||
|
z-index: -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.content, nav { max-width: 960px; }
|
.content, nav { max-width: 960px; }
|
||||||
|
@ -217,10 +218,18 @@ nav.sub {
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
padding-left: 0;
|
padding-left: 0;
|
||||||
}
|
}
|
||||||
.content pre.line-numbers { float: left; border: none; }
|
.content pre.line-numbers {
|
||||||
.line-numbers span { color: #c67e2d; }
|
float: left;
|
||||||
|
border: none;
|
||||||
|
|
||||||
|
-webkit-user-select: none;
|
||||||
|
-moz-user-select: none;
|
||||||
|
-ms-user-select: none;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
.line-numbers span { color: #c67e2d; cursor: pointer; }
|
||||||
.line-numbers .line-highlighted {
|
.line-numbers .line-highlighted {
|
||||||
background-color: #f6fdb0;
|
background-color: #f6fdb0 !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.content .highlighted {
|
.content .highlighted {
|
||||||
|
@ -465,6 +474,7 @@ h1 .stability {
|
||||||
.summary.Unmarked { background-color: #BBBBBB; }
|
.summary.Unmarked { background-color: #BBBBBB; }
|
||||||
|
|
||||||
:target { background: #FDFFD3; }
|
:target { background: #FDFFD3; }
|
||||||
|
.line-numbers :target { background-color: transparent; }
|
||||||
|
|
||||||
/* Code highlighting */
|
/* Code highlighting */
|
||||||
pre.rust .kw { color: #8959A8; }
|
pre.rust .kw { color: #8959A8; }
|
||||||
|
|
|
@ -50,7 +50,7 @@
|
||||||
resizeShortBlocks();
|
resizeShortBlocks();
|
||||||
$(window).on('resize', resizeShortBlocks);
|
$(window).on('resize', resizeShortBlocks);
|
||||||
|
|
||||||
function highlightSourceLines() {
|
function highlightSourceLines(ev) {
|
||||||
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
|
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
|
||||||
if (match) {
|
if (match) {
|
||||||
from = parseInt(match[1], 10);
|
from = parseInt(match[1], 10);
|
||||||
|
@ -59,14 +59,14 @@
|
||||||
if ($('#' + from).length === 0) {
|
if ($('#' + from).length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$('#' + from)[0].scrollIntoView();
|
if (ev === null) $('#' + from)[0].scrollIntoView();
|
||||||
$('.line-numbers span').removeClass('line-highlighted');
|
$('.line-numbers span').removeClass('line-highlighted');
|
||||||
for (i = from; i <= to; ++i) {
|
for (i = from; i <= to; ++i) {
|
||||||
$('#' + i).addClass('line-highlighted');
|
$('#' + i).addClass('line-highlighted');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
highlightSourceLines();
|
highlightSourceLines(null);
|
||||||
$(window).on('hashchange', highlightSourceLines);
|
$(window).on('hashchange', highlightSourceLines);
|
||||||
|
|
||||||
$(document).on('keyup', function(e) {
|
$(document).on('keyup', function(e) {
|
||||||
|
@ -778,4 +778,35 @@
|
||||||
$("#main > .docblock").before(wrapper);
|
$("#main > .docblock").before(wrapper);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$('pre.line-numbers').on('click', 'span', function() {
|
||||||
|
var prev_id = 0;
|
||||||
|
|
||||||
|
function set_fragment(name) {
|
||||||
|
if (history.replaceState) {
|
||||||
|
history.replaceState(null, null, '#' + name);
|
||||||
|
$(window).trigger('hashchange');
|
||||||
|
} else {
|
||||||
|
location.replace('#' + name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return function(ev) {
|
||||||
|
var cur_id = parseInt(ev.target.id);
|
||||||
|
|
||||||
|
if (ev.shiftKey && prev_id) {
|
||||||
|
if (prev_id > cur_id) {
|
||||||
|
var tmp = prev_id;
|
||||||
|
prev_id = cur_id;
|
||||||
|
cur_id = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
set_fragment(prev_id + '-' + cur_id);
|
||||||
|
} else {
|
||||||
|
prev_id = cur_id;
|
||||||
|
|
||||||
|
set_fragment(cur_id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}());
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue