-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathRescriptEditorSupport.ts
89 lines (82 loc) · 2.26 KB
/
RescriptEditorSupport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { fileURLToPath } from "url";
import { RequestMessage } from "vscode-languageserver";
import * as utils from "./utils";
import * as path from "path";
import { exec } from "child_process";
import fs from "fs";
let binaryPath = path.join(
path.dirname(__dirname),
process.platform,
"rescript-editor-support.exe"
);
export let binaryExists = fs.existsSync(binaryPath);
let findExecutable = (uri: string) => {
let filePath = fileURLToPath(uri);
let projectRootPath = utils.findProjectRootOfFile(filePath);
if (projectRootPath == null || !binaryExists) {
return null;
} else {
return { binaryPath, filePath, cwd: projectRootPath };
}
};
export function runDumpCommand(
msg: RequestMessage,
onResult: (
result: { hover?: string; definition?: { uri?: string; range: any } } | null
) => void
) {
let executable = findExecutable(msg.params.textDocument.uri);
if (executable == null) {
onResult(null);
} else {
let command =
executable.binaryPath +
" dump " +
executable.filePath +
":" +
msg.params.position.line +
":" +
msg.params.position.character;
exec(command, { cwd: executable.cwd }, function (_error, stdout, _stderr) {
let result = JSON.parse(stdout);
if (result && result[0]) {
onResult(result[0]);
} else {
onResult(null);
}
});
}
}
export function runCompletionCommand(
msg: RequestMessage,
code: string,
onResult: (result: [{ label: string }] | null) => void
) {
let executable = findExecutable(msg.params.textDocument.uri);
if (executable == null) {
onResult(null);
} else {
let tmpname = utils.createFileInTempDir();
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
let command =
executable.binaryPath +
" complete " +
executable.filePath +
":" +
msg.params.position.line +
":" +
msg.params.position.character +
" " +
tmpname;
exec(command, { cwd: executable.cwd }, function (_error, stdout, _stderr) {
// async close is fine. We don't use this file name again
fs.unlink(tmpname, () => null);
let result = JSON.parse(stdout);
if (result && result[0]) {
onResult(result);
} else {
onResult(null);
}
});
}
}