Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

exec -> execSync for invoking RescriptEditorSupport binary#82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
chenglou merged 1 commit intomasterfromsyncy
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 59 additions & 52 deletionsserver/src/RescriptEditorSupport.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,7 @@ 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 {execSync } from "child_process";
import fs from "fs";

let binaryPath = path.join(
Expand All@@ -27,67 +27,74 @@ let findExecutable = (uri: string) => {
}
};

export function runDumpCommand(
msg: RequestMessage,
onResult: (
result: { hover?: string; definition?: { uri?: string; range: any } } | null
) => void
) {
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) {
onResult(null);
} else {
let command =
executable.binaryPathQuoted +
" dump " +
executable.filePathQuoted +
":" +
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);
}
});
return null;
}

let command =
executable.binaryPathQuoted +
" dump " +
executable.filePathQuoted +
":" +
msg.params.position.line +
":" +
msg.params.position.character;

try {
let stdout = execSync(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?
Copy link
MemberAuthor

@chenglouchenglouFeb 6, 2021
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

cc@cristianoc. Same for the TODO a bit further

return null;
}
}

type completionCommandResult = [{ label: string }];
export function runCompletionCommand(
msg: RequestMessage,
code: string,
onResult: (result: [{ label: string }] | null) => void
) {
code: string
): completionCommandResult | null {
let executable = findExecutable(msg.params.textDocument.uri);
if (executable == null) {
onResult(null);
} else {
let tmpname = utils.createFileInTempDir();
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
returnnull;
}
let tmpname = utils.createFileInTempDir();
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });

let command =
executable.binaryPathQuoted +
" complete " +
executable.filePathQuoted +
":" +
msg.params.position.line +
":" +
msg.params.position.character +
" " +
tmpname;
let command =
executable.binaryPathQuoted +
" complete " +
executable.filePathQuoted +
":" +
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);
}
});
try {
let stdout = execSync(command, { 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);
}
}
71 changes: 33 additions & 38 deletionsserver/src/server.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -332,19 +332,16 @@ process.on("message", (msg: m.Message) => {
// type Hover = {contents: MarkedString | MarkedString[] | MarkupContent, range?: Range}
result: null,
};
runDumpCommand(msg, (result) => {
if (result && result.hover) {
let hoverResponse: m.ResponseMessage = {
...emptyHoverResponse,
result: {
contents: result.hover,
},
};
process.send!(hoverResponse);
} else {
process.send!(emptyHoverResponse);
}
});
let result = runDumpCommand(msg);
if (result !== null && result.hover != null) {
let hoverResponse: m.ResponseMessage = {
...emptyHoverResponse,
result: { contents: result.hover },
};
process.send!(hoverResponse);
} else {
process.send!(emptyHoverResponse);
}
} else if (msg.method === p.DefinitionRequest.method) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition
let emptyDefinitionResponse: m.ResponseMessage = {
Expand All@@ -355,38 +352,36 @@ process.on("message", (msg: m.Message) => {
// error: code and message set in case an exception happens during the definition request.
};

runDumpCommand(msg, (result) => {
if (result && result.definition) {
let definitionResponse: m.ResponseMessage = {
...emptyDefinitionResponse,
result: {
uri: result.definition.uri || msg.params.textDocument.uri,
range: result.definition.range,
},
};
process.send!(definitionResponse);
} else {
process.send!(emptyDefinitionResponse);
}
});
let result = runDumpCommand(msg);
if (result !== null && result.definition != null) {
let definitionResponse: m.ResponseMessage = {
...emptyDefinitionResponse,
result: {
uri: result.definition.uri || msg.params.textDocument.uri,
range: result.definition.range,
},
};
process.send!(definitionResponse);
} else {
process.send!(emptyDefinitionResponse);
}
} else if (msg.method === p.CompletionRequest.method) {
let emptyCompletionResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result: null,
};
let code = getOpenedFileContent(msg.params.textDocument.uri);
runCompletionCommand(msg, code, (result) => {
if (result) {
let definitionResponse: m.ResponseMessage = {
...emptyCompletionResponse,
result: result,
};
process.send!(definitionResponse);
} else {
process.send!(emptyCompletionResponse);
}
});
let result = runCompletionCommand(msg, code);
if (result === null) {
process.send!(emptyCompletionResponse);
} else {
let definitionResponse: m.ResponseMessage = {
...emptyCompletionResponse,
result: result,
};
process.send!(definitionResponse);
}
} else if (msg.method === p.DocumentFormattingRequest.method) {
// technically, a formatting failure should reply with the error. Sadly
// the LSP alert box for these error replies sucks (e.g. doesn't actually
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp