From af0614e2197c1a9dbd60d1867981373e2b107d9d Mon Sep 17 00:00:00 2001 From: Dervex Date: Sun, 19 Nov 2023 16:42:05 +0100 Subject: [PATCH 1/4] Add option to hide formatting errors --- stylua-vscode/package.json | 7 ++++++- stylua-vscode/src/extension.ts | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/stylua-vscode/package.json b/stylua-vscode/package.json index df2a4553..cbec987b 100644 --- a/stylua-vscode/package.json +++ b/stylua-vscode/package.json @@ -87,12 +87,17 @@ "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", "default": false, "description": "Search parent directories for a stylua configuration file if one is not directly available." + }, + "stylua.hideFormattingErrors": { + "type": "boolean", + "default": false, + "description": "Hide error notifications when file formatting fails. Recommended when using format on paste or save." } } } diff --git a/stylua-vscode/src/extension.ts b/stylua-vscode/src/extension.ts index b749cc81..478f85f1 100644 --- a/stylua-vscode/src/extension.ts +++ b/stylua-vscode/src/extension.ts @@ -114,7 +114,13 @@ export async function activate(context: vscode.ExtensionContext) { ); return [format]; } catch (err) { - vscode.window.showErrorMessage(`Could not format file: ${err}`); + if ( + !vscode.workspace + .getConfiguration("stylua") + .get("hideFormattingErrors") + ) { + vscode.window.showErrorMessage(`Could not format file: ${err}`); + } return []; } }, From 5920926c41321377f1f9a2d97a6009c2674992cb Mon Sep 17 00:00:00 2001 From: Dervex Date: Sun, 19 Nov 2023 18:15:56 +0100 Subject: [PATCH 2/4] Add status bar item and remove notification setting --- stylua-vscode/package.json | 5 ----- stylua-vscode/src/extension.ts | 12 +++++------- stylua-vscode/src/status.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 stylua-vscode/src/status.ts diff --git a/stylua-vscode/package.json b/stylua-vscode/package.json index cbec987b..6cd25650 100644 --- a/stylua-vscode/package.json +++ b/stylua-vscode/package.json @@ -93,11 +93,6 @@ "type": "boolean", "default": false, "description": "Search parent directories for a stylua configuration file if one is not directly available." - }, - "stylua.hideFormattingErrors": { - "type": "boolean", - "default": false, - "description": "Hide error notifications when file formatting fails. Recommended when using format on paste or save." } } } diff --git a/stylua-vscode/src/extension.ts b/stylua-vscode/src/extension.ts index 478f85f1..34ed9e1b 100644 --- a/stylua-vscode/src/extension.ts +++ b/stylua-vscode/src/extension.ts @@ -2,6 +2,7 @@ import * as vscode from "vscode"; import { formatCode, checkIgnored } from "./stylua"; import { GitHub } from "./github"; import { StyluaDownloader } from "./download"; +import { Status } from "./status"; /** * Convert a Position within a Document to a byte offset. @@ -58,6 +59,8 @@ export async function activate(context: vscode.ExtensionContext) { }) ); + let status = new Status(); + let disposable = vscode.languages.registerDocumentRangeFormattingEditProvider( ["lua", "luau"], { @@ -112,15 +115,10 @@ export async function activate(context: vscode.ExtensionContext) { fullDocumentRange, formattedText ); + status.update(null); return [format]; } catch (err) { - if ( - !vscode.workspace - .getConfiguration("stylua") - .get("hideFormattingErrors") - ) { - vscode.window.showErrorMessage(`Could not format file: ${err}`); - } + status.update(err); return []; } }, diff --git a/stylua-vscode/src/status.ts b/stylua-vscode/src/status.ts new file mode 100644 index 00000000..8e78b351 --- /dev/null +++ b/stylua-vscode/src/status.ts @@ -0,0 +1,32 @@ +import { window, StatusBarAlignment, StatusBarItem, ThemeColor } from "vscode"; + +export class Status { + private status: StatusBarItem; + + constructor() { + this.status = window.createStatusBarItem( + "stylua.status", + StatusBarAlignment.Right, + -1 + ); + + this.status.name = "StyLua"; + this.status.text = "StyLua"; + + this.status.show(); + } + + public update(err: unknown) { + if (err) { + this.status.text = "$(warning) StyLua"; + this.status.tooltip = `Could not format file: ${err}`; + this.status.backgroundColor = new ThemeColor( + "statusBarItem.errorBackground" + ); + } else { + this.status.text = "$(check) StyLua"; + this.status.tooltip = "File formatted successfully"; + this.status.backgroundColor = new ThemeColor("statusBarItem"); + } + } +} From ca7a2a48f9f06fc4296f6ea2e51375e5ac7acfe1 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 30 Dec 2023 13:56:41 +0100 Subject: [PATCH 3/4] Switch to language status item and update on editor change --- stylua-vscode/package.json | 5 ++++ stylua-vscode/src/extension.ts | 46 ++++++++++++++++++++++++++++++---- stylua-vscode/src/status.ts | 32 ----------------------- 3 files changed, 46 insertions(+), 37 deletions(-) delete mode 100644 stylua-vscode/src/status.ts diff --git a/stylua-vscode/package.json b/stylua-vscode/package.json index 6cd25650..33f90d12 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": { diff --git a/stylua-vscode/src/extension.ts b/stylua-vscode/src/extension.ts index 34ed9e1b..fc82a8de 100644 --- a/stylua-vscode/src/extension.ts +++ b/stylua-vscode/src/extension.ts @@ -2,7 +2,6 @@ import * as vscode from "vscode"; import { formatCode, checkIgnored } from "./stylua"; import { GitHub } from "./github"; import { StyluaDownloader } from "./download"; -import { Status } from "./status"; /** * Convert a Position within a Document to a byte offset. @@ -25,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); @@ -49,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")) { @@ -59,10 +69,22 @@ export async function activate(context: vscode.ExtensionContext) { }) ); - let status = new Status(); + 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, @@ -115,10 +137,16 @@ export async function activate(context: vscode.ExtensionContext) { fullDocumentRange, formattedText ); - status.update(null); + languageStatusItem.text = "$(check) StyLua"; + languageStatusItem.detail = "File formatted successfully"; + languageStatusItem.severity = + vscode.LanguageStatusSeverity.Information; return [format]; } catch (err) { - status.update(err); + languageStatusItem.text = "StyLua"; + languageStatusItem.detail = "Failed to format file"; + languageStatusItem.severity = vscode.LanguageStatusSeverity.Error; + outputChannel.error(err as string); return []; } }, @@ -126,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 diff --git a/stylua-vscode/src/status.ts b/stylua-vscode/src/status.ts deleted file mode 100644 index 8e78b351..00000000 --- a/stylua-vscode/src/status.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { window, StatusBarAlignment, StatusBarItem, ThemeColor } from "vscode"; - -export class Status { - private status: StatusBarItem; - - constructor() { - this.status = window.createStatusBarItem( - "stylua.status", - StatusBarAlignment.Right, - -1 - ); - - this.status.name = "StyLua"; - this.status.text = "StyLua"; - - this.status.show(); - } - - public update(err: unknown) { - if (err) { - this.status.text = "$(warning) StyLua"; - this.status.tooltip = `Could not format file: ${err}`; - this.status.backgroundColor = new ThemeColor( - "statusBarItem.errorBackground" - ); - } else { - this.status.text = "$(check) StyLua"; - this.status.tooltip = "File formatted successfully"; - this.status.backgroundColor = new ThemeColor("statusBarItem"); - } - } -} From 4fad65b850270d5a4b71e830d246e83d404b5e90 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 30 Dec 2023 13:57:24 +0100 Subject: [PATCH 4/4] Update changelog --- stylua-vscode/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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