From 08dacee104cb54f26de1974a233bd57c0ca574e6 Mon Sep 17 00:00:00 2001 From: Hana Date: Wed, 8 Dec 2021 14:14:26 -0500 Subject: [PATCH] src/goStatus: assign id/name to each status bar item Adopt the new `createStatusBarItem` API which allows users to control visibility per each status bar item the extension creates. This change also addresses an issue that caused the missing analysis tool error status overridden by the go version release notification message, because now the extension creates separate status bar items for these two different notifications. Fixes golang/vscode-go#1571 Change-Id: I6f457bace0385731a0218ccffebfba66e2fbb74b Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/370354 Reviewed-by: Suzy Mueller Trust: Hyang-Ah Hana Kim Run-TryBot: Hyang-Ah Hana Kim TryBot-Result: kokoro --- src/goCheck.ts | 4 +++- src/goEnvironmentStatus.ts | 10 ++++++++-- src/goInstallTools.ts | 15 +++++++++++---- src/goMain.ts | 9 +-------- src/goStatus.ts | 38 ++++++++++++++++++++++++++++++-------- src/testUtils.ts | 4 +++- 6 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/goCheck.ts b/src/goCheck.ts index c4d710bc07..021b52dfdf 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -20,7 +20,9 @@ import { goVet } from './goVet'; import { getTestFlags, goTest, TestConfig } from './testUtils'; import { ICheckResult } from './util'; -const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); +const STATUS_BAR_ITEM_NAME = 'Go Test'; +const statusBarItem = vscode.window.createStatusBarItem(STATUS_BAR_ITEM_NAME, vscode.StatusBarAlignment.Left); +statusBarItem.name = STATUS_BAR_ITEM_NAME; statusBarItem.command = 'go.test.showOutput'; const neverAgain = { title: "Don't Show Again" }; diff --git a/src/goEnvironmentStatus.ts b/src/goEnvironmentStatus.ts index 87f7473335..8b11e48fba 100644 --- a/src/goEnvironmentStatus.ts +++ b/src/goEnvironmentStatus.ts @@ -555,6 +555,7 @@ export async function getLatestGoVersions(): Promise { return results; } +const STATUS_BAR_ITEM_NAME = 'Go Notification'; const dismissedGoVersionUpdatesKey = 'dismissedGoVersionUpdates'; export async function offerToInstallLatestGoVersion() { @@ -585,7 +586,12 @@ export async function offerToInstallLatestGoVersion() { // notify user that there is a newer version of Go available if (options.length > 0) { - addGoStatus('Go Update Available', 'go.promptforgoinstall', 'A newer version of Go is available'); + addGoStatus( + STATUS_BAR_ITEM_NAME, + 'Go Update Available', + 'go.promptforgoinstall', + 'A newer version of Go is available' + ); vscode.commands.registerCommand('go.promptforgoinstall', () => { const download = { title: 'Download', @@ -630,7 +636,7 @@ export async function offerToInstallLatestGoVersion() { // TODO: should we removeGoStatus if user has closed the notification // without any action? It's kind of a feature now - without selecting // neverAgain, user can hide this statusbar item. - removeGoStatus(); + removeGoStatus(STATUS_BAR_ITEM_NAME); selection?.command(); }); }); diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts index 5fc4aa519c..1b02cc558f 100644 --- a/src/goInstallTools.ts +++ b/src/goInstallTools.ts @@ -44,6 +44,8 @@ import util = require('util'); import vscode = require('vscode'); import { isInPreviewMode, RestartReason } from './goLanguageServer'; +const STATUS_BAR_ITEM_NAME = 'Go Tools'; + // declinedUpdates tracks the tools that the user has declined to update. const declinedUpdates: Tool[] = []; @@ -395,7 +397,7 @@ Please select "Install", or follow the installation instructions [here](https:// break; case 'Install All': await installTools(missing, goVersion); - removeGoStatus(); + removeGoStatus(STATUS_BAR_ITEM_NAME); break; default: // The user has declined to install this tool. @@ -555,12 +557,17 @@ export async function offerToInstallTools() { let missing = await getMissingTools(goVersion); missing = missing.filter((x) => x.isImportant); if (missing.length > 0) { - addGoStatus('Analysis Tools Missing', 'go.promptforinstall', 'Not all Go tools are available on the GOPATH'); + addGoStatus( + STATUS_BAR_ITEM_NAME, + 'Analysis Tools Missing', + 'go.promptforinstall', + 'Not all Go tools are available on the GOPATH' + ); vscode.commands.registerCommand('go.promptforinstall', () => { const installItem = { title: 'Install', async command() { - removeGoStatus(); + removeGoStatus(STATUS_BAR_ITEM_NAME); await installTools(missing, goVersion); } }; @@ -583,7 +590,7 @@ export async function offerToInstallTools() { if (selection) { selection.command(); } else { - removeGoStatus(); + removeGoStatus(STATUS_BAR_ITEM_NAME); } }); }); diff --git a/src/goMain.ts b/src/goMain.ts index 8bcb57e3b0..b2cd019df6 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -100,14 +100,7 @@ import { resolvePath, runGoVersionM } from './util'; -import { - clearCacheForTools, - fileExists, - getCurrentGoRoot, - dirExists, - setCurrentGoRoot, - envPath -} from './utils/pathUtils'; +import { clearCacheForTools, fileExists, getCurrentGoRoot, dirExists, envPath } from './utils/pathUtils'; import { WelcomePanel } from './welcome'; import semver = require('semver'); import vscode = require('vscode'); diff --git a/src/goStatus.ts b/src/goStatus.ts index 2968297f97..89f98d5de4 100644 --- a/src/goStatus.ts +++ b/src/goStatus.ts @@ -24,7 +24,12 @@ import { getGoVersion } from './util'; export const outputChannel = vscode.window.createOutputChannel('Go'); -export const diagnosticsStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); +const STATUS_BAR_ITEM_NAME = 'Go Diagnostics'; +export const diagnosticsStatusBarItem = vscode.window.createStatusBarItem( + STATUS_BAR_ITEM_NAME, + vscode.StatusBarAlignment.Left +); +diagnosticsStatusBarItem.name = STATUS_BAR_ITEM_NAME; // statusbar item for switching the Go environment export let goEnvStatusbarItem: vscode.StatusBarItem; @@ -106,7 +111,13 @@ export async function expandGoStatusBar() { */ export async function initGoStatusBar() { if (!goEnvStatusbarItem) { - goEnvStatusbarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 50); + const STATUS_BAR_ITEM_NAME = 'Go'; + goEnvStatusbarItem = vscode.window.createStatusBarItem( + STATUS_BAR_ITEM_NAME, + vscode.StatusBarAlignment.Left, + 50 + ); + goEnvStatusbarItem.name = STATUS_BAR_ITEM_NAME; } // set Go version and command const version = await getGoVersion(); @@ -158,7 +169,13 @@ export function disposeGoStatusBar() { if (terminalCreationListener) { terminalCreationListener.dispose(); } - removeGoStatus(); + for (const statusBarEntry of statusBarEntries) { + if (statusBarEntry) { + const [name, entry] = statusBarEntry; + statusBarEntries.delete(name); + entry.dispose(); + } + } } /** @@ -171,18 +188,23 @@ export function showGoStatusBar() { } // status bar item to show warning messages such as missing analysis tools. -let statusBarEntry: vscode.StatusBarItem; +const statusBarEntries = new Map(); -export function removeGoStatus() { +export function removeGoStatus(name: string) { + const statusBarEntry = statusBarEntries.get(name); if (statusBarEntry) { statusBarEntry.dispose(); - statusBarEntry = undefined; + statusBarEntries.delete(name); } } -export function addGoStatus(message: string, command: string, tooltip?: string) { +export function addGoStatus(name: string, message: string, command: string, tooltip?: string) { + let statusBarEntry = statusBarEntries.get(name); if (!statusBarEntry) { - statusBarEntry = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, Number.MIN_VALUE); + statusBarEntry = vscode.window.createStatusBarItem(name, vscode.StatusBarAlignment.Right, Number.MIN_VALUE); + statusBarEntries.set(name, statusBarEntry); + + statusBarEntry.name = name; } statusBarEntry.text = `$(alert) ${message}`; statusBarEntry.command = command; diff --git a/src/testUtils.ts b/src/testUtils.ts index 3dc4e45afa..d34016874c 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -23,7 +23,9 @@ import { envPath, expandFilePathInOutput, getCurrentGoRoot, getCurrentGoWorkspac import { killProcessTree } from './utils/processUtils'; const testOutputChannel = vscode.window.createOutputChannel('Go Tests'); -const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); +const STATUS_BAR_ITEM_NAME = 'Go Test Cancel'; +const statusBarItem = vscode.window.createStatusBarItem(STATUS_BAR_ITEM_NAME, vscode.StatusBarAlignment.Left); +statusBarItem.name = STATUS_BAR_ITEM_NAME; statusBarItem.command = 'go.test.cancel'; statusBarItem.text = '$(x) Cancel Running Tests';