import { CodeLensProvider , TextDocument , CancellationToken , CodeLens , Range , Position } from 'vscode' ; interface ExtractedTemplateLiteral { content : string ; uri : string ; position : Position ; } function extractAllTemplateLiterals ( document ) { const documents = [ ] ; const text = document . getText ( ) ; const regExpGQL = new RegExp ( 'groq\\s*`([\\s\\S]+?)`' , 'mg' ) ; let prevIndex = 0 ; let result ; while ( ( result = regExpGQL . exec ( text ) ) !== null ) { const content = result [ 1 ] ; const queryPosition = text . indexOf ( content , prevIndex ) ; documents . push ( { content : content , uri : document . uri . path , position : document . positionAt ( queryPosition ) , } ) ; prevIndex = queryPosition + 1 ; } return documents ; } export class GROQCodeLensProvider implements CodeLensProvider { constructor ( ) { } public provideCodeLenses ( document , _token ) { if ( document . languageId === 'groq' ) { return [ new CodeLens ( new Range ( new Position ( 0 , 0 ) , new Position ( 0 , 0 ) ) , { title : 'Execute Query' , command : 'sanity.executeGroq' , arguments : [ document . getText ( ) ] , } ) , ] ; } const literals = extractAllTemplateLiterals ( document ) ; return literals . map ( ( literal ) => { return new CodeLens ( new Range ( new Position ( literal . position . line , 0 ) , new Position ( literal . position . line , 0 ) ) , { title : 'Execute Query' , command : 'sanity.executeGroq' , arguments : [ literal . content ] , } ) ; } ) ; } }