'use strict' ; import * as vscode from 'vscode' ; import * as cp from 'child_process' ; import * as path from 'path' ; import * as fs from 'fs' ; import { window , workspace } from 'vscode' ; import * as vsctmls from 'vscode-textmate-languageservice' ; import { check , ICheckResult } from './matlabDiagnostics' ; let diagnosticCollection ; export async function activate ( context ) { console . log ( 'Activating extension MATLAB' ) ; const selector = { language : 'matlab' , scheme : 'file' } ; const engine = new vsctmls . textmateEngine . TextmateEngine ( 'matlab' , 'source.matlab' ) ; const documentSymbolProvider = new vsctmls . documentSymbols . DocumentSymbolProvider ( engine ) ; const foldingProvider = new vsctmls . folding . FoldingProvider ( engine ) ; const workspaceSymbolProvider = new vsctmls . workspaceSymbols . WorkspaceSymbolProvider ( 'matlab' , documentSymbolProvider ) ; const peekFileDefinitionProvider = new vsctmls . peekDefinitions . PeekDefinitionProvider ( documentSymbolProvider ) ; context . subscriptions . push ( vscode . languages . registerDocumentSymbolProvider ( selector , documentSymbolProvider ) ) ; context . subscriptions . push ( vscode . languages . registerFoldingRangeProvider ( selector , foldingProvider ) ) ; context . subscriptions . push ( vscode . languages . registerWorkspaceSymbolProvider ( workspaceSymbolProvider ) ) ; context . subscriptions . push ( vscode . languages . registerDefinitionProvider ( [ 'matlab' ] , peekFileDefinitionProvider ) ) ; var matlabConfig = workspace . getConfiguration ( 'matlab' ) ; if ( ! matlabConfig [ 'lintOnSave' ] ) { return ; } if ( ! matlabConfig . has ( 'mlintpath' ) || matlabConfig [ 'mlintpath' ] == null ) { window . showErrorMessage ( 'Could not find path to the mlint executable in the configuration file.' ) ; return ; } var mlintPath = matlabConfig [ 'mlintpath' ] ; if ( ! fs . existsSync ( mlintPath ) ) { window . showErrorMessage ( 'Cannot find mlint at the given path, please check your configuration file.' ) ; return ; } diagnosticCollection = vscode . languages . createDiagnosticCollection ( 'matlab' ) ; context . subscriptions . push ( diagnosticCollection ) ; context . subscriptions . push ( workspace . onDidSaveTextDocument ( document => { lintDocument ( document , mlintPath ) ; } ) ) ; context . subscriptions . push ( workspace . onDidOpenTextDocument ( document => { lintDocument ( document , mlintPath ) ; } ) ) ; workspace . textDocuments . forEach ( document => lintDocument ( document , mlintPath ) ) ; } function lintDocument ( document , mlintPath ) { function mapSeverityToVSCodeSeverity ( sev ) { switch ( sev ) { case 'error' : return vscode . DiagnosticSeverity . Error ; case 'warning' : return vscode . DiagnosticSeverity . Warning ; default : return vscode . DiagnosticSeverity . Error ; } } if ( document . languageId != 'matlab' || document . uri . scheme != 'file' ) { return ; } let matlabConfig = vscode . workspace . getConfiguration ( 'matlab' ) ; check ( document , matlabConfig [ 'lintOnSave' ] , mlintPath ) . then ( errors => { diagnosticCollection . delete ( document . uri ) ; let diagnosticMap = new Map ( ) ; ; errors . forEach ( error => { let targetUri = vscode . Uri . file ( error . file ) ; var line = error . line - 1 ; if ( line < 0 ) line = 0 ; var startColumn = error . column [ 0 ] - 1 ; if ( startColumn < 0 ) startColumn = 0 ; var endColumn = error . column [ 1 ] ; let range = new vscode . Range ( line , startColumn , line , endColumn ) ; let diagnostic = new vscode . Diagnostic ( range , error . msg , mapSeverityToVSCodeSeverity ( error . severity ) ) ; let diagnostics = diagnosticMap . get ( targetUri ) ; if ( ! diagnostics ) { diagnostics = [ ] ; } diagnostics . push ( diagnostic ) ; diagnosticMap . set ( targetUri , diagnostics ) ; } ) ; let entries = [ ] ; diagnosticMap . forEach ( ( diags , uri ) => { entries . push ( [ uri , diags ] ) ; } ) ; diagnosticCollection . set ( entries ) ; } ) . catch ( err => { vscode . window . showErrorMessage ( err ) ; } ) ; }