Skip to content

Commit

Permalink
src/goStatus: present error icon if gopls couldn't be found
Browse files Browse the repository at this point in the history
We wanted the missing gopls error notification to be more
visible, but for some reason, sometimes it's auto-collapsed
and only visible only if user clicks the bell in the right
bottom corner. Even though there should be the "Analysis
tools missing" error, it may be difficult for users to miss
that. Add another icon next to the go version string in the
Go status bar item.

When the user clicks the Go status bar item, the quickpick
menu includes an option to install gopls.

We also promoted the missing tool notification to 'error'
level - missing tool is an error.

Change-Id: Ice121a186f7b23ec94663579087aa737e93df792
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/286672
Trust: Hyang-Ah Hana Kim <[email protected]>
Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Rebecca Stambler <[email protected]>
TryBot-Result: kokoro <[email protected]>
  • Loading branch information
hyangah committed Jan 26, 2021
1 parent 465fe7e commit 03274ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export async function promptForMissingTool(toolName: string) {
}
const msg = `The "${tool.name}" command is not available.
Run "go get -v ${getImportPath(tool, goVersion)}" to install.`;
const selected = await vscode.window.showInformationMessage(msg, ...installOptions);
const selected = await vscode.window.showErrorMessage(msg, ...installOptions);
switch (selected) {
case 'Install':
await installTools([tool], goVersion);
Expand Down Expand Up @@ -527,8 +527,8 @@ let suggestedDownloadGo = false;

async function suggestDownloadGo() {
const msg = `Failed to find the "go" binary in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath}).` +
`Check PATH, or Install Go and reload the window. ` +
`If PATH isn't what you expected, see https://github.com/golang/vscode-go/issues/971`;
`Check PATH, or Install Go and reload the window. ` +
`If PATH isn't what you expected, see https://github.com/golang/vscode-go/issues/971`;
if (suggestedDownloadGo) {
vscode.window.showErrorMessage(msg);
return;
Expand Down
5 changes: 3 additions & 2 deletions src/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ export async function startLanguageServerWithFallback(ctx: vscode.ExtensionConte
// We already created various notification - e.g. missing gopls, ...
// So, just leave a log message here instead of issuing one more notification.
outputChannel.appendLine(
`Failed to start the language server (${cfg.serverName}). Falling back to default language providers...`);
`Failed to start the language server (gopls). Falling back to default language providers...`);
outputChannel.show();
}
// If the server has been disabled, or failed to start,
// fall back to the default providers, while making sure not to
Expand All @@ -179,7 +180,7 @@ export async function startLanguageServerWithFallback(ctx: vscode.ExtensionConte

if (disposable) { disposable.dispose(); }
languageServerIsRunning = started;
updateLanguageServerIconGoStatusBar(started, cfg.serverName);
updateLanguageServerIconGoStatusBar(started, goConfig['useLanguageServer']);
languageServerStartInProgress = false;
});
}
Expand Down
47 changes: 34 additions & 13 deletions src/goStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { formatGoVersion, GoEnvironmentOption, terminalCreationListener } from '
import { buildLanguageServerConfig, getLocalGoplsVersion, languageServerIsRunning, serverOutputChannel } from './goLanguageServer';
import { isGoFile } from './goMode';
import { getModFolderPath, isModSupported } from './goModules';
import { allToolsInformation } from './goTools';
import { getGoVersion } from './util';

export let outputChannel = vscode.window.createOutputChannel('Go');
Expand All @@ -24,6 +25,7 @@ export let goEnvStatusbarItem: vscode.StatusBarItem;

let modulePath: string;
export const languageServerIcon = '$(zap)';
export const languageServerErrorIcon = '$(warning)';

export function updateGoStatusBar(editor: vscode.TextEditor) {
// Only update the module path if we are in a Go file.
Expand All @@ -47,11 +49,17 @@ export async function expandGoStatusBar() {
];

// Get the gopls configuration
const cfg = buildLanguageServerConfig(getGoConfig());
const goConfig = getGoConfig();
const cfg = buildLanguageServerConfig(goConfig);
if (languageServerIsRunning && cfg.serverName === 'gopls') {
const goplsVersion = await getLocalGoplsVersion(cfg);
options.push({label: `${languageServerIcon}Open 'gopls' trace`, description: `${goplsVersion}`});
}
if (!languageServerIsRunning && !cfg.serverName && goConfig['useLanguageServer']) {
options.push({
label: `Install Go Language Server`,
description: `${languageServerErrorIcon}'gopls' is required but missing`});
}

// If modules is enabled, add link to mod file
if (!!modulePath) {
Expand All @@ -72,6 +80,9 @@ export async function expandGoStatusBar() {
serverOutputChannel.show();
}
break;
case `Install Go Language Server`:
vscode.commands.executeCommand('go.tools.install', [allToolsInformation['gopls']]);
break;
case `Open 'go.mod'`:
const openPath = vscode.Uri.file(item.description);
vscode.workspace.openTextDocument(openPath).then((doc) => {
Expand Down Expand Up @@ -101,27 +112,37 @@ export async function initGoStatusBar() {
// Add an icon to indicate that the 'gopls' server is running.
// Assume if it is configured it is already running, since the
// icon will be updated on an attempt to start.
const cfg = buildLanguageServerConfig(getGoConfig());
updateLanguageServerIconGoStatusBar(languageServerIsRunning, cfg.serverName);
const goConfig = getGoConfig();
const cfg = buildLanguageServerConfig(goConfig);
updateLanguageServerIconGoStatusBar(languageServerIsRunning, goConfig['useLanguageServer']);

showGoStatusBar();
}

export async function updateLanguageServerIconGoStatusBar(started: boolean, server: string) {
export function updateLanguageServerIconGoStatusBar(started: boolean, enabled: boolean) {
if (!goEnvStatusbarItem) {
return;
}

const text = goEnvStatusbarItem.text;
if (started && server === 'gopls') {
if (!text.endsWith(languageServerIcon)) {
goEnvStatusbarItem.text = text + languageServerIcon;
}
} else {
if (text.endsWith(languageServerIcon)) {
goEnvStatusbarItem.text = text.substring(0, text.length - languageServerIcon.length);
}
// Split the existing goEnvStatusbarItem.text into the version string part and
// the gopls icon part.
let text = goEnvStatusbarItem.text;
let icon = '';
if (text.endsWith(languageServerIcon)) {
icon = languageServerIcon;
text = text.substring(0, text.length - languageServerIcon.length);
} else if (text.endsWith(languageServerErrorIcon)) {
icon = languageServerErrorIcon;
text = text.substring(0, text.length - languageServerErrorIcon.length);
}

if (started && enabled) {
icon = languageServerIcon;
} else if (!started && enabled) {
icon = languageServerErrorIcon;
}

goEnvStatusbarItem.text = text + icon;
}

/**
Expand Down

0 comments on commit 03274ad

Please sign in to comment.