-
Notifications
You must be signed in to change notification settings - Fork 8
/
smlEnvironmentManager.js
109 lines (90 loc) · 2.21 KB
/
smlEnvironmentManager.js
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
101
102
103
104
105
106
107
108
109
const spawn = require("child_process").spawn;
const vscode = require("vscode");
let sml;
const smlOutput = vscode.window.createOutputChannel("SML");
let allowNextCommand;
function start() {
allowNextCommand = false;
const interpreter = vscode.workspace
.getConfiguration()
.get("sml-environment-interpreter-path", "sml");
var cwd = {};
if (vscode.workspace.workspaceFolders !== undefined) {
var wd = vscode.workspace.workspaceFolders[0].uri.fsPath;
console.log("setting path to: " + wd);
cwd = { cwd: wd };
} else {
console.log("Unable to set working directory, no current workspace folder");
}
sml = spawn(interpreter, [], Object.assign({ shell: true }, cwd));
sml.stdin.setEncoding("utf-8");
sml.stdout.setEncoding("utf-8");
sml.stderr.setEncoding("utf-8");
console.log("started");
sml.stdin.read(0);
sml.on("error", function (err) {
console.log(err);
smlOutput.append(err.message);
});
sml.stderr.on("data", (data) => {
smlOutput.show(false);
smlOutput.append(data + `\n`);
allowNextCommand = true;
});
sml.stdout.on("data", (data) => {
smlOutput.show(false);
smlOutput.append(data + `\n`);
});
smlOutput.show(false);
}
async function execCode(code) {
while (!sml && !allowNextCommand) { ; }
if (sml.exitCode === 0 || sml.exitCode)
vscode.window.showErrorMessage("SML process died");
else {
try {
allowNextCommand = false;
sml.stdin.write(code + ";;;;\r\n");
} catch (error) {
smlOutput.append(error.message);
}
}
await vscode.commands.executeCommand(
"workbench.action.terminal.scrollToBottom"
);
}
async function execShortCode() {
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document;
const selection = editor.selection;
const code = document.getText(selection);
execCode(code);
}
}
async function execCurrentFile() {
restart()
const editor = vscode.window.activeTextEditor;
if (editor) {
const document = editor.document
const code = document.getText()
execCode(code);
}
}
function restart() {
if (sml.exitCode !== 0 && !sml.exitCode) {
sml.stdin.end();
}
sml.kill();
start();
}
function stop() {
sml.stdin.end();
}
module.exports = {
start,
stop,
restart,
execShortCode,
execCurrentFile
};