-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src/goEnvironmentStatus.ts: create status bar item for Go environment
This CL is the first in a series which should follow this general outline: 1. Create status bar item for switching Go binary (not implemented) 2. Create command palette menu for choosing the Go binary 3. Track the currently selected Go binary using workspace context 4. Show versions of Go that are not installed and allow them to be selected and installed 5. Ensure new integrated terminals use the selected environment 6. Detect if Go is not installed and prompt to install it 7. Detect if user has the latest version of Go installed and prompt them to install it 8. Cache Go paths upon extension initialization for faster menu loading This CL only creates a status bar item that displays the current Go path when pressed. Change-Id: I6d15811250fc9eaa257db071358261c906bbfe0a Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/238624 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
- Loading branch information
Showing
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright 2020 The Go Authors. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import vscode = require('vscode'); | ||
|
||
import { updateGoVarsFromConfig } from './goInstallTools'; | ||
import { getCurrentGoRoot } from './goPath'; | ||
import { getGoVersion } from './util'; | ||
|
||
// statusbar item for switching the Go environment | ||
let goEnvStatusbarItem: vscode.StatusBarItem; | ||
|
||
/** | ||
* Initialize the status bar item with current Go binary | ||
*/ | ||
export async function initGoStatusBar() { | ||
goEnvStatusbarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 50); | ||
|
||
// make goroot default to go.goroot and fallback on $PATH | ||
const goroot = await getActiveGoRoot(); | ||
if (!goroot) { | ||
// TODO: prompt user to install Go | ||
vscode.window.showErrorMessage('No Go command could be found.'); | ||
} | ||
|
||
// set Go version and command | ||
const version = await getGoVersion(); | ||
goEnvStatusbarItem.text = formatGoVersion(version.format()); | ||
goEnvStatusbarItem.command = 'go.environment.choose'; | ||
|
||
showGoStatusBar(); | ||
} | ||
|
||
/** | ||
* disable the Go environment status bar item | ||
*/ | ||
export function disposeGoStatusBar() { | ||
if (!!goEnvStatusbarItem) { | ||
goEnvStatusbarItem.dispose(); | ||
} | ||
} | ||
|
||
/** | ||
* Show the Go Environment statusbar item on the statusbar | ||
*/ | ||
export function showGoStatusBar() { | ||
if (!!goEnvStatusbarItem) { | ||
goEnvStatusbarItem.show(); | ||
} | ||
} | ||
|
||
/** | ||
* Hide the Go Environment statusbar item from the statusbar | ||
*/ | ||
export function hideGoStatusBar() { | ||
if (!!goEnvStatusbarItem) { | ||
goEnvStatusbarItem.hide(); | ||
} | ||
} | ||
|
||
/** | ||
* Present a command palette menu to the user to select their go binary | ||
* TODO: remove temporary alert and implement correct functionality | ||
*/ | ||
export function chooseGoEnvironment() { | ||
vscode.window.showInformationMessage(`Current GOROOT: ${getCurrentGoRoot()}`); | ||
} | ||
|
||
/** | ||
* return reference to the statusbar item | ||
*/ | ||
export function getGoEnvironmentStatusbarItem(): vscode.StatusBarItem { | ||
return goEnvStatusbarItem; | ||
} | ||
|
||
export async function getActiveGoRoot(): Promise<string | undefined> { | ||
// look for current current go binary | ||
let goroot = getCurrentGoRoot(); | ||
if (!goroot) { | ||
await updateGoVarsFromConfig(); | ||
goroot = getCurrentGoRoot(); | ||
} | ||
return goroot || undefined; | ||
} | ||
|
||
export function formatGoVersion(version: string): string { | ||
const versionWords = version.split(' '); | ||
if (versionWords[0] === 'devel') { | ||
// Go devel +hash | ||
return `Go ${versionWords[0]} ${versionWords[4]}`; | ||
} else if (versionWords.length > 0) { | ||
// some other version format | ||
return `Go ${version.substr(0, 8)}`; | ||
} else { | ||
// default semantic version format | ||
return `Go ${versionWords[0]}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Modification copyright 2020 The Go Authors. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
import * as assert from 'assert'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { disposeGoStatusBar, formatGoVersion, getGoEnvironmentStatusbarItem, initGoStatusBar } from '../../src/goEnvironmentStatus'; | ||
import { getGoVersion } from '../../src/util'; | ||
|
||
describe('#initGoStatusBar()', function () { | ||
this.beforeAll(() => { | ||
initGoStatusBar(); | ||
}); | ||
|
||
this.afterAll(() => { | ||
disposeGoStatusBar(); | ||
}); | ||
|
||
it('should create a status bar item', () => { | ||
assert.notEqual(getGoEnvironmentStatusbarItem(), undefined); | ||
}); | ||
|
||
it('should create a status bar item with a label matching go.goroot version', async () => { | ||
const version = await getGoVersion(); | ||
const versionLabel = formatGoVersion(version.format()); | ||
assert.equal( | ||
getGoEnvironmentStatusbarItem().text, | ||
versionLabel, | ||
'goroot version does not match status bar item text' | ||
); | ||
}); | ||
}); |