'use strict' ; import { workspace , ExtensionContext , TextDocument , languages , Uri , Diagnostic , DiagnosticCollection , TextDocumentContentChangeEvent } from 'vscode' ; import { isNullOrUndefined } from 'util' ; import { lintText } from './linter' ; let diagnosticCollection ; let diagnosticMap ; export function activate ( context ) { console . log ( "Write-Good Linter active..." ) ; diagnosticCollection = languages . createDiagnosticCollection ( "Write-Good Lints" ) ; diagnosticMap = new Map ( ) ; if ( context == null ) return ; function isWriteGoodLanguage ( languageId ) { let wgLanguages = workspace . getConfiguration ( 'write-good' ) . get ( 'languages' ) ; return ( wgLanguages . indexOf ( languageId ) > - 1 || wgLanguages === '*' ) ; } context . subscriptions . push ( workspace . onDidSaveTextDocument ( document => { if ( isWriteGoodLanguage ( document . languageId ) ) { doLint ( document ) ; } } ) ) ; context . subscriptions . push ( workspace . onDidChangeTextDocument ( event => { if ( isWriteGoodLanguage ( event . document . languageId ) ) { const onlyLintOnSave = workspace . getConfiguration ( 'write-good' ) . get ( 'only-lint-on-save' ) ; if ( ! onlyLintOnSave ) { doLint ( event . document ) ; } } } ) ) ; context . subscriptions . push ( workspace . onDidOpenTextDocument ( event => { if ( isWriteGoodLanguage ( event . languageId ) ) { doLint ( event ) ; } } ) ) ; context . subscriptions . push ( workspace . onDidCloseTextDocument ( event => { if ( diagnosticMap . has ( event . uri . toString ( ) ) ) { diagnosticMap . delete ( event . uri . toString ( ) ) ; } resetDiagnostics ( ) ; } ) ) ; } export function deactivate ( ) { console . log ( "Write-Good Linter deactivating..." ) ; } function resetDiagnostics ( ) { diagnosticCollection . clear ( ) ; diagnosticMap . forEach ( ( diags , file ) => { diagnosticCollection . set ( Uri . parse ( file ) , diags ) ; } ) ; } function getWriteGoodConfig ( ) { var wgConfig = workspace . getConfiguration ( 'write-good' ) . get ( 'write-good-config' ) ; if ( isNullOrUndefined ( wgConfig ) ) { wgConfig = { } ; } return wgConfig ; } function doPartialLint ( document , changes ) { const wgConfig = getWriteGoodConfig ( ) ; let diagnostics = diagnosticMap . get ( document . uri . toString ( ) ) ; changes . forEach ( ( changeEvent , index ) => { let toRemove = [ ] ; diagnostics . forEach ( ( diagnostic , index ) => { if ( diagnostic . range . intersection ( changeEvent . range ) != undefined ) { toRemove . push ( index ) ; } } ) ; toRemove . forEach ( ( i ) => diagnostics . splice ( i , 1 ) ) ; if ( changeEvent . text . length > 0 ) { const lineOffset = changeEvent . range . start . line ; lintText ( changeEvent . text , wgConfig , lineOffset , diagnostics ) ; } } ) ; } function doLint ( document ) { const wgConfig = getWriteGoodConfig ( ) ; let diagnostics = [ ] ; lintText ( document . getText ( ) , wgConfig , 0 , diagnostics ) ; diagnosticMap . set ( document . uri . toString ( ) , diagnostics ) ; resetDiagnostics ( ) ; }