Skip to content

Commit bc8b63e

Browse files
committed
src/config: move getGoConfig/getGoplsConfig to config
And let them use the new Configuration class For #1094 Change-Id: I96d0cc32b6499e59d1062c2b04031bc675703a24 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/283255 Trust: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Suzy Mueller <[email protected]>
1 parent 3a013bd commit bc8b63e

39 files changed

+118
-77
lines changed

src/config.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function initConfig(ctx: vscode.ExtensionContext) {
2020
const isTrusted = getFromWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, false);
2121
defaultConfig = new Configuration(isTrusted, vscode.workspace.getConfiguration);
2222
ctx.subscriptions.push(
23-
vscode.commands.registerCommand('go.workspace.isTrusted.toggle', defaultConfig.toggleWorkspaceIsTrusted)
23+
vscode.commands.registerCommand('go.workspace.isTrusted.toggle', toggleWorkspaceIsTrusted)
2424
);
2525

2626
if (isTrusted) {
@@ -40,7 +40,7 @@ export async function initConfig(ctx: vscode.ExtensionContext) {
4040
'More Info');
4141
switch (val) {
4242
case 'Trust This Workspace':
43-
await defaultConfig.toggleWorkspaceIsTrusted();
43+
await toggleWorkspaceIsTrusted();
4444
break;
4545
case 'More Info':
4646
vscode.env.openExternal(
@@ -58,22 +58,27 @@ function ignoredWorkspaceConfig(cfg: vscode.WorkspaceConfiguration, keys: string
5858
});
5959
}
6060

61+
async function toggleWorkspaceIsTrusted() {
62+
const v = defaultConfig.toggleWorkspaceIsTrusted();
63+
await updateWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, v);
64+
}
65+
6166
// Go extension configuration for a workspace.
6267
export class Configuration {
6368
constructor(
64-
private workspaceIsTrusted: boolean,
65-
private getConfiguration: typeof vscode.workspace.getConfiguration) { }
69+
private workspaceIsTrusted = false,
70+
private getConfiguration = vscode.workspace.getConfiguration) { }
6671

67-
public async toggleWorkspaceIsTrusted() {
72+
public toggleWorkspaceIsTrusted() {
6873
this.workspaceIsTrusted = !this.workspaceIsTrusted;
69-
await updateWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, this.workspaceIsTrusted);
74+
return this.workspaceIsTrusted;
7075
}
7176

7277
// returns a Proxied vscode.WorkspaceConfiguration, which prevents
7378
// 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) {
7782
return cfg;
7883
}
7984

@@ -117,3 +122,24 @@ class WrappedConfiguration implements vscode.WorkspaceConfiguration {
117122
return this._wrapped.update(section, value, configurationTarget, overrideInLanguage);
118123
}
119124
}
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+
}

