From c3a9d977e5b9643c7a13fa45492f0ca454c287b0 Mon Sep 17 00:00:00 2001 From: Dervex <78505208+DervexHero@users.noreply.github.com> Date: Sat, 30 Dec 2023 14:02:53 +0100 Subject: [PATCH] Add status bar instead of error notifications (VSC extension) (#826) * Add option to hide formatting errors * Add status bar item and remove notification setting * Switch to language status item and update on editor change * Update changelog --------- Co-authored-by: JohnnyMorganz --- stylua-vscode/CHANGELOG.md | 4 ++++ stylua-vscode/package.json | 7 +++++- stylua-vscode/src/extension.ts | 44 ++++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/stylua-vscode/CHANGELOG.md b/stylua-vscode/CHANGELOG.md index 9f8bde8e..c560d204 100644 --- a/stylua-vscode/CHANGELOG.md +++ b/stylua-vscode/CHANGELOG.md @@ -11,6 +11,10 @@ To view the changelog of the StyLua binary, see [here](https://github.com/Johnny ## [Unreleased] +### Changed + +- Removed excessive error notifications on formatting failure and replaced with VSCode language status bar item + ## [1.5.0] - 2023-03-11 ### Added diff --git a/stylua-vscode/package.json b/stylua-vscode/package.json index eaa5f65a..67856700 100644 --- a/stylua-vscode/package.json +++ b/stylua-vscode/package.json @@ -42,6 +42,11 @@ "command": "stylua.authenticate", "title": "Authorize StyLua to use GitHub API", "category": "StyLua" + }, + { + "command": "stylua.showOutputChannel", + "title": "Show Output Channel", + "category": "StyLua" } ], "configuration": { @@ -87,7 +92,7 @@ "stylua.disableVersionCheck": { "type": "boolean", "default": false, - "description": "Disable checking the version of stylua for newer versions. Useful if you do not want network requests" + "description": "Disable checking the version of stylua for newer versions. Useful if you do not want network requests." }, "stylua.searchParentDirectories": { "type": "boolean", diff --git a/stylua-vscode/src/extension.ts b/stylua-vscode/src/extension.ts index b749cc81..fc82a8de 100644 --- a/stylua-vscode/src/extension.ts +++ b/stylua-vscode/src/extension.ts @@ -24,6 +24,11 @@ const byteOffset = ( export async function activate(context: vscode.ExtensionContext) { console.log("stylua activated"); + const outputChannel = vscode.window.createOutputChannel("StyLua", { + log: true, + }); + outputChannel.info("StyLua activated"); + const github = new GitHub(); context.subscriptions.push(github); @@ -48,6 +53,12 @@ export async function activate(context: vscode.ExtensionContext) { }) ); + context.subscriptions.push( + vscode.commands.registerCommand("stylua.showOutputChannel", async () => { + outputChannel.show(); + }) + ); + context.subscriptions.push( vscode.workspace.onDidChangeConfiguration(async (change) => { if (change.affectsConfiguration("stylua")) { @@ -58,8 +69,22 @@ export async function activate(context: vscode.ExtensionContext) { }) ); + const documentSelector = ["lua", "luau"]; + + const languageStatusItem = vscode.languages.createLanguageStatusItem( + "stylua", + documentSelector + ); + languageStatusItem.name = "StyLua"; + languageStatusItem.text = "$(check) StyLua"; + languageStatusItem.detail = "Ready"; + languageStatusItem.command = { + title: "Show Output", + command: "stylua.showOutputChannel", + }; + let disposable = vscode.languages.registerDocumentRangeFormattingEditProvider( - ["lua", "luau"], + documentSelector, { async provideDocumentRangeFormattingEdits( document: vscode.TextDocument, @@ -112,9 +137,16 @@ export async function activate(context: vscode.ExtensionContext) { fullDocumentRange, formattedText ); + languageStatusItem.text = "$(check) StyLua"; + languageStatusItem.detail = "File formatted successfully"; + languageStatusItem.severity = + vscode.LanguageStatusSeverity.Information; return [format]; } catch (err) { - vscode.window.showErrorMessage(`Could not format file: ${err}`); + languageStatusItem.text = "StyLua"; + languageStatusItem.detail = "Failed to format file"; + languageStatusItem.severity = vscode.LanguageStatusSeverity.Error; + outputChannel.error(err as string); return []; } }, @@ -122,6 +154,14 @@ export async function activate(context: vscode.ExtensionContext) { ); context.subscriptions.push(disposable); + context.subscriptions.push(languageStatusItem); + context.subscriptions.push( + vscode.window.onDidChangeActiveTextEditor((editor) => { + languageStatusItem.text = "$(check) StyLua"; + languageStatusItem.detail = "Ready"; + languageStatusItem.severity = vscode.LanguageStatusSeverity.Information; + }) + ); } // this method is called when your extension is deactivated