Skip to content

Commit 61d7bb8

Browse files
committed
Implement highlightedInstruction support in the assembly view.
1 parent 2d7ffd5 commit 61d7bb8

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/components/shared/AssemblyView-codemirror.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
* width of the editor, it covers both the gutter and the main area.
2020
*/
2121
import { EditorView, gutter } from '@codemirror/view';
22-
import { EditorState, StateField, StateEffect } from '@codemirror/state';
22+
import {
23+
EditorState,
24+
StateField,
25+
StateEffect,
26+
Compartment,
27+
} from '@codemirror/state';
2328
import { syntaxHighlighting } from '@codemirror/language';
2429
import { classHighlighter } from '@lezer/highlight';
2530
import clamp from 'clamp';
@@ -37,12 +42,16 @@ import {
3742
timingsExtension,
3843
updateTimingsEffect,
3944
StringMarker,
45+
createHighlightedLineExtension,
4046
} from 'firefox-profiler/utils/codemirror-shared';
4147

4248
// An "effect" is like a redux action. This effect is used to replace the value
4349
// of the state field addressToLineMapField.
4450
const updateAddressToLineMapEffect = StateEffect.define<AddressToLineMap>();
4551

52+
// This "compartment" allows us to swap the highlighted line when it changes.
53+
const highlightedLineConf = new Compartment();
54+
4655
// This "state field" stores the current AddressToLineMap. This field allows the
4756
// instructionAddressGutter to map line numbers to addresses.
4857
const addressToLineMapField = StateField.define<AddressToLineMap>({
@@ -174,23 +183,31 @@ export class AssemblyViewEditor {
174183
_view: EditorView;
175184
_addressToLineMap: AddressToLineMap;
176185
_addressTimings: AddressTimings;
186+
_highlightedAddress: Address | null;
177187

178188
// Create a CodeMirror editor and add it as a child element of domParent.
179189
constructor(
180190
initialAssemblyCode: DecodedInstruction[],
181191
addressTimings: AddressTimings,
192+
highlightedAddress: Address | null,
182193
domParent: Element
183194
) {
184195
this._addressToLineMap = new AddressToLineMap(
185196
getInstructionAddresses(initialAssemblyCode)
186197
);
187198
this._addressTimings = addressTimings;
199+
this._highlightedAddress = highlightedAddress;
200+
const highlightedLine =
201+
highlightedAddress !== null
202+
? this._addressToLineMap.addressToLine(highlightedAddress)
203+
: null;
188204
let state = EditorState.create({
189205
doc: instructionsToText(initialAssemblyCode),
190206
extensions: [
191207
timingsExtension,
192208
addressToLineMapField,
193209
instructionAddressGutter,
210+
highlightedLineConf.of(createHighlightedLineExtension(highlightedLine)),
194211
syntaxHighlighting(classHighlighter),
195212
EditorState.readOnly.of(true),
196213
EditorView.editable.of(false),
@@ -220,6 +237,11 @@ export class AssemblyViewEditor {
220237
this._addressTimings,
221238
this._addressToLineMap
222239
);
240+
// Recalculate the highlighted line based on the new address-to-line mapping.
241+
const highlightedLine =
242+
this._highlightedAddress !== null
243+
? this._addressToLineMap.addressToLine(this._highlightedAddress)
244+
: null;
223245
// The CodeMirror way of replacing the entire contents is to insert new text
224246
// and overwrite the full range of existing text.
225247
const text = instructionsToText(assemblyCode);
@@ -236,6 +258,9 @@ export class AssemblyViewEditor {
236258
effects: [
237259
updateAddressToLineMapEffect.of(this._addressToLineMap),
238260
updateTimingsEffect.of(lineTimings),
261+
highlightedLineConf.reconfigure(
262+
createHighlightedLineExtension(highlightedLine)
263+
),
239264
],
240265
});
241266
}
@@ -280,4 +305,18 @@ export class AssemblyViewEditor {
280305
this.scrollToLine(lineNumber - topSpaceLines);
281306
}
282307
}
308+
309+
setHighlightedInstruction(address: Address | null) {
310+
// Store the highlighted address so we can recalculate the line number
311+
// when the address-to-line mapping changes.
312+
this._highlightedAddress = address;
313+
// Convert the address to a line number and update the highlighted line.
314+
const lineNumber =
315+
address !== null ? this._addressToLineMap.addressToLine(address) : null;
316+
this._view.dispatch({
317+
effects: highlightedLineConf.reconfigure(
318+
createHighlightedLineExtension(lineNumber)
319+
),
320+
});
321+
}
283322
}

src/components/shared/AssemblyView.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class AssemblyView extends React.PureComponent<AssemblyViewProps> {
119119
const editor = new AssemblyViewEditor(
120120
this._getAssemblyCodeOrFallback(),
121121
this.props.timings,
122+
this.props.highlightedInstruction ?? null,
122123
domParent
123124
);
124125
this._editor = editor;
@@ -164,5 +165,13 @@ export class AssemblyView extends React.PureComponent<AssemblyViewProps> {
164165
if (this.props.timings !== prevProps.timings) {
165166
this._editor.setTimings(this.props.timings);
166167
}
168+
169+
if (
170+
this.props.highlightedInstruction !== prevProps.highlightedInstruction
171+
) {
172+
this._editor.setHighlightedInstruction(
173+
this.props.highlightedInstruction ?? null
174+
);
175+
}
167176
}
168177
}

0 commit comments

Comments
 (0)