-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
86 lines (81 loc) · 2.54 KB
/
extension.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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode");
const path = require("path");
const http = require("http");
const { spawn } = require("child_process");
const port = 5858;
const child = spawn("wolframscript", [
"-f",
path.join(__dirname, "server.wls"),
port.toString(),
]);
console.debug(`server pid = ${child.pid}`);
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
context.subscriptions.push(
vscode.languages.registerDocumentFormattingEditProvider(
{ language: "wolfram" },
{
async provideDocumentFormattingEdits(document) {
const cellContent = [];
for (let i = 0; i < document.lineCount; i++) {
cellContent.push(document.lineAt(i).text);
}
const cellContentString = cellContent.join("\n");
return [
vscode.TextEdit.replace(
new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(
document.lineCount - 1,
document.lineAt(document.lineCount - 1).text.length
)
),
await new Promise((resolve) => {
const chunks = [];
const req = http.request(
{
hostname: "127.0.0.1",
port,
path: "/",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": cellContentString.length,
},
},
(res) => {
res.on("data", (chunk) => chunks.push(chunk));
res.on("end", () => {
const data = Buffer.concat(chunks).toString();
console.debug(data);
resolve(data);
});
}
);
req.write(cellContentString);
req.end();
})
),
];
},
}
)
);
}
// This method is called when your extension is deactivated
function deactivate() {
child.stdin.end();
child.stdout.destroy();
child.stderr.destroy();
child.kill("SIGKILL");
}
module.exports = {
activate,
deactivate,
};