Skip to content

Commit e5c03ae

Browse files
xFrednetsyphar
authored andcommitted
Add line numbers to crate/**/source/
1 parent e1852e1 commit e5c03ae

File tree

5 files changed

+144
-15
lines changed

5 files changed

+144
-15
lines changed

static/source.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,95 @@
3838
toggleSource(toggleSourceButton);
3939
});
4040
});
41+
42+
// This code has been adapted from the rustdoc implementation here:
43+
// https://github.com/rust-lang/rust/blob/5c848860/src/librustdoc/html/static/js/src-script.js#L152-L204
44+
function highlightLineNumbers() {
45+
const match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
46+
if (!match) {
47+
return;
48+
}
49+
let from = parseInt(match[1], 10);
50+
let to = from;
51+
if (typeof match[2] !== "undefined") {
52+
to = parseInt(match[2], 10);
53+
}
54+
if (to < from) {
55+
const tmp = to;
56+
to = from;
57+
from = tmp;
58+
}
59+
let elem = document.getElementById(from);
60+
if (!elem) {
61+
return;
62+
}
63+
const x = document.getElementById(from);
64+
if (x) {
65+
x.scrollIntoView();
66+
}
67+
Array.from(document.getElementsByClassName("line-number-highlighted")).forEach(e => {
68+
e.classList.remove("line-number-highlighted");
69+
});
70+
for (let i = from; i <= to; ++i) {
71+
elem = document.getElementById(i);
72+
if (!elem) {
73+
break;
74+
}
75+
elem.classList.add("line-number-highlighted");
76+
}
77+
}
78+
79+
const handleLineNumbers = (function () {
80+
let prev_line_id = 0;
81+
82+
const set_fragment = name => {
83+
const x = window.scrollX,
84+
y = window.scrollY;
85+
if (window.history && typeof window.history.pushState === "function") {
86+
history.replaceState(null, null, "#" + name);
87+
highlightLineNumbers();
88+
} else {
89+
location.replace("#" + name);
90+
}
91+
// Prevent jumps when selecting one or many lines
92+
window.scrollTo(x, y);
93+
};
94+
95+
return ev => {
96+
let cur_line_id = parseInt(ev.target.id, 10);
97+
// This event handler is attached to the entire line number column, but it should only
98+
// be run if one of the anchors is clicked. It also shouldn't do anything if the anchor
99+
// is clicked with a modifier key (to open a new browser tab).
100+
if (isNaN(cur_line_id) ||
101+
ev.ctrlKey ||
102+
ev.altKey ||
103+
ev.metaKey) {
104+
return;
105+
}
106+
ev.preventDefault();
107+
108+
if (ev.shiftKey && prev_line_id) {
109+
// Swap selection if needed
110+
if (prev_line_id > cur_line_id) {
111+
const tmp = prev_line_id;
112+
prev_line_id = cur_line_id;
113+
cur_line_id = tmp;
114+
}
115+
116+
set_fragment(prev_line_id + "-" + cur_line_id);
117+
} else {
118+
prev_line_id = cur_line_id;
119+
120+
set_fragment(cur_line_id);
121+
}
122+
};
123+
}());
124+
125+
window.addEventListener("hashchange", highlightLineNumbers)
126+
127+
Array.from(document.getElementById("line-numbers").children[0].children).forEach(el => {
128+
el.addEventListener("click", handleLineNumbers);
129+
});
130+
131+
highlightLineNumbers();
41132
})();

templates/crate/source.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,15 @@
127127
{% else %}
128128
{% set file_name = "" %}
129129
{% endif %}
130-
<div id="source-code" class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
131-
{{- file_content|highlight(file_name)|safe -}}
130+
<div id="source-code-container" class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
131+
<div data-nosnippet class="source-code"><pre id="line-numbers"><code>
132+
{%- for line in 1..=file_content.lines().count() -%}
133+
<a href="#{{line|safe}}" id="{{line|safe}}">{{line|safe}}</a>
134+
{%~ endfor -%}
135+
</code></pre></div>
136+
<div id="source-code" class="source-code">
137+
{{- file_content|highlight(file_name)|safe -}}
138+
</div>
132139
</div>
133140
{%- endif -%}
134141
</div>

templates/style/_syntax-themes.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ html {
1515
--color-syntax-question-mark: #ff9011;
1616
--color-syntax-self: #c82829;
1717
--color-syntax-string: #718c00;
18+
--color-line-number: #c67e2d;
19+
--color-line-number-highlighted: #fdffd3;
1820
}
1921

2022
// To add a new theme, copy the above theme into a new `html[data-docs-rs-theme="name"]`
@@ -37,6 +39,8 @@ html[data-docs-rs-theme="dark"] {
3739
--color-syntax-question-mark: #ff9011;
3840
--color-syntax-self: #ee6868;
3941
--color-syntax-string: #83a300;
42+
--color-line-number: #3b91e2;
43+
--color-line-number-highlighted: #0a042f;
4044
}
4145

4246
html[data-docs-rs-theme="ayu"] {
@@ -56,4 +60,6 @@ html[data-docs-rs-theme="ayu"] {
5660
--color-syntax-question-mark: #ff9011;
5761
--color-syntax-self: #36a3d9;
5862
--color-syntax-string: #b8cc52;
63+
--color-line-number: #708090;
64+
--color-line-number-highlighted: #272b2e;
5965
}

templates/style/_syntax.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
@import "syntax-themes";
22

3+
#line-numbers > code > a, #line-numbers > code > a:visited {
4+
color: var(--color-line-number);
5+
}
6+
7+
.line-number-highlighted {
8+
background-color: var(--color-line-number-highlighted);
9+
}
10+
311
pre > code {
412
color: var(--color-syntax-foreground);
513
background-color: var(--color-syntax-background);

templates/style/style.scss

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -886,23 +886,40 @@ ul.pure-menu-list {
886886
}
887887
}
888888

889-
#source-code {
890-
pre {
891-
margin-top: 0;
892-
margin-bottom: 0;
893-
height: 100%;
889+
#source-warning {
890+
height: 100%;
891+
}
892+
}
894893

895-
code {
896-
height: 100%;
897-
}
898-
}
894+
#source-code-container {
895+
display: inline-flex;
896+
overflow: scroll;
897+
}
898+
#line-numbers {
899+
text-align: right;
900+
letter-spacing: normal;
901+
}
902+
#line-numbers > code > a {
903+
padding: 0 8px;
904+
}
905+
// This class is used to the source code and the line number container in the
906+
// `crate/**/source/*` view
907+
.source-code {
908+
pre {
909+
margin-top: 0;
910+
margin-bottom: 0;
911+
height: 100%;
899912

900-
&.expanded {
901-
width: calc(100% - 46px);
913+
code {
914+
height: 100%;
902915
}
903916
}
904917

905-
#source-warning {
906-
height: 100%;
918+
&.expanded {
919+
width: calc(100% - 46px);
907920
}
908921
}
922+
#source-code {
923+
overflow: scroll;
924+
width: 100%;
925+
}

0 commit comments

Comments
 (0)