|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright 2020 The Go Authors. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import vscode = require('vscode'); |
| 9 | + |
| 10 | +import { updateGoVarsFromConfig } from './goInstallTools'; |
| 11 | +import { getCurrentGoRoot } from './goPath'; |
| 12 | +import { getGoVersion } from './util'; |
| 13 | + |
| 14 | +// statusbar item for switching the Go environment |
| 15 | +let goEnvStatusbarItem: vscode.StatusBarItem; |
| 16 | + |
| 17 | +/** |
| 18 | + * Initialize the status bar item with current Go binary |
| 19 | + */ |
| 20 | +export async function initGoStatusBar() { |
| 21 | + goEnvStatusbarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 50); |
| 22 | + |
| 23 | + // make goroot default to go.goroot and fallback on $PATH |
| 24 | + const goroot = await getActiveGoRoot(); |
| 25 | + if (!goroot) { |
| 26 | + // TODO: prompt user to install Go |
| 27 | + vscode.window.showErrorMessage('No Go command could be found.'); |
| 28 | + } |
| 29 | + |
| 30 | + // set Go version and command |
| 31 | + const version = await getGoVersion(); |
| 32 | + goEnvStatusbarItem.text = formatGoVersion(version.format()); |
| 33 | + goEnvStatusbarItem.command = 'go.environment.choose'; |
| 34 | + |
| 35 | + showGoStatusBar(); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * disable the Go environment status bar item |
| 40 | + */ |
| 41 | +export function disposeGoStatusBar() { |
| 42 | + if (!!goEnvStatusbarItem) { |
| 43 | + goEnvStatusbarItem.dispose(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Show the Go Environment statusbar item on the statusbar |
| 49 | + */ |
| 50 | +export function showGoStatusBar() { |
| 51 | + if (!!goEnvStatusbarItem) { |
| 52 | + goEnvStatusbarItem.show(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Hide the Go Environment statusbar item from the statusbar |
| 58 | + */ |
| 59 | +export function hideGoStatusBar() { |
| 60 | + if (!!goEnvStatusbarItem) { |
| 61 | + goEnvStatusbarItem.hide(); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Present a command palette menu to the user to select their go binary |
| 67 | + * TODO: remove temporary alert and implement correct functionality |
| 68 | + */ |
| 69 | +export function chooseGoEnvironment() { |
| 70 | + vscode.window.showInformationMessage(`Current GOROOT: ${getCurrentGoRoot()}`); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * return reference to the statusbar item |
| 75 | + */ |
| 76 | +export function getGoEnvironmentStatusbarItem(): vscode.StatusBarItem { |
| 77 | + return goEnvStatusbarItem; |
| 78 | +} |
| 79 | + |
| 80 | +export async function getActiveGoRoot(): Promise<string | undefined> { |
| 81 | + // look for current current go binary |
| 82 | + let goroot = getCurrentGoRoot(); |
| 83 | + if (!goroot) { |
| 84 | + await updateGoVarsFromConfig(); |
| 85 | + goroot = getCurrentGoRoot(); |
| 86 | + } |
| 87 | + return goroot || undefined; |
| 88 | +} |
| 89 | + |
| 90 | +export function formatGoVersion(version: string): string { |
| 91 | + const versionWords = version.split(' '); |
| 92 | + if (versionWords[0] === 'devel') { |
| 93 | + // Go devel +hash |
| 94 | + return `Go ${versionWords[0]} ${versionWords[4]}`; |
| 95 | + } else if (versionWords.length > 0) { |
| 96 | + // some other version format |
| 97 | + return `Go ${version.substr(0, 8)}`; |
| 98 | + } else { |
| 99 | + // default semantic version format |
| 100 | + return `Go ${versionWords[0]}`; |
| 101 | + } |
| 102 | +} |
0 commit comments