import * as ts from 'typescript/lib/tsserverlibrary' ; import ScriptSourceHelper from './script-source-helper' ; import { findAllNodes , findNode } from './nodes' ; export default class StandardScriptSourceHelper implements ScriptSourceHelper { constructor ( private readonly typescript , private readonly project ) { } public getNode ( fileName , position ) { const sourceFile = this . getSourceFile ( fileName ) ; return sourceFile && findNode ( this . typescript , sourceFile , position ) ; } public getAllNodes ( fileName , cond ) { const sourceFile = this . getSourceFile ( fileName ) ; return sourceFile ? findAllNodes ( this . typescript , sourceFile , cond ) : [ ] ; } public getLineAndChar ( fileName , position ) { const scriptInto = this . project . getScriptInfo ( fileName ) ; if ( ! scriptInto ) { return { line : 0 , character : 0 } ; } const location = scriptInto . positionToLineOffset ( position ) ; return { line : location . line - 1 , character : location . offset - 1 } ; } public getOffset ( fileName , line , character ) { const scriptInto = this . project . getScriptInfo ( fileName ) ; if ( ! scriptInto ) { return 0 ; } return scriptInto . lineOffsetToPosition ( line + 1 , character + 1 ) ; } private getProgram ( ) { return this . project . getLanguageService ( ) . getProgram ( ) ; } private getSourceFile ( fileName ) { const program = this . getProgram ( ) ; return program ? program . getSourceFile ( fileName ) : undefined ; } }