@@ -12,11 +12,12 @@ import vscodeUri = require('vscode-uri');
1212import { getGoConfig } from './config' ;
1313import { formatGoVersion , GoEnvironmentOption , terminalCreationListener } from './goEnvironmentStatus' ;
1414import { GoDocumentSelector , isGoFile } from './goMode' ;
15- import { isModSupported , runGoEnv } from './goModules' ;
15+ import { runGoEnv } from './goModules' ;
1616import { allToolsInformation } from './goToolsInformation' ;
1717import { getGoVersion } from './util' ;
1818import { GoExtensionContext } from './context' ;
1919import { CommandFactory } from './commands' ;
20+ import { LanguageClient , State } from 'vscode-languageclient/node' ;
2021
2122export const outputChannel = vscode . window . createOutputChannel ( 'Go' , { log : true } ) ;
2223
@@ -32,28 +33,39 @@ export let goEnvStatusbarItem: vscode.StatusBarItem;
3233
3334let gomod : string ;
3435let 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
3840export 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 */
122134export 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/**
0 commit comments