file import * as ps from "child_process" ; import * as config from "./Config" ; import * as remote from "./remote" ; import * as hack from "./types/hack" ; export async function version ( ) { return run ( [ "--version" ] ) ; } /**
 * Hack client hangs if executed in lsp mode before running it standalone.
 */ export async function start ( ) { return run ( [ ] ) ; } export async function check ( ) { return run ( [ "check" ] ) ; } export async function typeAtPos ( fileName , line , character ) { const arg = `${ fileName }:${ line }:${ character }` ; const args = [ "--type-at-pos" , arg ] ; const typeAtPosResponse = await run ( args ) ; if ( ! typeAtPosResponse || ! typeAtPosResponse . type || typeAtPosResponse . type === "(unknown)" || typeAtPosResponse . type === "_" || typeAtPosResponse . type === "noreturn" ) { return undefined ; } return typeAtPosResponse . type ; } export async function outline ( text ) { return run ( [ "--outline" ] , text ) ; } export async function search ( query ) { return run ( [ "--search" , query ] ) ; } export async function ideFindRefs ( text , line , character ) { return run ( [ "--ide-find-refs" , `${ line }:${ character }` ] , text ) ; } export async function ideHighlightRefs ( text , line , character ) { return run ( [ "--ide-highlight-refs" , `${ line }:${ character }` ] , text ) ; } export async function ideGetDefinition ( text , line , character ) { return run ( [ "--ide-get-definition" , `${ line }:${ character }` ] , text ) ; } export async function autoComplete ( text , position ) { const autoTok = "AUTO332" ; const input = [ text . slice ( 0 , position ) , autoTok , text . slice ( position ) ] . join ( "" ) ; return run ( [ "--auto-complete" ] , input ) ; } export async function format ( text , startPos , endPos ) { return run ( [ "--format" , startPos . toString ( ) , ( endPos + 1 ) . toString ( ) ] , text ) ; } async function run ( extraArgs , stdin ? ) { return new Promise < any > ( ( resolve , _ ) => { const workspacePath = config . remoteEnabled && config . remoteWorkspacePath ? config . remoteWorkspacePath : config . localWorkspacePath ; const command = remote . getCommand ( config . clientPath ) ; const args = remote . getArgs ( config . clientPath , [ ... extraArgs , "--json" , "--from" , "vscode-hack" , workspacePath , ] ) ; const p = ps . execFile ( command , args , { maxBuffer : 1024 * 1024 } , ( err , stdout , stderr ) => { if ( err !== null && err . code !== 0 && err . code !== 2 ) { console . error ( `Hack: hh_client execution error: ${ err }` ) ; resolve ( null ) ; } if ( ! stdout ) { stdout = stderr ; } try { const output = JSON . parse ( stdout ) ; resolve ( output ) ; } catch ( parseErr ) { console . error ( `Hack: hh_client output error: ${ parseErr }` ) ; resolve ( null ) ; } } ) ; if ( stdin && p && p . stdin ) { p . stdin . write ( stdin ) ; p . stdin . end ( ) ; } } ) ; }