-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathRescriptEditorSupport.ts
100 lines (91 loc) · 2.36 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
90
91
92
93
94
95
96
97
98
99
100
import { fileURLToPath } from "url";
import { RequestMessage } from "vscode-languageserver";
import * as utils from "./utils";
import * as path from "path";
import { execFileSync } 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: binaryPath,
filePath: filePath,
cwd: projectRootPath,
};
}
};
type dumpCommandResult = {
hover?: string;
definition?: { uri?: string; range: any };
};
export function runDumpCommand(msg: RequestMessage): dumpCommandResult | null {
let executable = findExecutable(msg.params.textDocument.uri);
if (executable == null) {
return null;
}
let command =
executable.filePath +
":" +
msg.params.position.line +
":" +
msg.params.position.character;
try {
let stdout = execFileSync(executable.binaryPath, ["dump", command], {
cwd: executable.cwd,
});
let parsed = JSON.parse(stdout.toString());
if (parsed && parsed[0]) {
return parsed[0];
} else {
return null;
}
} catch (error) {
// TODO: @cristianoc any exception possible?
return null;
}
}
type completionCommandResult = [{ label: string }];
export function runCompletionCommand(
msg: RequestMessage,
code: string
): completionCommandResult | null {
let executable = findExecutable(msg.params.textDocument.uri);
if (executable == null) {
return null;
}
let tmpname = utils.createFileInTempDir();
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
let command =
executable.filePath +
":" +
msg.params.position.line +
":" +
msg.params.position.character;
try {
let stdout = execFileSync(
executable.binaryPath,
["complete", command, tmpname],
{ cwd: executable.cwd }
);
let parsed = JSON.parse(stdout.toString());
if (parsed && parsed[0]) {
return parsed;
} else {
return null;
}
} catch (error) {
// TODO: @cristianoc any exception possible?
return null;
} finally {
fs.unlink(tmpname, () => null);
}
}