import * as vscode from 'vscode' ; import { highlightRules , highlightProblems } from './decoratorTypes' ; export async function init ( ) { let activeEditor = vscode . window . activeTextEditor ; if ( ! activeEditor ) { vscode . window . showInformationMessage ( 'Editor does not exist!' ) ; return ; } vscode . window . onDidChangeActiveTextEditor ( ( ) => { try { activeEditor = vscode . window . activeTextEditor ; highlightDecorators ( ) ; } catch ( error ) { vscode . window . showWarningMessage ( 'Error while trying to detect current active editor.' ) ; console . error ( 'Error:' , error ) ; } } ) ; vscode . window . onDidChangeTextEditorSelection ( ( ) => { try { activeEditor = vscode . window . activeTextEditor ; highlightDecorators ( ) ; } catch ( error ) { vscode . window . showWarningMessage ( 'Error while trying to detect current active editor.' ) ; console . error ( 'Error:' , error ) ; } } ) ; } function highlightDecorators ( onlyActive = false ) { try { if ( onlyActive ) { vscode . window . visibleTextEditors . map ( ( editor ) => { decorate ( editor ) ; } ) ; } else { vscode . window . visibleTextEditors . map ( ( editor ) => { if ( editor !== vscode . window . activeTextEditor ) { return ; } decorate ( editor ) ; } ) ; } } catch ( error ) { vscode . window . showWarningMessage ( 'Error while trying to update decorator highlights.' ) ; console . error ( 'Error:' , error ) ; } } function decorate ( editor ) { const regex = /@[A-Za-z]+/g ; const sourceCode = editor . document . getText ( ) ; const decorationsArray = [ ] ; const sourceCodeLines = sourceCode . split ( '\n' ) ; for ( let line = 0 ; line < sourceCodeLines . length ; line ++ ) { const currentLine = sourceCodeLines [ line ] ; const matches = currentLine . match ( regex ) ; if ( matches !== null ) { if ( matches . index === undefined ) { matches . map ( ( match ) => { const startPos = currentLine . indexOf ( match ) ; const endPos = startPos + match . length ; let range = new vscode . Range ( new vscode . Position ( line , startPos ) , new vscode . Position ( line , endPos ) ) ; decorationsArray . push ( { range } ) ; } ) ; } else { let range = new vscode . Range ( new vscode . Position ( line , matches . index ) , new vscode . Position ( line , matches . index + matches [ 0 ] . length ) ) ; decorationsArray . push ( { range } ) ; } checkFunctionName ( matches , sourceCodeLines [ line + 1 ] , line + 1 , editor ) ; } } editor . setDecorations ( highlightRules , decorationsArray ) ; } function checkFunctionName ( validators , line , lineNumber , editor ) { const functionName = line . match ( /function(.*?)\(/ ) [ 1 ] . trim ( ) ; const decorationsArray = [ ] ; let valid = false ; validators . map ( ( validator ) => { valid = validName ( validator , functionName ) ; } ) ; if ( ! valid ) { const currentLine = editor . document . lineAt ( lineNumber ) ; const range = new vscode . Range ( currentLine . range . start , currentLine . range . end ) ; decorationsArray . push ( { range } ) ; } editor . setDecorations ( highlightProblems , decorationsArray ) ; } function validName ( validator , name ) { return false ; }