@@ -20,7 +20,7 @@ export async function initConfig(ctx: vscode.ExtensionContext) {
20
20
const isTrusted = getFromWorkspaceState ( WORKSPACE_IS_TRUSTED_KEY , false ) ;
21
21
defaultConfig = new Configuration ( isTrusted , vscode . workspace . getConfiguration ) ;
22
22
ctx . subscriptions . push (
23
- vscode . commands . registerCommand ( 'go.workspace.isTrusted.toggle' , defaultConfig . toggleWorkspaceIsTrusted )
23
+ vscode . commands . registerCommand ( 'go.workspace.isTrusted.toggle' , toggleWorkspaceIsTrusted )
24
24
) ;
25
25
26
26
if ( isTrusted ) {
@@ -40,7 +40,7 @@ export async function initConfig(ctx: vscode.ExtensionContext) {
40
40
'More Info' ) ;
41
41
switch ( val ) {
42
42
case 'Trust This Workspace' :
43
- await defaultConfig . toggleWorkspaceIsTrusted ( ) ;
43
+ await toggleWorkspaceIsTrusted ( ) ;
44
44
break ;
45
45
case 'More Info' :
46
46
vscode . env . openExternal (
@@ -58,22 +58,27 @@ function ignoredWorkspaceConfig(cfg: vscode.WorkspaceConfiguration, keys: string
58
58
} ) ;
59
59
}
60
60
61
+ async function toggleWorkspaceIsTrusted ( ) {
62
+ const v = defaultConfig . toggleWorkspaceIsTrusted ( ) ;
63
+ await updateWorkspaceState ( WORKSPACE_IS_TRUSTED_KEY , v ) ;
64
+ }
65
+
61
66
// Go extension configuration for a workspace.
62
67
export class Configuration {
63
68
constructor (
64
- private workspaceIsTrusted : boolean ,
65
- private getConfiguration : typeof vscode . workspace . getConfiguration ) { }
69
+ private workspaceIsTrusted = false ,
70
+ private getConfiguration = vscode . workspace . getConfiguration ) { }
66
71
67
- public async toggleWorkspaceIsTrusted ( ) {
72
+ public toggleWorkspaceIsTrusted ( ) {
68
73
this . workspaceIsTrusted = ! this . workspaceIsTrusted ;
69
- await updateWorkspaceState ( WORKSPACE_IS_TRUSTED_KEY , this . workspaceIsTrusted ) ;
74
+ return this . workspaceIsTrusted ;
70
75
}
71
76
72
77
// returns a Proxied vscode.WorkspaceConfiguration, which prevents
73
78
// from using the workspace configuration if the workspace is untrusted.
74
- public get < T > ( uri ?: vscode . Uri ) : vscode . WorkspaceConfiguration {
75
- const cfg = this . getConfiguration ( 'go' , uri ) ;
76
- if ( this . workspaceIsTrusted ) {
79
+ public get < T > ( section : string , uri ?: vscode . Uri ) : vscode . WorkspaceConfiguration {
80
+ const cfg = this . getConfiguration ( section , uri ) ;
81
+ if ( section !== 'go' || this . workspaceIsTrusted ) {
77
82
return cfg ;
78
83
}
79
84
@@ -117,3 +122,24 @@ class WrappedConfiguration implements vscode.WorkspaceConfiguration {
117
122
return this . _wrapped . update ( section , value , configurationTarget , overrideInLanguage ) ;
118
123
}
119
124
}
125
+
126
+ // getGoConfig is declared as an exported const rather than a function, so it can be stubbbed in testing.
127
+ export const getGoConfig = ( uri ?: vscode . Uri ) => {
128
+ return getConfig ( 'go' , uri ) ;
129
+ } ;
130
+
131
+ // getGoplsConfig returns the user's gopls configuration.
132
+ export function getGoplsConfig ( uri ?: vscode . Uri ) {
133
+ return getConfig ( 'gopls' , uri ) ;
134
+ }
135
+
136
+ function getConfig ( section : string , uri ?: vscode . Uri ) {
137
+ if ( ! uri ) {
138
+ if ( vscode . window . activeTextEditor ) {
139
+ uri = vscode . window . activeTextEditor . document . uri ;
140
+ } else {
141
+ uri = null ;
142
+ }
143
+ }
144
+ return defaultConfig ? defaultConfig . get ( section , uri ) : new Configuration ( ) . get ( section , uri ) ;
145
+ }
0 commit comments