Skip to content

Commit

Permalink
feat: support github-actions-workflow language (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick authored May 13, 2024
1 parent 711e4cf commit e67d7e2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
89 changes: 48 additions & 41 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,58 @@ 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
];
const provider = {
provideDocumentFormattingEdits(document) {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
const config = vscode.workspace.getConfiguration("", document.uri);

function activate() {
vscode.languages.registerDocumentFormattingEditProvider("yaml", {
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,
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 [];
}

return [vscode.TextEdit.replace(range, result.stdout.toString())];
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() { }
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"multi-root ready"
],
"activationEvents": [
"onLanguage:yaml"
"onLanguage:yaml",
"onLanguage:github-actions-workflow"
],
"main": "./extension.js",
"contributes": {
Expand Down

0 comments on commit e67d7e2

Please sign in to comment.