@@ -12,11 +12,12 @@ import vscodeUri = require('vscode-uri');
12
12
import { getGoConfig } from './config' ;
13
13
import { formatGoVersion , GoEnvironmentOption , terminalCreationListener } from './goEnvironmentStatus' ;
14
14
import { GoDocumentSelector , isGoFile } from './goMode' ;
15
- import { isModSupported , runGoEnv } from './goModules' ;
15
+ import { runGoEnv } from './goModules' ;
16
16
import { allToolsInformation } from './goToolsInformation' ;
17
17
import { getGoVersion } from './util' ;
18
18
import { GoExtensionContext } from './context' ;
19
19
import { CommandFactory } from './commands' ;
20
+ import { LanguageClient , State } from 'vscode-languageclient/node' ;
20
21
21
22
export const outputChannel = vscode . window . createOutputChannel ( 'Go' , { log : true } ) ;
22
23
@@ -32,28 +33,39 @@ export let goEnvStatusbarItem: vscode.StatusBarItem;
32
33
33
34
let gomod : string ;
34
35
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)' ;
37
39
38
40
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 ( ) => {
39
54
// Only update the module path if we are in a Go file.
40
55
// This allows the user to open output windows without losing
41
56
// the go.mod information in the status bar.
57
+ const editor = vscode . window . activeTextEditor ;
42
58
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 } ` ) ;
52
66
}
53
67
}
54
- }
55
68
56
- export const expandGoStatusBar : CommandFactory = ( ctx , goCtx ) => async ( ) => {
57
69
const { languageServerIsRunning, serverOutputChannel } = goCtx ;
58
70
const options = [
59
71
{ label : 'Locate Configured Go Tools' , description : 'display go env' } ,
@@ -66,7 +78,7 @@ export const expandGoStatusBar: CommandFactory = (ctx, goCtx) => async () => {
66
78
const goplsIsRunning = languageServerIsRunning && cfg && cfg . serverName === 'gopls' ;
67
79
if ( goplsIsRunning ) {
68
80
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 } ` } ) ;
70
82
}
71
83
// In case gopls still need to be installed, cfg.serverName will be empty.
72
84
if ( ! goplsIsRunning && goConfig . get ( 'useLanguageServer' ) === true && cfg ?. serverName === '' ) {
@@ -120,13 +132,13 @@ export const expandGoStatusBar: CommandFactory = (ctx, goCtx) => async () => {
120
132
* Initialize the status bar item with current Go binary
121
133
*/
122
134
export async function initGoStatusBar ( goCtx : GoExtensionContext ) {
123
- const { languageServerIsRunning } = goCtx ;
135
+ const { languageClient } = goCtx ;
124
136
if ( ! goEnvStatusbarItem ) {
125
137
const STATUS_BAR_ITEM_NAME = 'Go' ;
126
138
goEnvStatusbarItem = vscode . window . createStatusBarItem (
127
139
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.
130
142
) ;
131
143
goEnvStatusbarItem . name = STATUS_BAR_ITEM_NAME ;
132
144
}
@@ -141,33 +153,45 @@ export async function initGoStatusBar(goCtx: GoExtensionContext) {
141
153
// Assume if it is configured it is already running, since the
142
154
// icon will be updated on an attempt to start.
143
155
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
+ }
147
160
}
148
161
149
- export function updateLanguageServerIconGoStatusBar ( started : boolean , enabled : boolean ) {
162
+ export function updateLanguageServerIconGoStatusBar ( languageClient : LanguageClient | undefined , enabled : boolean ) {
150
163
if ( ! goEnvStatusbarItem ) {
151
164
return ;
152
165
}
153
166
154
167
// Split the existing goEnvStatusbarItem.text into the version string part and
155
168
// the gopls icon part.
156
169
let text = goEnvStatusbarItem . text ;
157
- let icon = '' ;
158
170
if ( text . endsWith ( languageServerIcon ) ) {
159
171
text = text . substring ( 0 , text . length - languageServerIcon . length ) ;
160
172
} else if ( text . endsWith ( languageServerErrorIcon ) ) {
161
173
text = text . substring ( 0 , text . length - languageServerErrorIcon . length ) ;
174
+ } else if ( text . endsWith ( languageServerStartingIcon ) ) {
175
+ text = text . substring ( 0 , text . length - languageServerStartingIcon . length ) ;
162
176
}
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 ) {
165
186
icon = languageServerIcon ;
166
- } else if ( ! started && enabled ) {
187
+ color = undefined ;
188
+ } else if ( languageClient . state === State . Stopped ) {
167
189
icon = languageServerErrorIcon ;
190
+ color = new vscode . ThemeColor ( 'statusBarItem.errorBackground' ) ;
168
191
}
169
192
170
193
goEnvStatusbarItem . text = text + icon ;
194
+ goEnvStatusbarItem . backgroundColor = color ;
171
195
}
172
196
173
197
/**
0 commit comments