|
86 | 86 |
|
87 | 87 | $effect(() => { |
88 | 88 | if (!markdown && view === 'js') { |
| 89 | + const highlight = (line: number, a: number[], b: number[]) => { |
| 90 | + const split = { |
| 91 | + original: workspace.current.contents.split('\n'), |
| 92 | + generated: current.result.js.code.split('\n') |
| 93 | + }; |
| 94 | +
|
| 95 | + const original = { |
| 96 | + start: split.original.slice(0, a[2]).join('\n').length + 1 + a[3], |
| 97 | + end: split.original.slice(0, b[2]).join('\n').length + 1 + b[3] |
| 98 | + }; |
| 99 | +
|
| 100 | + const generated = { |
| 101 | + start: split.generated.slice(0, line).join('\n').length + 1 + a[0], |
| 102 | + end: split.generated.slice(0, line).join('\n').length + 1 + b[0] |
| 103 | + }; |
| 104 | +
|
| 105 | + workspace.highlight_range(original); |
| 106 | + js_workspace.highlight_range(generated); |
| 107 | + }; |
| 108 | +
|
89 | 109 | workspace.onhover((pos) => { |
90 | 110 | if (!current?.result?.js.map) return; |
91 | 111 |
|
92 | | - const lines = decode(current.result.js.map.mappings); |
| 112 | + const mappings = decode(current.result.js.map.mappings); |
93 | 113 |
|
94 | 114 | const { line, column } = locate(workspace.current.contents, pos)!; |
95 | 115 |
|
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 | | -
|
| 116 | + for (let i = 0; i < mappings.length; i += 1) { |
| 117 | + const segments = mappings[i]; |
| 118 | + for (let j = 0; j < segments.length - 1; j += 1) { |
| 119 | + // segment is [generated_column, source_index, original_line, original_column] |
101 | 120 | const a = segments[j]; |
102 | | - const b = segments[j + 1] ?? lines[++bi][0]; |
| 121 | + const b = segments[j + 1]; |
103 | 122 |
|
104 | 123 | if (!b) continue; |
105 | 124 |
|
|
110 | 129 | if (b[2] === line && b[3] < column) continue; |
111 | 130 |
|
112 | 131 | // 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); |
| 132 | + highlight(i, a, b); |
130 | 133 | return; |
131 | 134 | } |
132 | 135 |
|
|
136 | 139 | }); |
137 | 140 |
|
138 | 141 | js_workspace.onhover((pos) => { |
139 | | - // TODO same in reverse |
| 142 | + if (!current?.result?.js.map) return; |
| 143 | +
|
| 144 | + const mappings = decode(current.result.js.map.mappings); |
| 145 | +
|
| 146 | + const { line, column } = locate(current.result.js.code, pos)!; |
| 147 | +
|
| 148 | + const segments = mappings[line]; |
| 149 | +
|
| 150 | + for (let i = 0; i < segments.length - 1; i += 1) { |
| 151 | + const a = segments[i]; |
| 152 | + const b = segments[i + 1]; |
| 153 | +
|
| 154 | + if (a[0] <= column && b[0] >= column) { |
| 155 | + highlight(line, a, b); |
| 156 | + return; |
| 157 | + } |
| 158 | +
|
| 159 | + workspace.highlight_range(null); |
| 160 | + js_workspace.highlight_range(null); |
| 161 | + } |
140 | 162 | }); |
141 | 163 | } |
142 | 164 | }); |
|
0 commit comments