@@ -22,13 +22,17 @@ export class GoExplorerProvider implements vscode.TreeDataProvider<vscode.TreeIt
22
22
private activeFolder ?: vscode . WorkspaceFolder ;
23
23
private activeDocument ?: vscode . TextDocument ;
24
24
25
- static setup ( ctx : vscode . ExtensionContext ) {
25
+ static setup ( { subscriptions } : vscode . ExtensionContext ) {
26
26
const provider = new this ( ) ;
27
- ctx . subscriptions . push ( vscode . window . registerTreeDataProvider ( 'go.explorer' , provider ) ) ;
28
- ctx . subscriptions . push ( vscode . commands . registerCommand ( 'go.explorer.refresh' , ( ) => provider . update ( true ) ) ) ;
29
- ctx . subscriptions . push (
30
- vscode . commands . registerCommand ( 'go.explorer.open' , ( item : EnvTreeItem ) => provider . open ( item ) )
31
- ) ;
27
+ const {
28
+ window : { registerTreeDataProvider } ,
29
+ commands : { registerCommand }
30
+ } = vscode ;
31
+ subscriptions . push ( registerTreeDataProvider ( 'go.explorer' , provider ) ) ;
32
+ subscriptions . push ( registerCommand ( 'go.explorer.refresh' , ( ) => provider . update ( true ) ) ) ;
33
+ subscriptions . push ( registerCommand ( 'go.explorer.open' , ( item ) => provider . open ( item ) ) ) ;
34
+ subscriptions . push ( registerCommand ( 'go.workspace.editEnv' , ( item ) => provider . editEnv ( item ) ) ) ;
35
+ subscriptions . push ( registerCommand ( 'go.workspace.resetEnv' , ( item ) => provider . resetEnv ( item ) ) ) ;
32
36
return provider ;
33
37
}
34
38
@@ -89,6 +93,39 @@ export class GoExplorerProvider implements vscode.TreeDataProvider<vscode.TreeIt
89
93
await vscode . window . showTextDocument ( doc ) ;
90
94
}
91
95
96
+ private async editEnv ( item ?: EnvTreeItem ) {
97
+ const uri = this . activeFolder ?. uri ;
98
+ if ( ! uri ) {
99
+ return ;
100
+ }
101
+ let pick : { label ?: string ; description ?: string } ;
102
+ if ( isEnvTreeItem ( item ) ) {
103
+ pick = { label : item . key , description : item . value } ;
104
+ } else {
105
+ const items = Object . entries < string > ( await runGoEnv ( uri ) )
106
+ . filter ( ( [ label ] ) => ! GoEnv . readonlyVars . has ( label ) )
107
+ . map ( ( [ label , description ] ) => ( {
108
+ label,
109
+ description
110
+ } ) ) ;
111
+ pick = await vscode . window . showQuickPick ( items , { title : 'Go: Edit Workspace Env' } ) ;
112
+ }
113
+ if ( ! pick ) return ;
114
+ const { label, description } = pick ;
115
+ const value = await vscode . window . showInputBox ( { title : label , value : description } ) ;
116
+ if ( typeof value !== 'undefined' ) {
117
+ await GoEnv . edit ( { [ label ] : value } ) ;
118
+ }
119
+ }
120
+
121
+ private async resetEnv ( item ?: EnvTreeItem ) {
122
+ if ( item ?. key ) {
123
+ await GoEnv . reset ( [ item . key ] ) ;
124
+ return ;
125
+ }
126
+ await GoEnv . reset ( ) ;
127
+ }
128
+
92
129
private envTree ( ) {
93
130
if ( this . activeFolder ) {
94
131
const { name, uri } = this . activeFolder ;
@@ -130,14 +167,14 @@ export class GoExplorerProvider implements vscode.TreeDataProvider<vscode.TreeIt
130
167
131
168
class EnvTree implements vscode . TreeItem {
132
169
label = 'env' ;
133
- contextValue = 'go:explorer:env ' ;
170
+ contextValue = 'go:explorer:envtree ' ;
134
171
collapsibleState = vscode . TreeItemCollapsibleState . Expanded ;
135
172
iconPath = new vscode . ThemeIcon ( 'symbol-folder' ) ;
136
173
constructor ( public description = '' , public workspace ?: vscode . Uri ) { }
137
174
}
138
175
139
176
function isEnvTree ( item ?: vscode . TreeItem ) : item is EnvTree {
140
- return item ?. contextValue === 'go:explorer:env ' ;
177
+ return item ?. contextValue === 'go:explorer:envtree ' ;
141
178
}
142
179
143
180
class EnvTreeItem implements vscode . TreeItem {
@@ -148,14 +185,18 @@ class EnvTreeItem implements vscode.TreeItem {
148
185
constructor ( public key : string , public value : string ) {
149
186
this . label = `${ key } =${ replaceHome ( value ) } ` ;
150
187
this . contextValue = 'go:explorer:envitem' ;
151
- if ( GoEnv . fileVars . includes ( key ) ) {
152
- this . contextValue = 'go:explorer:envitem:file ' ;
188
+ if ( GoEnv . fileVars . has ( key ) ) {
189
+ this . contextValue = 'go:explorer:envfile ' ;
153
190
this . file = vscode . Uri . file ( value ) ;
154
191
}
155
192
this . tooltip = `${ key } =${ value } ` ;
156
193
}
157
194
}
158
195
196
+ function isEnvTreeItem ( item ?: vscode . TreeItem ) : item is EnvTreeItem {
197
+ return item ?. contextValue === 'go:explorer:envitem' ;
198
+ }
199
+
159
200
class GoEnv {
160
201
/**
161
202
* get returns a subset of go env vars, the union of this.vars and values
@@ -173,7 +214,7 @@ class GoEnv {
173
214
* update writes to toolsEnvVars in the go workspace config.
174
215
* @param vars a record of env vars to update.
175
216
*/
176
- static async update ( vars : Record < string , string > ) {
217
+ static async edit ( vars : Record < string , string > ) {
177
218
const config = getGoConfig ( ) ;
178
219
await config . update ( 'toolsEnvVars' , { ...config [ 'toolsEnvVars' ] , ...vars } ) ;
179
220
}
@@ -182,19 +223,34 @@ class GoEnv {
182
223
* reset removes entries from toolsEnvVars in the go workspace config.
183
224
* @param vars env vars to reset.
184
225
*/
185
- static async reset ( vars : string [ ] ) {
226
+ static async reset ( vars ? : string [ ] ) {
186
227
const config = getGoConfig ( ) ;
187
- const env = { ...config [ 'toolsEnvVars' ] } ;
188
- for ( const v of vars ) {
189
- delete env [ v ] ;
228
+ let env : Record < string , string > = { } ;
229
+ if ( vars ) {
230
+ env = { ...config [ 'toolsEnvVars' ] } ;
231
+ for ( const v of vars ) {
232
+ delete env [ v ] ;
233
+ }
190
234
}
191
235
await config . update ( 'toolsEnvVars' , env ) ;
192
236
}
193
237
194
- /** A list of env vars that point to files. */
195
- static fileVars = [ 'GOMOD' , 'GOWORK' , 'GOENV' ] ;
196
-
197
- /** The list of env vars that should always be visible if they contain a value. */
238
+ /** Vars that point to files. */
239
+ static fileVars = new Set ( [ 'GOMOD' , 'GOWORK' , 'GOENV' ] ) ;
240
+
241
+ /** Vars available from 'go env' but not read from the environment */
242
+ static readonlyVars = new Set ( [
243
+ 'GOEXE' ,
244
+ 'GOGCCFLAGS' ,
245
+ 'GOHOSTARCH' ,
246
+ 'GOHOSTOS' ,
247
+ 'GOMOD' ,
248
+ 'GOTOOLDIR' ,
249
+ 'GOVERSION' ,
250
+ 'GOWORK'
251
+ ] ) ;
252
+
253
+ /** Vars that should always be visible if they contain a value. */
198
254
private static vars = [ 'GOPRIVATE' , 'GOMOD' , 'GOWORK' , 'GOENV' ] ;
199
255
}
200
256
0 commit comments