From b9c754c7246e6b73cb58d80b79c617f00d3caf09 Mon Sep 17 00:00:00 2001 From: Juanra GM Date: Mon, 3 Oct 2022 09:55:26 +0200 Subject: [PATCH] refactor: extract `setStatusBar` method --- src/commands/analyse.ts | 6 +----- src/commands/clearCache.ts | 10 +++++----- src/commands/pauseFileWatcher.ts | 10 +++++----- src/extension.ts | 22 ++++++++++++++++++++-- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/commands/analyse.ts b/src/commands/analyse.ts index 9fae575..9d2e2aa 100644 --- a/src/commands/analyse.ts +++ b/src/commands/analyse.ts @@ -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) { diff --git a/src/commands/clearCache.ts b/src/commands/clearCache.ts index da7a68c..df45f73 100644 --- a/src/commands/clearCache.ts +++ b/src/commands/clearCache.ts @@ -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, diff --git a/src/commands/pauseFileWatcher.ts b/src/commands/pauseFileWatcher.ts index 2afdf3c..38f4d39 100644 --- a/src/commands/pauseFileWatcher.ts +++ b/src/commands/pauseFileWatcher.ts @@ -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(); } diff --git a/src/extension.ts b/src/extension.ts index f55f321..f2b2057 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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(); }