Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support github-actions-workflow language #9

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading