Skip to content

Commit ad1f44a

Browse files
committed
highlight JS output on editor hover
1 parent cafd011 commit ad1f44a

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

packages/repl/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"editor": "workspace:*",
8484
"esm-env": "^1.0.0",
8585
"esrap": "^1.2.2",
86+
"locate-character": "^3.0.0",
8687
"marked": "^14.1.2",
8788
"resolve.exports": "^2.0.2",
8889
"svelte": "5.1.11",

packages/repl/src/lib/Output/Output.svelte

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { marked } from 'marked';
3+
import { locate } from 'locate-character';
34
import AstView from './AstView.svelte';
45
import CompilerOptions from './CompilerOptions.svelte';
56
import PaneWithPanel from './PaneWithPanel.svelte';
@@ -90,10 +91,47 @@
9091
9192
const lines = decode(current.result.js.map.mappings);
9293
93-
// TODO find the `line`/`column` corresponding to `pos`
94+
const { line, column } = locate(workspace.current.contents, pos)!;
9495
95-
for (const segments of lines) {
96-
// TODO find the segment that corresponds to `line`/`column`
96+
for (let ai = 0; ai < lines.length; ai += 1) {
97+
const segments = lines[ai];
98+
for (let j = 0; j < segments.length; j += 1) {
99+
let bi = ai;
100+
101+
const a = segments[j];
102+
const b = segments[j + 1] ?? lines[++bi][0];
103+
104+
if (!b) continue;
105+
106+
if (a[2] > line) continue;
107+
if (b[2] < line) continue;
108+
109+
if (a[2] === line && a[3] > column) continue;
110+
if (b[2] === line && b[3] < column) continue;
111+
112+
// if we're still here, we have a match
113+
const split = {
114+
original: workspace.current.contents.split('\n'),
115+
generated: current.result.js.code.split('\n')
116+
};
117+
118+
const original = {
119+
start: split.original.slice(0, a[2]).join('\n').length + 1 + a[3],
120+
end: split.original.slice(0, b[2]).join('\n').length + 1 + b[3]
121+
};
122+
123+
const generated = {
124+
start: split.generated.slice(0, ai).join('\n').length + 1 + a[0],
125+
end: split.generated.slice(0, bi).join('\n').length + 1 + b[0]
126+
};
127+
128+
workspace.highlight_range(original);
129+
js_workspace.highlight_range(generated);
130+
return;
131+
}
132+
133+
workspace.highlight_range(null);
134+
js_workspace.highlight_range(null);
97135
}
98136
});
99137

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)