@@ -2,7 +2,7 @@ import { fileURLToPath } from "url";
2
2
import { RequestMessage } from "vscode-languageserver" ;
3
3
import * as utils from "./utils" ;
4
4
import * as path from "path" ;
5
- import { exec } from "child_process" ;
5
+ import { execSync } from "child_process" ;
6
6
import fs from "fs" ;
7
7
8
8
let binaryPath = path . join (
@@ -27,67 +27,74 @@ let findExecutable = (uri: string) => {
27
27
}
28
28
} ;
29
29
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 {
36
35
let executable = findExecutable ( msg . params . textDocument . uri ) ;
37
36
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 ;
56
60
}
57
61
}
58
62
63
+ type completionCommandResult = [ { label : string } ] ;
59
64
export function runCompletionCommand (
60
65
msg : RequestMessage ,
61
- code : string ,
62
- onResult : ( result : [ { label : string } ] | null ) => void
63
- ) {
66
+ code : string
67
+ ) : completionCommandResult | null {
64
68
let executable = findExecutable ( msg . params . textDocument . uri ) ;
65
69
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" } ) ;
70
74
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 ;
81
85
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 ) ;
92
99
}
93
100
}
0 commit comments