From e67d7e27f3bb544e4215ba14cce7533e79a22d37 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Mon, 13 May 2024 22:49:41 +0900 Subject: [PATCH] feat: support github-actions-workflow language (#9) --- extension.js | 89 ++++++++++++++++++++++++++++------------------------ package.json | 3 +- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/extension.js b/extension.js index 1771bae..6789a48 100644 --- a/extension.js +++ b/extension.js @@ -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() { } diff --git a/package.json b/package.json index cb007a2..696e6cb 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "multi-root ready" ], "activationEvents": [ - "onLanguage:yaml" + "onLanguage:yaml", + "onLanguage:github-actions-workflow" ], "main": "./extension.js", "contributes": {