Skip to content

Commit 164eb05

Browse files
committed
Small analysis binary invocation clean up
I don't like callbacks though
1 parent 968e555 commit 164eb05

File tree

3 files changed

+62
-102
lines changed

3 files changed

+62
-102
lines changed

server/src/RescriptEditorSupport.ts

-94
This file was deleted.

server/src/server.ts

+36-8
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ import {
1010
DidOpenTextDocumentNotification,
1111
DidChangeTextDocumentNotification,
1212
DidCloseTextDocumentNotification,
13-
Location,
13+
CompletionItem,
14+
Hover,
1415
} from "vscode-languageserver-protocol";
1516
import * as utils from "./utils";
1617
import * as c from "./constants";
1718
import * as chokidar from "chokidar";
1819
import { assert } from "console";
1920
import { fileURLToPath } from "url";
2021
import { ChildProcess } from "child_process";
21-
import {
22-
runCompletionCommand, runDefinitionCommand, runHoverCommand,
23-
} from "./RescriptEditorSupport";
22+
import { Location } from "vscode-languageserver";
2423

2524
// https://microsoft.github.io/language-server-protocol/specification#initialize
2625
// According to the spec, there could be requests before the 'initialize' request. Link in comment tells how to handle them.
@@ -341,30 +340,59 @@ function onMessage(msg: m.Message) {
341340
send(response);
342341
}
343342
} else if (msg.method === p.HoverRequest.method) {
343+
let result: Hover | null = utils.runAnalysisAfterSanityCheck(
344+
msg,
345+
(filePath) => [
346+
"hover",
347+
filePath,
348+
msg.params.position.line,
349+
msg.params.position.character,
350+
]
351+
);
344352
let hoverResponse: m.ResponseMessage = {
345353
jsonrpc: c.jsonrpcVersion,
346354
id: msg.id,
347355
// type result = Hover | null
348356
// type Hover = {contents: MarkedString | MarkedString[] | MarkupContent, range?: Range}
349-
result: runHoverCommand(msg),
357+
result,
350358
};
351359
send(hoverResponse);
352360
} else if (msg.method === p.DefinitionRequest.method) {
353361
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
362+
let result: Location | null = utils.runAnalysisAfterSanityCheck(
363+
msg,
364+
(filePath) => [
365+
"definition",
366+
filePath,
367+
msg.params.position.line,
368+
msg.params.position.character,
369+
]
370+
);
354371
let definitionResponse: m.ResponseMessage = {
355372
jsonrpc: c.jsonrpcVersion,
356373
id: msg.id,
357-
// result should be: Location | Array<Location> | Array<LocationLink> | null
358-
result: runDefinitionCommand(msg),
374+
result,
359375
// error: code and message set in case an exception happens during the definition request.
360376
};
361377
send(definitionResponse);
362378
} else if (msg.method === p.CompletionRequest.method) {
363379
let code = getOpenedFileContent(msg.params.textDocument.uri);
380+
let tmpname = utils.createFileInTempDir();
381+
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
382+
let result:
383+
| CompletionItem[]
384+
| null = utils.runAnalysisAfterSanityCheck(msg, (filePath) => [
385+
"complete",
386+
filePath,
387+
msg.params.position.line,
388+
msg.params.position.character,
389+
tmpname,
390+
]);
391+
fs.unlink(tmpname, () => null);
364392
let completionResponse: m.ResponseMessage = {
365393
jsonrpc: c.jsonrpcVersion,
366394
id: msg.id,
367-
result: runCompletionCommand(msg, code),
395+
result,
368396
};
369397
send(completionResponse);
370398
} else if (msg.method === p.DocumentFormattingRequest.method) {

server/src/utils.ts

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as path from "path";
55
import * as t from "vscode-languageserver-types";
66
import fs from "fs";
77
import * as os from "os";
8+
import { fileURLToPath } from "url";
9+
import { RequestMessage } from "vscode-languageserver";
810

911
let tempFilePrefix = "rescript_format_file_" + process.pid + "_";
1012
let tempFileId = 0;
@@ -102,6 +104,30 @@ export let formatUsingValidBscExePath = (
102104
}
103105
};
104106

107+
export let runAnalysisAfterSanityCheck = (
108+
msg: RequestMessage,
109+
getArgs: (filePath: string) => Array<string>
110+
) => {
111+
let binaryPath;
112+
if (fs.existsSync(c.analysisCurrentPlatformBinaryPath)) {
113+
binaryPath = c.analysisCurrentPlatformBinaryPath;
114+
} else if (fs.existsSync(c.analysisProductionBinaryPath)) {
115+
binaryPath = c.analysisProductionBinaryPath;
116+
} else {
117+
return null;
118+
}
119+
120+
let filePath = fileURLToPath(msg.params.textDocument.uri);
121+
let projectRootPath = findProjectRootOfFile(filePath);
122+
if (projectRootPath == null) {
123+
return null;
124+
}
125+
let stdout = childProcess.execFileSync(binaryPath, getArgs(filePath), {
126+
cwd: projectRootPath,
127+
});
128+
return JSON.parse(stdout.toString());
129+
};
130+
105131
export let runBsbWatcherUsingValidBsbNodePath = (
106132
bsbNodePath: p.DocumentUri,
107133
projectRootPath: p.DocumentUri

0 commit comments

Comments
 (0)