Skip to content

Commit

Permalink
refactor: extract setStatusBar method
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Oct 3, 2022
1 parent 22e3c81 commit b9c754c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
6 changes: 1 addition & 5 deletions src/commands/analyse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ import { spawn } from "child_process";
import { Diagnostic, DiagnosticSeverity, Range, Uri } from "vscode";

function setStatusBarProgress(ext: Ext, progress?: number) {
const { statusBarItem } = ext;
let text = "$(sync~spin) PHPStan analysing...";
if (!!progress && progress > 0) text += ` (${progress}%)`;

statusBarItem.text = text;
statusBarItem.tooltip = "";
statusBarItem.command = ext.getCommandName(showOutput);
statusBarItem.show();
ext.setStatusBar({ text, command: showOutput });
}

async function refreshDiagnostics(ext: Ext, result: PHPStanAnalyseResult) {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/clearCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import showOutput from "./showOutput";
import { spawn } from "child_process";

export async function clearCache(ext: Ext) {
const { statusBarItem, settings } = ext;
const { settings } = ext;

statusBarItem.text = "$(sync~spin) PHPStan clearing cache...";
statusBarItem.tooltip = "";
statusBarItem.command = ext.getCommandName(showOutput);
statusBarItem.show();
ext.setStatusBar({
text: "$(sync~spin) PHPStan clearing cache...",
command: showOutput,
});

const childProcess = spawn(
settings.phpPath,
Expand Down
10 changes: 5 additions & 5 deletions src/commands/pauseFileWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Ext } from "../extension";
import resumeFileWatcher from "./resumeFileWatcher";

export default function pauseFileWatcher(ext: Ext) {
const { statusBarItem } = ext;
ext.setStatusBar({
text: "$(debug-pause) PHPStan",
tooltip: "Resume file watcher",
command: resumeFileWatcher,
});
ext.store.fileWatcher.enabled = false;
statusBarItem.text = "$(debug-pause) PHPStan";
statusBarItem.tooltip = "Resume file watcher";
statusBarItem.command = ext.getCommandName(resumeFileWatcher);
statusBarItem.show();
}
22 changes: 20 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,28 @@ export class Ext<
};
}

setStatusBar(data: {
text: string;
tooltip?: string;
command?: string | ((ext: Ext) => void);
}) {
this.statusBarItem.text = data.text;
this.statusBarItem.tooltip =
typeof data.tooltip === "string" ? data.tooltip : undefined;
if (typeof data.command === "string") {
this.statusBarItem.command = data.command;
} else if (typeof data.command === "function") {
this.statusBarItem.command = this.getCommandName(data.command);
} else {
this.statusBarItem.command = undefined;
}
this.statusBarItem.show();
}

clearStatusBar() {
this.statusBarItem.text = "";
delete this.statusBarItem.tooltip;
delete this.statusBarItem.command;
this.statusBarItem.tooltip = undefined;
this.statusBarItem.command = undefined;
this.statusBarItem.hide();
}

Expand Down

0 comments on commit b9c754c

Please sign in to comment.