Skip to content

Commit d50cbf7

Browse files
committed
Allow clicking on source links in the MIR output
1 parent ab9c098 commit d50cbf7

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

ui/frontend/Output.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Execute from './Output/Execute';
99
import Gist from './Output/Gist';
1010
import Section from './Output/Section';
1111
import SimplePane, { SimplePaneProps } from './Output/SimplePane';
12+
import PaneWithMir from './Output/PaneWithMir';
1213
import * as selectors from './selectors';
1314

1415
const Tab: React.SFC<TabProps> = ({ kind, focus, label, onClick, tabProps }) => {
@@ -82,7 +83,7 @@ const Output: React.SFC = () => {
8283
{focus === Focus.MacroExpansion && <SimplePane {...macroExpansion} kind="macro-expansion" />}
8384
{focus === Focus.Asm && <PaneWithCode {...assembly} kind="asm" />}
8485
{focus === Focus.LlvmIr && <PaneWithCode {...llvmIr} kind="llvm-ir" />}
85-
{focus === Focus.Mir && <PaneWithCode {...mir} kind="mir" />}
86+
{focus === Focus.Mir && <PaneWithMir {...mir} kind="mir" />}
8687
{focus === Focus.Wasm && <PaneWithCode {...wasm} kind="wasm" />}
8788
{focus === Focus.Gist && <Gist />}
8889
</div>

ui/frontend/Output/PaneWithMir.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import { PrismCode } from 'react-prism';
3+
4+
import Header from './Header';
5+
import SimplePane, { SimplePaneProps } from './SimplePane';
6+
7+
interface PaneWithMirProps extends SimplePaneProps {
8+
code?: string;
9+
}
10+
11+
const PaneWithMir: React.SFC<PaneWithMirProps> = ({ code, ...rest }) => (
12+
<SimplePane {...rest}>
13+
<div className="output-result">
14+
<Header label="Result" />
15+
<pre>
16+
<PrismCode className="language-rust_mir">
17+
{code}
18+
</PrismCode>
19+
</pre>
20+
</div>
21+
</SimplePane>
22+
);
23+
24+
export default PaneWithMir;

ui/frontend/highlighting.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export function configureRustErrors({
4848
'backtrace-enable': /Run with `RUST_BACKTRACE=1` environment variable to display a backtrace/i,
4949
};
5050

51+
Prism.languages.rust_mir = { // eslint-disable-line @typescript-eslint/camelcase
52+
'mir-source': /src\/.*.rs:\d+:\d+: \d+:\d+/,
53+
}
54+
5155
Prism.hooks.add('wrap', env => {
5256
if (env.type === 'error-explanation') {
5357
const errorMatch = /E\d+/.exec(env.content);
@@ -104,6 +108,16 @@ export function configureRustErrors({
104108
env.attributes['data-line'] = line;
105109
env.attributes['data-col'] = '1';
106110
}
111+
if (env.type === 'mir-source') {
112+
const lineMatch = /(\d+):(\d+): (\d+):(\d+)/.exec(env.content);
113+
const [_, startLine, startCol, endLine, endCol] = lineMatch;
114+
env.tag = 'a';
115+
env.attributes.href = '#';
116+
env.attributes['data-start-line'] = startLine;
117+
env.attributes['data-start-col'] = startCol;
118+
env.attributes['data-end-line'] = endLine;
119+
env.attributes['data-end-col'] = endCol;
120+
}
107121
});
108122

109123
Prism.hooks.add('after-highlight', env => {
@@ -141,5 +155,14 @@ export function configureRustErrors({
141155
reExecuteWithBacktrace();
142156
};
143157
});
158+
159+
const mirSourceLinks = env.element.querySelectorAll('.mir-source');
160+
Array.from(mirSourceLinks).forEach((link: HTMLAnchorElement) => {
161+
const { startLine, startCol } = link.dataset;
162+
link.onclick = e => {
163+
e.preventDefault();
164+
gotoPosition(startLine, startCol);
165+
};
166+
});
144167
});
145168
}

0 commit comments

Comments
 (0)