@@ -3,29 +3,44 @@ import { RequestMessage } from "vscode-languageserver";
3
3
import * as utils from "./utils" ;
4
4
import * as path from "path" ;
5
5
import { exec } from "child_process" ;
6
+ import * as tmp from "tmp" ;
7
+ import fs from "fs" ;
8
+
9
+ let findExecutable = ( uri : string ) => {
10
+ let filePath = fileURLToPath ( uri ) ;
11
+ let projectRootPath = utils . findProjectRootOfFile ( filePath ) ;
12
+ if ( projectRootPath == null ) {
13
+ return null ;
14
+ } else {
15
+ // Currently assumes the dump command is "bin.exe" at the project root.
16
+ let binaryPath = path . join ( projectRootPath , "bin.exe" ) ;
17
+ if ( fs . existsSync ( binaryPath ) ) {
18
+ return { binaryPath, filePath, cwd : projectRootPath } ;
19
+ } else {
20
+ return null ;
21
+ }
22
+ }
23
+ } ;
6
24
7
25
export function runDumpCommand (
8
- msg : RequestMessage ,
26
+ msg : RequestMessage ,
9
27
onResult : (
10
28
result : { hover ?: string ; definition ?: { uri ?: string ; range : any } } | null
11
29
) => void
12
30
) {
13
- let filePath = fileURLToPath ( msg . params . textDocument . uri ) ;
14
- let projectRootPath = utils . findProjectRootOfFile ( filePath ) ;
15
- if ( projectRootPath == null ) {
31
+ let executable = findExecutable ( msg . params . textDocument . uri ) ;
32
+ if ( executable == null ) {
16
33
onResult ( null ) ;
17
34
} else {
18
- // Currently assumes the dump command is "bin.exe" at the project root.
19
- let commandPath = path . join ( projectRootPath , "bin.exe" ) ;
20
35
let command =
21
- commandPath +
36
+ executable . binaryPath +
22
37
" dump " +
23
- filePath +
38
+ executable . filePath +
24
39
":" +
25
40
msg . params . position . line +
26
41
":" +
27
42
msg . params . position . character ;
28
- exec ( command , { cwd : projectRootPath } , function ( _error , _stdout , stderr ) {
43
+ exec ( command , { cwd : executable . cwd } , function ( _error , _stdout , stderr ) {
29
44
let result = JSON . parse ( stderr ) ;
30
45
if ( result && result [ 0 ] ) {
31
46
onResult ( result [ 0 ] ) ;
@@ -35,3 +50,39 @@ export function runDumpCommand(
35
50
} ) ;
36
51
}
37
52
}
53
+
54
+ export function runCompletionCommand (
55
+ msg : RequestMessage ,
56
+ code : string ,
57
+ onResult : ( result : [ { label : string } ] | null ) => void
58
+ ) {
59
+ let executable = findExecutable ( msg . params . textDocument . uri ) ;
60
+ if ( executable == null ) {
61
+ onResult ( null ) ;
62
+ } else {
63
+ let tmpobj = tmp . fileSync ( ) ;
64
+ let tmpname = tmpobj . name ;
65
+ fs . writeFileSync ( tmpname , code , { encoding : "utf-8" } ) ;
66
+
67
+ let command =
68
+ executable . binaryPath +
69
+ " complete " +
70
+ executable . filePath +
71
+ ":" +
72
+ msg . params . position . line +
73
+ ":" +
74
+ msg . params . position . character +
75
+ " " +
76
+ tmpname ;
77
+
78
+ exec ( command , { cwd : executable . cwd } , function ( _error , _stdout , stderr ) {
79
+ tmpobj . removeCallback ( ) ;
80
+ let result = JSON . parse ( stderr ) ;
81
+ if ( result && result [ 0 ] ) {
82
+ onResult ( result ) ;
83
+ } else {
84
+ onResult ( null ) ;
85
+ }
86
+ } ) ;
87
+ }
88
+ }
0 commit comments