import * as vscode from 'vscode' ; import * as cp from 'child_process' ; import Linter , { LinterError } from './linter' ; const diagnosticCollection = vscode . languages . createDiagnosticCollection ( "protolint" ) ; export function activate ( context ) { const result = cp . spawnSync ( 'protolint' , [ 'version' ] ) ; if ( result . status !== 0 ) { vscode . window . showErrorMessage ( "protolint was not detected. Download from: https://github.com/yoheimuta/protolint" ) ; return ; } vscode . commands . registerCommand ( 'protolint.lint' , runLint ) ; vscode . workspace . onDidSaveTextDocument ( ( document ) => { vscode . commands . executeCommand ( 'protolint.lint' ) ; } ) ; vscode . window . onDidChangeActiveTextEditor ( ( e ) => { vscode . commands . executeCommand ( 'protolint.lint' ) ; } ) ; } function runLint ( ) { let editor = vscode . window . activeTextEditor ; if ( ! editor ) { return ; } const doc = editor . document ; if ( doc . languageId !== 'proto3' && doc . languageId !== 'proto' ) { return ; } doLint ( doc , diagnosticCollection ) ; } async function doLint ( codeDocument , collection ) { const linter = new Linter ( codeDocument ) ; const errors = await linter . lint ( ) ; const diagnostics = errors . map ( error => { return new vscode . Diagnostic ( error . range , error . proto . reason , vscode . DiagnosticSeverity . Warning ) ; } ) ; collection . clear ( ) ; collection . set ( codeDocument . uri , diagnostics ) ; }