-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformattingProvider.ts
57 lines (49 loc) · 1.63 KB
/
formattingProvider.ts
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
import * as vscode from 'vscode';
import * as cp from 'child_process';
import * as os from 'os';
import { log } from './log';
export async function provideDocumentFormattingEdits(document: vscode.TextDocument, _options: vscode.FormattingOptions): Promise<vscode.TextEdit[]> {
let shardsPath = vscode.workspace.getConfiguration("shards").get<string>("shardsPath");
if (shardsPath) {
log(`Using shards path: ${shardsPath}`);
// Spawn the child process
// let wsFolder = vscode.workspace.getWorkspaceFolder(document.uri);
let cwd = /* wsFolder ? wsFolder.uri.fsPath : */ os.tmpdir();
const child = cp.spawn(shardsPath, ['format', '--', '-'], {
cwd: cwd,
});
// Handle stdout
let output = "";
let haveErrors = false;
child.stdout.on('data', (data) => {
log('Program output: ' + data);
output += data.toString();
});
// Handle stderr
child.stderr.on('data', (data) => {
const errorOutput = data.toString();
log('Program error: ' + errorOutput);
});
// Handle errors and exit
child.on('error', (err) => {
console.error('Error: ' + err.message);
haveErrors = true;
});
// Write data to the child process's stdin
child.stdin.write(document.getText());
child.stdin.end(); // Close stdin when you're done writing
await new Promise((resolve, reject) => child.on('exit', (code) => {
log(`Child process exited with code ${code}`);
if (code === 0) {
resolve(0);
} else {
reject();
}
}));
return [vscode.TextEdit.replace(new vscode.Range(0, 0, document.lineCount, 0), output)];
} else {
log(`Formatter path not set, please set 'shards.path'`);
}
log(`Formatting failed`);
return [];
}