-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
65 lines (54 loc) · 1.83 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
const { spawnSync } = require("node:child_process");
const { dirname } = require("node:path");
const vscode = require("vscode");
const yamlformattedLanguages = [
"yaml",
"github-actions-workflow", // Provided in https://github.com/github/vscode-github-actions
"dockercompose", // Provided in https://github.com/Microsoft/vscode-docker
];
const provider = {
provideDocumentFormattingEdits(document) {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
const config = vscode.workspace.getConfiguration("", document.uri);
const args = config.get("yamlfmt.args", []).filter(arg => arg !== "-in");
args.push("-in");
const result = spawnSync("yamlfmt", args, {
cwd: workspaceFolder ? workspaceFolder.uri.fsPath : dirname(document.uri.fsPath),
input: document.getText(),
});
if (result.error) {
console.error(result.error);
const prefix = "spawnSync ";
vscode.window.showErrorMessage(
result.error.message.startsWith(prefix) ?
result.error.message.substring(prefix.length) :
result.error.message
);
return [];
}
if (result.stderr.length > 0) {
console.log(result.stderr.toString());
vscode.window.showErrorMessage(result.stderr.toString());
return [];
}
if (result.stdout.length < 1) {
console.warn("yamlfmt's stdout buffer is empty");
return [];
}
const range = new vscode.Range(
document.lineAt(0).range.start,
document.lineAt(document.lineCount - 1).range.end,
);
return [vscode.TextEdit.replace(range, result.stdout.toString())];
}
};
function activate() {
for (const lang of yamlformattedLanguages) {
vscode.languages.registerDocumentFormattingEditProvider(lang, provider);
}
}
function deactivate() { }
module.exports = {
activate,
deactivate
};