Skip to content

Commit

Permalink
server: better types for response result.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoc committed May 8, 2021
1 parent ee6e843 commit d9aa929
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ let projectsFiles: Map<
// ^ caching AND states AND distributed system. Why does LSP has to be stupid like this

// will be properly defined later depending on the mode (stdio/node-rpc)
let send: (msg: m.Message) => void = (_) => { };
let send: (msg: m.Message) => void = (_) => {};

interface CreateInterfaceRequestParams {
uri: string;
};
}

let createInterfaceRequest = new v.RequestType<CreateInterfaceRequestParams, string, void>("rescript-vscode.create_interface");
let createInterfaceRequest = new v.RequestType<
CreateInterfaceRequestParams,
string,
void
>("rescript-vscode.create_interface");

let sendUpdatedDiagnostics = () => {
projectsFiles.forEach(({ filesWithDiagnostics }, projectRootPath) => {
Expand Down Expand Up @@ -350,12 +354,10 @@ function onMessage(msg: m.Message) {
} else if (msg.method === p.HoverRequest.method) {
let params = msg.params as p.HoverParams;
let filePath = fileURLToPath(params.textDocument.uri);
let result: Hover | null = utils.runAnalysisAfterSanityCheck(filePath, [
"hover",
let result: typeof p.HoverRequest.type = utils.runAnalysisAfterSanityCheck(
filePath,
params.position.line,
params.position.character,
]);
["hover", filePath, params.position.line, params.position.character]
);
let hoverResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
Expand All @@ -368,14 +370,15 @@ function onMessage(msg: m.Message) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
let params = msg.params as p.DefinitionParams;
let filePath = fileURLToPath(params.textDocument.uri);
let result:
| Location[]
| null = utils.runAnalysisAfterSanityCheck(filePath, [
let result: typeof p.DefinitionRequest.type = utils.runAnalysisAfterSanityCheck(
filePath,
[
"definition",
filePath,
params.position.line,
params.position.character,
]);
]
);
let definitionResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
Expand All @@ -387,7 +390,7 @@ function onMessage(msg: m.Message) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
let params = msg.params as p.ReferenceParams;
let filePath = fileURLToPath(params.textDocument.uri);
let result: Location | null = utils.runAnalysisAfterSanityCheck(
let result: typeof p.ReferencesRequest.type = utils.runAnalysisAfterSanityCheck(
filePath,
[
"references",
Expand All @@ -407,12 +410,10 @@ function onMessage(msg: m.Message) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol
let params = msg.params as p.DocumentSymbolParams;
let filePath = fileURLToPath(params.textDocument.uri);
let result:
| SymbolInformation[]
| null = utils.runAnalysisAfterSanityCheck(filePath, [
"documentSymbol",
filePath,
]);
let result: typeof p.DocumentSymbolRequest.type = utils.runAnalysisAfterSanityCheck(
filePath,
["documentSymbol", filePath]
);
let definitionResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
Expand All @@ -425,15 +426,16 @@ function onMessage(msg: m.Message) {
let code = getOpenedFileContent(params.textDocument.uri);
let tmpname = utils.createFileInTempDir();
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
let result:
| CompletionItem[]
| null = utils.runAnalysisAfterSanityCheck(filePath, [
let result: typeof p.CompletionRequest.type = utils.runAnalysisAfterSanityCheck(
filePath,
[
"completion",
filePath,
params.position.line,
params.position.character,
tmpname,
]);
]
);
fs.unlink(tmpname, () => null);
let completionResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
Expand Down Expand Up @@ -554,14 +556,21 @@ function onMessage(msg: m.Message) {

send(response);
} else {
let cmiPartialPath = utils.replaceFileExtension(filePath.split(projDir)[1], c.cmiExt);
let cmiPath = path.join(projDir, c.compilerDirPartialPath, cmiPartialPath);
let cmiPartialPath = utils.replaceFileExtension(
filePath.split(projDir)[1],
c.cmiExt
);
let cmiPath = path.join(
projDir,
c.compilerDirPartialPath,
cmiPartialPath
);
let cmiAvailable = fs.existsSync(cmiPath);

if (!cmiAvailable) {
let params: p.ShowMessageParams = {
type: p.MessageType.Error,
message: `No compiled interface file found. Please compile your project first.`
message: `No compiled interface file found. Please compile your project first.`,
};

let response: m.NotificationMessage = {
Expand All @@ -572,7 +581,11 @@ function onMessage(msg: m.Message) {

send(response);
} else {
let intfResult = utils.createInterfaceFileUsingValidBscExePath(filePath, cmiPath, bscNativePath)
let intfResult = utils.createInterfaceFileUsingValidBscExePath(
filePath,
cmiPath,
bscNativePath
);

if (intfResult.kind === "success") {
let response: m.ResponseMessage = {
Expand All @@ -588,8 +601,8 @@ function onMessage(msg: m.Message) {
id: msg.id,
error: {
code: m.ErrorCodes.InternalError,
message: "Unable to create interface file."
}
message: "Unable to create interface file.",
},
};

send(response);
Expand Down

0 comments on commit d9aa929

Please sign in to comment.