@@ -2,7 +2,7 @@ import { fileURLToPath } from "url";
22import { RequestMessage } from "vscode-languageserver" ;
33import * as utils from "./utils" ;
44import * as path from "path" ;
5- import { exec } from "child_process" ;
5+ import { execSync } from "child_process" ;
66import fs from "fs" ;
77
88let binaryPath = path . join (
@@ -27,67 +27,74 @@ let findExecutable = (uri: string) => {
2727 }
2828} ;
2929
30- export function runDumpCommand (
31- msg : RequestMessage ,
32- onResult : (
33- result : { hover ?: string ; definition ?: { uri ?: string ; range : any } } | null
34- ) => void
35- ) {
30+ type dumpCommandResult = {
31+ hover ?: string ;
32+ definition ?: { uri ?: string ; range : any } ;
33+ } ;
34+ export function runDumpCommand ( msg : RequestMessage ) : dumpCommandResult | null {
3635 let executable = findExecutable ( msg . params . textDocument . uri ) ;
3736 if ( executable == null ) {
38- onResult ( null ) ;
39- } else {
40- let command =
41- executable . binaryPathQuoted +
42- " dump " +
43- executable . filePathQuoted +
44- ":" +
45- msg . params . position . line +
46- ":" +
47- msg . params . position . character ;
48- exec ( command , { cwd : executable . cwd } , function ( _error , stdout , _stderr ) {
49- let result = JSON . parse ( stdout ) ;
50- if ( result && result [ 0 ] ) {
51- onResult ( result [ 0 ] ) ;
52- } else {
53- onResult ( null ) ;
54- }
55- } ) ;
37+ return null ;
38+ }
39+
40+ let command =
41+ executable . binaryPathQuoted +
42+ " dump " +
43+ executable . filePathQuoted +
44+ ":" +
45+ msg . params . position . line +
46+ ":" +
47+ msg . params . position . character ;
48+
49+ try {
50+ let stdout = execSync ( command , { cwd : executable . cwd } ) ;
51+ let parsed = JSON . parse ( stdout . toString ( ) ) ;
52+ if ( parsed && parsed [ 0 ] ) {
53+ return parsed [ 0 ] ;
54+ } else {
55+ return null ;
56+ }
57+ } catch ( error ) {
58+ // TODO: @cristianoc any exception possible?
59+ return null ;
5660 }
5761}
5862
63+ type completionCommandResult = [ { label : string } ] ;
5964export function runCompletionCommand (
6065 msg : RequestMessage ,
61- code : string ,
62- onResult : ( result : [ { label : string } ] | null ) => void
63- ) {
66+ code : string
67+ ) : completionCommandResult | null {
6468 let executable = findExecutable ( msg . params . textDocument . uri ) ;
6569 if ( executable == null ) {
66- onResult ( null ) ;
67- } else {
68- let tmpname = utils . createFileInTempDir ( ) ;
69- fs . writeFileSync ( tmpname , code , { encoding : "utf-8" } ) ;
70+ return null ;
71+ }
72+ let tmpname = utils . createFileInTempDir ( ) ;
73+ fs . writeFileSync ( tmpname , code , { encoding : "utf-8" } ) ;
7074
71- let command =
72- executable . binaryPathQuoted +
73- " complete " +
74- executable . filePathQuoted +
75- ":" +
76- msg . params . position . line +
77- ":" +
78- msg . params . position . character +
79- " " +
80- tmpname ;
75+ let command =
76+ executable . binaryPathQuoted +
77+ " complete " +
78+ executable . filePathQuoted +
79+ ":" +
80+ msg . params . position . line +
81+ ":" +
82+ msg . params . position . character +
83+ " " +
84+ tmpname ;
8185
82- exec ( command , { cwd : executable . cwd } , function ( _error , stdout , _stderr ) {
83- // async close is fine. We don't use this file name again
84- fs . unlink ( tmpname , ( ) => null ) ;
85- let result = JSON . parse ( stdout ) ;
86- if ( result && result [ 0 ] ) {
87- onResult ( result ) ;
88- } else {
89- onResult ( null ) ;
90- }
91- } ) ;
86+ try {
87+ let stdout = execSync ( command , { cwd : executable . cwd } ) ;
88+ let parsed = JSON . parse ( stdout . toString ( ) ) ;
89+ if ( parsed && parsed [ 0 ] ) {
90+ return parsed ;
91+ } else {
92+ return null ;
93+ }
94+ } catch ( error ) {
95+ // TODO: @cristianoc any exception possible?
96+ return null ;
97+ } finally {
98+ fs . unlink ( tmpname , ( ) => null ) ;
9299 }
93100}
0 commit comments