import * as fs from 'fs' ; import * as net from "net" ; import * as path from "path" ; import * as process from "process" ; import * as child_process from "child_process" ; import { save_as_temp , clear_temp_file_suffixes } from "./utils" ; import * as utils from "./utils" ; export function server ( ) { return path . join ( utils . user_dir ( ) , "dotnet" , "syntaxer" , "syntaxer.dll" ) ; } function cscs ( ) { return path . join ( utils . user_dir ( ) , "dotnet" , "cscs.dll" ) ; } let PORT = 18003 ; export function start_syntaxer ( ) { let runtime = "dotnet" ; let SERVER = server ( ) ; let CSCS = cscs ( ) ; let args = [ SERVER , `-port:${ PORT }` , "-listen" , `-client:${ process . pid }` , "-timeout:60000" , `-cscs_path:${ CSCS }` ] ; child_process . execFile ( runtime , args , ( error , stdout , stderr ) => { console . log ( stderr ) ; console . log ( stdout ) ; } ) ; } export class Syntaxer { public static init ( ) { } public static sentStopRequest ( ) { let client = new net . Socket ( ) ; client . connect ( PORT , '127.0.0.1' , function ( ) { client . write ( '-exit' ) ; } ) ; } private static send ( request , onData ) { let client = new net . Socket ( ) ; client . connect ( PORT , '127.0.0.1' , function ( ) { client . write ( request ) ; } ) ; client . on ( 'error' , function ( error ) { let server_exe = server ( ) ; if ( fs . existsSync ( server_exe ) ) { start_syntaxer ( ) ; } } ) ; client . on ( 'data' , function ( data ) { let response = data . toString ( ) ; client . destroy ( ) ; if ( onData ) onData ( response ) ; } ) ; } public static send_request ( request , code , file , resolve , inject_source_info = true ) { let temp_file = save_as_temp ( code , file , inject_source_info ) ; Syntaxer . send ( request . replace ( "$temp_file$" , temp_file ) , data => { resolve ( clear_temp_file_suffixes ( data ) ) ; fs . unlink ( temp_file , error => { } ) ; } ) ; } public static getCompletions ( code , file , position , resolve ) { let request = `-client:${ process . pid }\n-op:completion\n-script:$temp_file$\n-pos:${ position }\n-doc` ; Syntaxer . send_request ( request , code , file , resolve ) ; } public static suggestUsings ( code , file , word , resolve ) { let request = `-client:${ process . pid }\n-op:suggest_usings:${ word }\n-script:$temp_file$` ; Syntaxer . send_request ( request , code , file , resolve ) ; } public static getSignatureHelp ( code , file , position , resolve ) { let request = `-client:${ process . pid }\n-op:signaturehelp\n-script:$temp_file$\n-pos:${ position }` ; Syntaxer . send_request ( request , code , file , resolve ) ; } public static getTooltip ( code , file , position , resolve ) { let hint = '' ; let request = `-client:${ process . pid }\n-op:tooltip:${ hint }\n-script:$temp_file$\n-pos:${ position }` ; Syntaxer . send_request ( request , code , file , resolve ) ; } public static getRefrences ( code , file , position , resolve ) { let request = `-client:${ process . pid }\n-op:references\n-script:$temp_file$\n-pos:${ position }` ; Syntaxer . send_request ( request , code , file , resolve ) ; } public static getReferencesAsync ( code , file , position ) { return new Promise ( ( resolve , reject ) => { let request = `-client:${ process . pid }\n-op:references\n-script:$temp_file$\n-pos:${ position }` ; Syntaxer . send_request ( request , code , file , resolve ) ; } ) ; } public static getRenamingInfo ( code , file , position , resolve ) { let request = `-client:${ process . pid }\n-op:references\n-context:all\n-script:$temp_file$\n-pos:${ position }` ; Syntaxer . send_request ( request , code , file , resolve ) ; } public static ping ( resolve ) { let request = `-client:${ process . pid }\n-op:ping` ; let client = new net . Socket ( ) ; client . connect ( PORT , '127.0.0.1' , ( ) => client . write ( request ) ) ; client . on ( 'data' , data => { let response = data . toString ( ) ; client . destroy ( ) ; resolve ( response ) ; } ) ; client . on ( 'error' , ( ) => resolve ( "error" ) ) ; } public static doDocFormat ( code , file , position , resolve ) { let request = `-client:${ process . pid }\n-op:format\n-script:$temp_file$\n-pos:${ position }` ; Syntaxer . send_request ( request , code , file , resolve , false ) ; } public static getDefinition ( code , file , position , resolve ) { let request = `-client:${ process . pid }\n-op:resolve\n-script:$temp_file$\n-pos:${ position }` ; Syntaxer . send_request ( request , code , file , resolve ) ; } }