Skip to content

Commit 7805ea3

Browse files
authored
exec -> execSync for invoking RescriptEditorSupport binary (#82)
Part of #81
1 parent d1e5411 commit 7805ea3

File tree

2 files changed

+92
-90
lines changed

2 files changed

+92
-90
lines changed

server/src/RescriptEditorSupport.ts

+59-52
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fileURLToPath } from "url";
22
import { RequestMessage } from "vscode-languageserver";
33
import * as utils from "./utils";
44
import * as path from "path";
5-
import { exec } from "child_process";
5+
import { execSync } from "child_process";
66
import fs from "fs";
77

88
let 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 }];
5964
export 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
}

server/src/server.ts

+33-38
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,16 @@ process.on("message", (msg: m.Message) => {
332332
// type Hover = {contents: MarkedString | MarkedString[] | MarkupContent, range?: Range}
333333
result: null,
334334
};
335-
runDumpCommand(msg, (result) => {
336-
if (result && result.hover) {
337-
let hoverResponse: m.ResponseMessage = {
338-
...emptyHoverResponse,
339-
result: {
340-
contents: result.hover,
341-
},
342-
};
343-
process.send!(hoverResponse);
344-
} else {
345-
process.send!(emptyHoverResponse);
346-
}
347-
});
335+
let result = runDumpCommand(msg);
336+
if (result !== null && result.hover != null) {
337+
let hoverResponse: m.ResponseMessage = {
338+
...emptyHoverResponse,
339+
result: { contents: result.hover },
340+
};
341+
process.send!(hoverResponse);
342+
} else {
343+
process.send!(emptyHoverResponse);
344+
}
348345
} else if (msg.method === p.DefinitionRequest.method) {
349346
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
350347
let emptyDefinitionResponse: m.ResponseMessage = {
@@ -355,38 +352,36 @@ process.on("message", (msg: m.Message) => {
355352
// error: code and message set in case an exception happens during the definition request.
356353
};
357354

358-
runDumpCommand(msg, (result) => {
359-
if (result && result.definition) {
360-
let definitionResponse: m.ResponseMessage = {
361-
...emptyDefinitionResponse,
362-
result: {
363-
uri: result.definition.uri || msg.params.textDocument.uri,
364-
range: result.definition.range,
365-
},
366-
};
367-
process.send!(definitionResponse);
368-
} else {
369-
process.send!(emptyDefinitionResponse);
370-
}
371-
});
355+
let result = runDumpCommand(msg);
356+
if (result !== null && result.definition != null) {
357+
let definitionResponse: m.ResponseMessage = {
358+
...emptyDefinitionResponse,
359+
result: {
360+
uri: result.definition.uri || msg.params.textDocument.uri,
361+
range: result.definition.range,
362+
},
363+
};
364+
process.send!(definitionResponse);
365+
} else {
366+
process.send!(emptyDefinitionResponse);
367+
}
372368
} else if (msg.method === p.CompletionRequest.method) {
373369
let emptyCompletionResponse: m.ResponseMessage = {
374370
jsonrpc: c.jsonrpcVersion,
375371
id: msg.id,
376372
result: null,
377373
};
378374
let code = getOpenedFileContent(msg.params.textDocument.uri);
379-
runCompletionCommand(msg, code, (result) => {
380-
if (result) {
381-
let definitionResponse: m.ResponseMessage = {
382-
...emptyCompletionResponse,
383-
result: result,
384-
};
385-
process.send!(definitionResponse);
386-
} else {
387-
process.send!(emptyCompletionResponse);
388-
}
389-
});
375+
let result = runCompletionCommand(msg, code);
376+
if (result === null) {
377+
process.send!(emptyCompletionResponse);
378+
} else {
379+
let definitionResponse: m.ResponseMessage = {
380+
...emptyCompletionResponse,
381+
result: result,
382+
};
383+
process.send!(definitionResponse);
384+
}
390385
} else if (msg.method === p.DocumentFormattingRequest.method) {
391386
// technically, a formatting failure should reply with the error. Sadly
392387
// the LSP alert box for these error replies sucks (e.g. doesn't actually

0 commit comments

Comments
 (0)