import * as cp from 'child_process' ; import { SymbolInformation , DocumentSymbolParams , } from 'vscode-languageserver' ; import { ILogger , ISymbolProvider } from '../interfaces' ; export abstract class AbstractTagsSymbolProvider implements ISymbolProvider { protected executable : string ; protected workspaceRoot : string ; protected logger : ILogger ; public constructor ( executable , workspaceRoot , logger ) { this . executable = executable ; this . workspaceRoot = workspaceRoot ; this . logger = logger ; } protected abstract getFileSymbolsCommand ( documentSymbolParams ) ; protected abstract getWorkspaceSymbolsCommand ( ) ; protected abstract parseTags ( rawTags ) ; public getSymbolsForFile ( documentSymbolParams ) { let command = this . getFileSymbolsCommand ( documentSymbolParams ) ; return this . getSymbols ( command ) ; } public getSymbolsForWorkspace ( options , cancellationToken ? ) { let command = this . getWorkspaceSymbolsCommand ( ) ; return this . getSymbols ( command ) . then ( symbols => { return symbols . filter ( documentSymbol => { return documentSymbol . name . toLowerCase ( ) . indexOf ( options . query . toLowerCase ( ) ) >= 0 ; } ) ; } ) ; } private getSymbols ( command ) { return new Promise ( ( resolve , reject ) => { this . logger . log ( command ) ; cp . exec ( command , ( error , stdout , stderr ) => { let errorMessage = '' ; if ( error ) { errorMessage += JSON . stringify ( error ) ; errorMessage += '\n' ; } if ( stderr ) { errorMessage += stderr . toString ( ) ; } if ( errorMessage ) { this . logger . error ( errorMessage ) ; resolve ( [ ] ) ; } this . logger . log ( stdout . toString ( 'UTF8' ) ) ; resolve ( this . parseTags ( stdout . toString ( 'UTF8' ) ) ) ; } ) ; } ) ; } }