Skip to content

Commit

Permalink
Add status bar instead of error notifications (VSC extension) (#826)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
DervexDev and JohnnyMorganz authored Dec 30, 2023
1 parent a61d983 commit c3a9d97
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 4 additions & 0 deletions stylua-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion stylua-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
44 changes: 42 additions & 2 deletions stylua-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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")) {
Expand All @@ -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,
Expand Down Expand Up @@ -112,16 +137,31 @@ 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 [];
}
},
}
);

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
Expand Down

0 comments on commit c3a9d97

Please sign in to comment.