file import * as vscode from "vscode" ; import { LanguageClient } from "vscode-languageclient" ; import { TypeCoverageResponse } from "./types/lsp" ; export class HackCoverageChecker { private visible : boolean = false ; private coverageStatus : vscode . StatusBarItem ; private hhvmCoverDiag : vscode . DiagnosticCollection ; private languageClient : LanguageClient ; constructor ( languageClient ) { this . coverageStatus = vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Left ) ; this . hhvmCoverDiag = vscode . languages . createDiagnosticCollection ( "hack_coverage" ) ; this . languageClient = languageClient ; } public async start ( context ) { context . subscriptions . push ( vscode . workspace . onDidSaveTextDocument ( async ( doc ) => this . check ( doc ) ) ) ; context . subscriptions . push ( vscode . window . onDidChangeActiveTextEditor ( async ( editor ) => { if ( editor ) { await this . check ( editor . document ) ; } else { this . hhvmCoverDiag . clear ( ) ; this . coverageStatus . hide ( ) ; } } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( "hack.toggleCoverageHighlight" , async ( ) => this . toggle ( ) ) ) ; context . subscriptions . push ( this . hhvmCoverDiag , this . coverageStatus ) ; if ( vscode . window . activeTextEditor ) { await this . check ( vscode . window . activeTextEditor . document ) ; } } public async toggle ( ) { if ( this . visible ) { this . hhvmCoverDiag . clear ( ) ; this . visible = false ; } else { this . visible = true ; const editor = vscode . window . activeTextEditor ; if ( editor ) { await this . check ( editor . document ) ; } } } private async check ( document ) { this . hhvmCoverDiag . clear ( ) ; if ( document . languageId !== "hack" || document . uri . scheme !== "file" ) { this . coverageStatus . hide ( ) ; return ; } let coverageResponse ; try { coverageResponse = await this . languageClient . sendRequest < TypeCoverageResponse > ( "textDocument/typeCoverage" , { textDocument : this . languageClient . code2ProtocolConverter . asTextDocumentIdentifier ( document ) } ) ; } catch ( e ) { this . coverageStatus . hide ( ) ; return ; } this . coverageStatus . text = `$(paintcan)  ${ coverageResponse . coveredPercent }%` ; this . coverageStatus . tooltip = `This file is ${ coverageResponse . coveredPercent }% covered by Hack.\nClick to toggle highlighting of uncovered areas.` ; this . coverageStatus . command = "hack.toggleCoverageHighlight" ; this . coverageStatus . show ( ) ; if ( this . visible && coverageResponse . uncoveredRanges ) { const diagnostics = [ ] ; coverageResponse . uncoveredRanges . forEach ( uncoveredRange => { const diagnostic = new vscode . Diagnostic ( uncoveredRange . range , uncoveredRange . message || coverageResponse . defaultMessage , vscode . DiagnosticSeverity . Information ) ; diagnostic . source = "Type Coverage" ; diagnostics . push ( diagnostic ) ; } ) ; this . hhvmCoverDiag . set ( document . uri , diagnostics ) ; } } }