import { Position } from '@sourcegraph/extension-api-types' ; import { convertNode } from '../token_position' ; import { CodeViewProps } from './dom' ; interface Coordinates { x : number ; y : number ; } export const createMouseEvent = ( type , coords ) : MouseEvent => { const event = new MouseEvent ( type , { clientX : coords . x , clientY : coords . y , bubbles : true , } ) ; return event ; } ; const invalidPosition = ( { line , character } , message ) : string => `Invalid position L${ line }:${ character }. ${ message }. Positions are 0-indexed.` ; param eventType param codeViewProps param position export const dispatchMouseEventAtPositionImpure = ( eventType , { codeView , getCodeElementFromLineNumber } , position ) : void => { const line = getCodeElementFromLineNumber ( codeView , position . line ) ; if ( ! line ) { throw new Error ( invalidPosition ( position , 'Line not found' ) ) ; } if ( ! line . classList . contains ( 'annotated' ) ) { convertNode ( line ) ; } let characterOffset = 0 ; for ( const child of line . childNodes ) { const value = child . textContent ; if ( ! value ) { continue ; } if ( characterOffset <= position . character && characterOffset + value . length >= position . character ) { const rect = line . getBoundingClientRect ( ) ; const { top , height , left , width } = rect ; const event = createMouseEvent ( eventType , { x : left + width / 2 , y : top + height / 2 , } ) ; child . dispatchEvent ( event ) ; return ; } characterOffset += value . length ; } } ;