'use strict' ; import { IPCMessageReader , IPCMessageWriter , createConnection , IConnection , TextDocumentSyncKind , TextDocuments , TextDocument , Diagnostic , DiagnosticSeverity , InitializeParams , InitializeResult , TextDocumentPositionParams , CompletionItem , CompletionItemKind } from 'vscode-languageserver' ; let connection = createConnection ( new IPCMessageReader ( process ) , new IPCMessageWriter ( process ) ) ; let documents = new TextDocuments ( ) ; documents . listen ( connection ) ; let workspaceRoot ; connection . onInitialize ( ( params ) : InitializeResult => { workspaceRoot = params . rootPath ; return { capabilities : { textDocumentSync : documents . syncKind } } ; } ) ; documents . onDidOpen ( ( change ) => { validateFile ( change . document ) ; } ) ; interface Settings { languageCrystalServer : ExampleSettings ; } interface ExampleSettings { maxNumberOfProblems : number ; } interface FileChanged { uri : string ; } let maxNumberOfProblems ; connection . onDidChangeConfiguration ( ( change ) => { let settings = < Settings > change . settings ; maxNumberOfProblems = settings [ "crystal-ide" ] . maxNumberOfProblems || 100 ; documents . all ( ) . forEach ( validateFile ) ; } ) ; function fileUriToPath ( uri ) { return decodeURI ( uri . replace ( "file://" , "" ) ) ; } function validateFile ( file ) { let exec = require ( 'child_process' ) . exec ; let diagnostics = [ ] ; exec ( "crystal build --no-color --no-codegen -f json --release \"" + fileUriToPath ( file . uri ) + "\"" , ( err , response ) => { if ( response ) { let results = JSON . parse ( response ) ; let length = Math . min ( maxNumberOfProblems , results . length ) ; for ( var problems = 0 ; problems < length ; problems ++ ) { let problem = results [ problems ] ; diagnostics . push ( { severity : DiagnosticSeverity . Error , range : { start : { line : problem . line - 1 , character : problem . column - 1 } , end : { line : problem . line - 1 , character : ( problem . column + ( problem . size || 0 ) - 1 ) } } , message : problem . message , source : 'Crystal Language' } ) ; } } connection . sendDiagnostics ( { uri : file . uri , diagnostics } ) ; } ) ; } connection . onDidChangeWatchedFiles ( ( change ) => { change . changes . forEach ( validateFile ) ; } ) ; connection . listen ( ) ;