Skip to content

Commit 38b056d

Browse files
mcjcloudhyangah
authored andcommitted
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]>
1 parent b029c5d commit 38b056d

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@
345345
"command": "go.languageserver.restart",
346346
"title": "Go: Restart Language Server",
347347
"description": "Restart the running instance of the language server"
348+
},
349+
{
350+
"command": "go.environment.choose",
351+
"title": "Go: Choose Go Environment",
352+
"description": "Choose a different Go version or binary for this project. (WIP)"
348353
}
349354
],
350355
"breakpoints": [

src/goEnvironmentStatus.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

src/goMain.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { GoDebugConfigurationProvider } from './goDebugConfiguration';
2020
import { extractFunction, extractVariable } from './goDoctor';
2121
import { toolExecutionEnvironment } from './goEnv';
22+
import { chooseGoEnvironment, initGoStatusBar } from './goEnvironmentStatus';
2223
import { runFillStruct } from './goFillStruct';
2324
import * as goGenerateTests from './goGenerateTests';
2425
import { goGetPackage } from './goGetPackage';
@@ -169,6 +170,9 @@ export function activate(ctx: vscode.ExtensionContext): void {
169170
);
170171
showHideStatus(vscode.window.activeTextEditor);
171172

173+
// show the go environment status bar item
174+
initGoStatusBar();
175+
172176
const testCodeLensProvider = new GoRunTestCodeLensProvider();
173177
const referencesCodeLensProvider = new GoReferencesCodeLensProvider();
174178

@@ -559,6 +563,13 @@ export function activate(ctx: vscode.ExtensionContext): void {
559563
})
560564
);
561565

566+
// Go Enviornment switching commands
567+
ctx.subscriptions.push(
568+
vscode.commands.registerCommand('go.environment.choose', () => {
569+
chooseGoEnvironment();
570+
})
571+
);
572+
562573
vscode.languages.setLanguageConfiguration(GO_MODE.language, {
563574
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g
564575
});

test/integration/statusbar.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
* Modification copyright 2020 The Go Authors. All rights reserved.
4+
* Licensed under the MIT License. See LICENSE in the project root for license information.
5+
*--------------------------------------------------------*/
6+
7+
import * as assert from 'assert';
8+
import { describe, it } from 'mocha';
9+
10+
import { disposeGoStatusBar, formatGoVersion, getGoEnvironmentStatusbarItem, initGoStatusBar } from '../../src/goEnvironmentStatus';
11+
import { getGoVersion } from '../../src/util';
12+
13+
describe('#initGoStatusBar()', function () {
14+
this.beforeAll(() => {
15+
initGoStatusBar();
16+
});
17+
18+
this.afterAll(() => {
19+
disposeGoStatusBar();
20+
});
21+
22+
it('should create a status bar item', () => {
23+
assert.notEqual(getGoEnvironmentStatusbarItem(), undefined);
24+
});
25+
26+
it('should create a status bar item with a label matching go.goroot version', async () => {
27+
const version = await getGoVersion();
28+
const versionLabel = formatGoVersion(version.format());
29+
assert.equal(
30+
getGoEnvironmentStatusbarItem().text,
31+
versionLabel,
32+
'goroot version does not match status bar item text'
33+
);
34+
});
35+
});

0 commit comments

Comments
 (0)