Skip to content

Commit 4711dfa

Browse files
hyangahgopherbot
authored andcommitted
extension: move the Go statusbar item to right
And add background color in case of error. Change-Id: I8c9df8d95e6d7681383bc2d6ca57101373e2881d Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/560477 Commit-Queue: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Suzy Mueller <[email protected]> Auto-Submit: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent a671ba3 commit 4711dfa

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

extension/src/commands/startLanguageServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ export const startLanguageServer: CommandFactory = (ctx, goCtx) => {
8888
goCtx.serverInfo?.Commands
8989
);
9090

91-
updateStatus(goCtx, goConfig, true);
9291
console.log(`Server: ${JSON.stringify(goCtx.serverInfo, null, 2)}`);
9392
} catch (e) {
9493
const msg = `Error starting language server: ${e}`;
9594
console.log(msg);
9695
goCtx.serverOutputChannel?.append(msg);
9796
} finally {
97+
updateStatus(goCtx, goConfig, true);
9898
unlock();
9999
}
100100
};
@@ -103,7 +103,7 @@ export const startLanguageServer: CommandFactory = (ctx, goCtx) => {
103103
function updateStatus(goCtx: GoExtensionContext, goConfig: vscode.WorkspaceConfiguration, didStart: boolean) {
104104
goCtx.languageServerIsRunning = didStart;
105105
vscode.commands.executeCommand('setContext', 'go.goplsIsRunning', didStart);
106-
updateLanguageServerIconGoStatusBar(didStart, goConfig['useLanguageServer'] === true);
106+
updateLanguageServerIconGoStatusBar(goCtx.languageClient, goConfig['useLanguageServer'] === true);
107107
}
108108

109109
function shouldActivateLanguageFeatures() {

extension/src/goEnvironmentStatus.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,15 @@ export function getGoEnvironmentStatusbarItem(): vscode.StatusBarItem {
415415

416416
export function formatGoVersion(version?: GoVersion): string {
417417
if (!version || !version.isValid()) {
418-
return 'Go (unknown)';
418+
return '(unknown)';
419419
}
420420
const versionStr = version.format(true);
421421
const versionWords = versionStr.split(' ');
422422
if (versionWords.length > 1 && versionWords[0] === 'devel') {
423423
// go devel +hash or go devel go1.17-hash
424-
return versionWords[1].startsWith('go') ? `Go ${versionWords[1].slice(2)}` : `Go ${versionWords[1]}`;
424+
return versionWords[1].startsWith('go') ? `${versionWords[1].slice(2)}` : `${versionWords[1]}`;
425425
} else {
426-
return `Go ${versionWords[0]}`;
426+
return `${versionWords[0]}`;
427427
}
428428
}
429429

extension/src/goStatus.ts

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import vscodeUri = require('vscode-uri');
1212
import { getGoConfig } from './config';
1313
import { formatGoVersion, GoEnvironmentOption, terminalCreationListener } from './goEnvironmentStatus';
1414
import { GoDocumentSelector, isGoFile } from './goMode';
15-
import { isModSupported, runGoEnv } from './goModules';
15+
import { runGoEnv } from './goModules';
1616
import { allToolsInformation } from './goToolsInformation';
1717
import { getGoVersion } from './util';
1818
import { GoExtensionContext } from './context';
1919
import { CommandFactory } from './commands';
20+
import { LanguageClient, State } from 'vscode-languageclient/node';
2021

2122
export const outputChannel = vscode.window.createOutputChannel('Go', { log: true });
2223

@@ -32,28 +33,39 @@ export let goEnvStatusbarItem: vscode.StatusBarItem;
3233

3334
let gomod: string;
3435
let gowork: string;
35-
export const languageServerIcon = '$(zap)';
36-
export const languageServerErrorIcon = '$(warning)';
36+
const languageServerIcon = '$(zap)';
37+
const languageServerErrorIcon = '$(warning)';
38+
const languageServerStartingIcon = '$(sync~spin)';
3739

3840
export async function updateGoStatusBar(editor: vscode.TextEditor | undefined) {
41+
if (!editor) {
42+
return;
43+
}
44+
if (isGoFile(editor.document)) {
45+
showGoStatusBar();
46+
return;
47+
}
48+
if (editor.document.languageId?.toLowerCase() !== 'log') {
49+
goEnvStatusbarItem.hide();
50+
}
51+
}
52+
53+
export const expandGoStatusBar: CommandFactory = (ctx, goCtx) => async () => {
3954
// Only update the module path if we are in a Go file.
4055
// This allows the user to open output windows without losing
4156
// the go.mod information in the status bar.
57+
const editor = vscode.window.activeTextEditor;
4258
if (!!editor && isGoFile(editor.document)) {
43-
const isMod = await isModSupported(editor.document.uri);
44-
if (isMod) {
45-
runGoEnv(vscodeUri.Utils.dirname(editor.document.uri), ['GOMOD', 'GOWORK']).then((p) => {
46-
gomod = p['GOMOD'] === '/dev/null' || p['GOMOD'] === 'NUL' ? '' : p['GOMOD'];
47-
gowork = p['GOWORK'];
48-
});
49-
} else {
50-
gomod = '';
51-
gowork = '';
59+
const cwd = vscodeUri.Utils.dirname(editor.document.uri);
60+
try {
61+
const p = await runGoEnv(cwd, ['GOMOD', 'GOWORK']);
62+
gomod = p['GOMOD'] === '/dev/null' || p['GOMOD'] === 'NUL' ? '' : p['GOMOD'];
63+
gowork = p['GOWORK'];
64+
} catch (e) {
65+
outputChannel.debug(`failed to run go env from ${cwd} - ${e}`);
5266
}
5367
}
54-
}
5568

56-
export const expandGoStatusBar: CommandFactory = (ctx, goCtx) => async () => {
5769
const { languageServerIsRunning, serverOutputChannel } = goCtx;
5870
const options = [
5971
{ label: 'Locate Configured Go Tools', description: 'display go env' },
@@ -66,7 +78,7 @@ export const expandGoStatusBar: CommandFactory = (ctx, goCtx) => async () => {
6678
const goplsIsRunning = languageServerIsRunning && cfg && cfg.serverName === 'gopls';
6779
if (goplsIsRunning) {
6880
const goplsVersion = cfg.version;
69-
options.push({ label: `${languageServerIcon}Open 'gopls' trace`, description: `${goplsVersion?.version}` });
81+
options.push({ label: `${languageServerIcon} Open 'gopls' trace`, description: `${goplsVersion?.version}` });
7082
}
7183
// In case gopls still need to be installed, cfg.serverName will be empty.
7284
if (!goplsIsRunning && goConfig.get('useLanguageServer') === true && cfg?.serverName === '') {
@@ -120,13 +132,13 @@ export const expandGoStatusBar: CommandFactory = (ctx, goCtx) => async () => {
120132
* Initialize the status bar item with current Go binary
121133
*/
122134
export async function initGoStatusBar(goCtx: GoExtensionContext) {
123-
const { languageServerIsRunning } = goCtx;
135+
const { languageClient } = goCtx;
124136
if (!goEnvStatusbarItem) {
125137
const STATUS_BAR_ITEM_NAME = 'Go';
126138
goEnvStatusbarItem = vscode.window.createStatusBarItem(
127139
STATUS_BAR_ITEM_NAME,
128-
vscode.StatusBarAlignment.Left,
129-
50
140+
vscode.StatusBarAlignment.Right,
141+
100.09999 // place the item right after the language status item https://github.com/microsoft/vscode-python/issues/18040#issuecomment-992567670.
130142
);
131143
goEnvStatusbarItem.name = STATUS_BAR_ITEM_NAME;
132144
}
@@ -141,33 +153,45 @@ export async function initGoStatusBar(goCtx: GoExtensionContext) {
141153
// Assume if it is configured it is already running, since the
142154
// icon will be updated on an attempt to start.
143155
const goConfig = getGoConfig();
144-
updateLanguageServerIconGoStatusBar(!!languageServerIsRunning, goConfig['useLanguageServer'] === true);
145-
146-
showGoStatusBar();
156+
updateLanguageServerIconGoStatusBar(languageClient, goConfig['useLanguageServer'] === true);
157+
if (vscode.window.visibleTextEditors.some((editor) => !!editor && isGoFile(editor.document))) {
158+
showGoStatusBar();
159+
}
147160
}
148161

149-
export function updateLanguageServerIconGoStatusBar(started: boolean, enabled: boolean) {
162+
export function updateLanguageServerIconGoStatusBar(languageClient: LanguageClient | undefined, enabled: boolean) {
150163
if (!goEnvStatusbarItem) {
151164
return;
152165
}
153166

154167
// Split the existing goEnvStatusbarItem.text into the version string part and
155168
// the gopls icon part.
156169
let text = goEnvStatusbarItem.text;
157-
let icon = '';
158170
if (text.endsWith(languageServerIcon)) {
159171
text = text.substring(0, text.length - languageServerIcon.length);
160172
} else if (text.endsWith(languageServerErrorIcon)) {
161173
text = text.substring(0, text.length - languageServerErrorIcon.length);
174+
} else if (text.endsWith(languageServerStartingIcon)) {
175+
text = text.substring(0, text.length - languageServerStartingIcon.length);
162176
}
163-
164-
if (started && enabled) {
177+
let color = undefined;
178+
let icon = '';
179+
if (!enabled || !languageClient) {
180+
icon = '';
181+
color = new vscode.ThemeColor('statusBarItem.warningBackground');
182+
} else if (languageClient.state === State.Starting) {
183+
icon = languageServerStartingIcon;
184+
color = undefined;
185+
} else if (languageClient.state === State.Running) {
165186
icon = languageServerIcon;
166-
} else if (!started && enabled) {
187+
color = undefined;
188+
} else if (languageClient.state === State.Stopped) {
167189
icon = languageServerErrorIcon;
190+
color = new vscode.ThemeColor('statusBarItem.errorBackground');
168191
}
169192

170193
goEnvStatusbarItem.text = text + icon;
194+
goEnvStatusbarItem.backgroundColor = color;
171195
}
172196

173197
/**

extension/src/language/goLanguageServer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ export async function buildLanguageClient(
466466
const { crashCount = 0 } = goCtx;
467467
goCtx.crashCount = crashCount + 1;
468468
if (goCtx.crashCount < 5) {
469+
updateLanguageServerIconGoStatusBar(c, true);
469470
return {
470471
message: '', // suppresses error popups
471472
action: CloseAction.Restart
@@ -477,7 +478,7 @@ export async function buildLanguageClient(
477478
'The connection to gopls has been closed. The gopls server may have crashed.',
478479
errorKind.crash
479480
);
480-
updateLanguageServerIconGoStatusBar(false, true);
481+
updateLanguageServerIconGoStatusBar(c, true);
481482
return {
482483
message: '', // suppresses error popups - there will be other popups.
483484
action: CloseAction.DoNotRestart

0 commit comments

Comments
 (0)