src/goBuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import path = require('path');
77
import vscode = require('vscode');
8+
import { getGoConfig } from './config';
89
import { toolExecutionEnvironment } from './goEnv';
910
import { buildDiagnosticCollection } from './goMain';
1011
import { isModSupported } from './goModules';
@@ -13,7 +14,6 @@ import { diagnosticsStatusBarItem, outputChannel } from './goStatus';
1314
import { getTestFlags } from './testUtils';
1415
import {
1516
getCurrentGoPath,
16-
getGoConfig,
1717
getModuleCache,
1818
getTempFilePath,
1919
getWorkspaceFolderPath,

src/goCover.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import fs = require('fs');
99
import path = require('path');
1010
import vscode = require('vscode');
11+
import { getGoConfig } from './config';
1112
import { isModSupported } from './goModules';
1213
import { getImportPathToFolder } from './goPackages';
1314
import { getTestFlags, goTest, showTestOutput, TestConfig } from './testUtils';
14-
import { getGoConfig } from './util';
1515
import { fixDriveCasingInWindows } from './utils/pathUtils';
1616

1717
let gutterSvgs: { [key: string]: string; };

src/goDebugConfiguration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import path = require('path');
99
import vscode = require('vscode');
1010
import parse = require('yargs-parser');
1111
import unparse = require('yargs-unparser');
12+
import { getGoConfig } from './config';
1213
import { toolExecutionEnvironment } from './goEnv';
1314
import { promptForMissingTool } from './goInstallTools';
1415
import { packagePathToGoModPathMap } from './goModules';
1516
import { getFromGlobalState, updateGlobalState } from './stateUtils';
16-
import { getBinPath, getGoConfig, resolvePath } from './util';
17+
import { getBinPath, resolvePath } from './util';
1718
import { parseEnvFiles } from './utils/envUtils';
1819

1920
export class GoDebugConfigurationProvider implements vscode.DebugConfigurationProvider {

src/goDeclaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import cp = require('child_process');
99
import path = require('path');
1010
import vscode = require('vscode');
11+
import { getGoConfig } from './config';
1112
import { toolExecutionEnvironment } from './goEnv';
1213
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
1314
import { getModFolderPath, promptToUpdateToolForModules } from './goModules';
1415
import {
1516
byteOffsetAt,
1617
getBinPath,
1718
getFileArchive,
18-
getGoConfig,
1919
getModuleCache,
2020
getWorkspaceFolderPath,
2121
goKeywords,

src/goEnv.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import path = require('path');
99
import vscode = require('vscode');
10-
import { getCurrentGoPath, getGoConfig, getToolsGopath, resolvePath } from './util';
10+
import { getGoConfig } from './config';
11+
import { getCurrentGoPath, getToolsGopath, resolvePath } from './util';
1112

1213
// toolInstallationEnvironment returns the environment in which tools should
1314
// be installed. It always returns a new object.

src/goEnvironmentStatus.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import path = require('path');
1313
import { promisify } from 'util';
1414
import vscode = require('vscode');
1515
import WebRequest = require('web-request');
16+
import { getGoConfig } from './config';
1617
import { toolInstallationEnvironment } from './goEnv';
1718
import { logVerbose } from './goLogging';
1819
import { addGoStatus, goEnvStatusbarItem, outputChannel, removeGoStatus } from './goStatus';
1920
import { getFromGlobalState, getFromWorkspaceState, updateGlobalState, updateWorkspaceState } from './stateUtils';
2021
import {
21-
getBinPath, getCheckForToolsUpdatesConfig, getGoConfig, getGoVersion,
22+
getBinPath, getCheckForToolsUpdatesConfig, getGoVersion,
2223
getTempFilePath, GoVersion, rmdirRecursive } from './util';
2324
import {
2425
correctBinname,

src/goExtraInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import vscode = require('vscode');
99
import { CancellationToken, Hover, HoverProvider, Position, TextDocument, WorkspaceConfiguration } from 'vscode';
10+
import { getGoConfig } from './config';
1011
import { definitionLocation } from './goDeclaration';
11-
import { getGoConfig } from './util';
1212

1313
export class GoHoverProvider implements HoverProvider {
1414
private goConfig: WorkspaceConfiguration | undefined;

src/goFormat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import cp = require('child_process');
99
import path = require('path');
1010
import vscode = require('vscode');
11+
import { getGoConfig } from './config';
1112
import { toolExecutionEnvironment } from './goEnv';
1213
import { promptForMissingTool, promptForUpdatingTool } from './goInstallTools';
13-
import { getBinPath, getGoConfig } from './util';
14+
import { getBinPath } from './util';
1415
import { killProcessTree } from './utils/processUtils';
1516

1617
export class GoDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {

src/goGenerateTests.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import cp = require('child_process');
99
import path = require('path');
1010
import vscode = require('vscode');
11+
import { getGoConfig } from './config';
1112

1213
import { toolExecutionEnvironment } from './goEnv';
1314
import { promptForMissingTool } from './goInstallTools';
1415
import { GoDocumentSymbolProvider } from './goOutline';
1516
import { outputChannel } from './goStatus';
16-
import { getBinPath, getGoConfig } from './util';
17+
import { getBinPath } from './util';
1718

1819
const generatedWord = 'Generated ';
1920

0 commit comments

Comments
 (0)