1919 * width of the editor, it covers both the gutter and the main area.
2020 */
2121import { 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' ;
2328import { syntaxHighlighting } from '@codemirror/language' ;
2429import { classHighlighter } from '@lezer/highlight' ;
2530import 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.
4450const 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.
4857const 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}
0 commit comments