This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
forked from bluebrown/vscode-extension-yamlfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
64 lines (53 loc) · 1.79 KB
/
extension.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
58
59
60
61
62
63
64
import { spawnSync } from "node:child_process";
import { dirname } from "node:path";
import * as vscode from "vscode";
const yamlformattedLanguages = [
"yaml",
"github-actions-workflow", // Provided in https://github.com/github/vscode-github-actions
];
const provider = {
provideDocumentFormattingEdits(document: vscode.TextDocument) {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
const config = vscode.workspace.getConfiguration("", document.uri);
const baseArgs = config.get("yamlfmt.args", []).filter(arg => arg !== "-in");
const args = [...baseArgs, "-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,
};