src/goMain.ts: refactor codelens activation

Added static activation methods to code lens providers
to simplify goMain activation.

Change-Id: I99b810cdf58504ca9a7df1f21819fbe9ad90f947
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/404578
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
diff --git a/src/goMain.ts b/src/goMain.ts
index 7812323..fa14394 100644
--- a/src/goMain.ts
+++ b/src/goMain.ts
@@ -199,11 +199,8 @@
 
 	registerCommand('go.environment.status', (_ctx, goCtx) => () => expandGoStatusBar(goCtx));
 
-	const testCodeLensProvider = new GoRunTestCodeLensProvider();
-	const referencesCodeLensProvider = new GoReferencesCodeLensProvider();
-
-	ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, testCodeLensProvider));
-	ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, referencesCodeLensProvider));
+	GoRunTestCodeLensProvider.activate(ctx);
+	GoReferencesCodeLensProvider.activate(ctx);
 
 	// debug
 	ctx.subscriptions.push(
@@ -306,11 +303,6 @@
 				clearCacheForTools();
 			}
 
-			if (updatedGoConfig['enableCodeLens']) {
-				testCodeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['runtest']);
-				referencesCodeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['references']);
-			}
-
 			if (e.affectsConfiguration('go.formatTool')) {
 				checkToolExists(getFormatTool(updatedGoConfig));
 			}
diff --git a/src/goReferencesCodelens.ts b/src/goReferencesCodelens.ts
index fc9cb67..3dca097 100644
--- a/src/goReferencesCodelens.ts
+++ b/src/goReferencesCodelens.ts
@@ -14,6 +14,7 @@
 import { GoReferenceProvider } from './language/legacy/goReferences';
 import { getBinPath } from './util';
 import vscode = require('vscode');
+import { GO_MODE } from './goMode';
 
 const methodRegex = /^func\s+\(\s*\w+\s+\*?\w+\s*\)\s+/;
 
@@ -24,6 +25,22 @@
 }
 
 export class GoReferencesCodeLensProvider extends GoBaseCodeLensProvider {
+	static activate(ctx: vscode.ExtensionContext) {
+		const referencesCodeLensProvider = new this();
+		ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, referencesCodeLensProvider));
+		ctx.subscriptions.push(
+			vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
+				if (!e.affectsConfiguration('go')) {
+					return;
+				}
+				const updatedGoConfig = getGoConfig();
+				if (updatedGoConfig['enableCodeLens']) {
+					referencesCodeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['references']);
+				}
+			})
+		);
+	}
+
 	public provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
 		if (!this.enabled) {
 			return [];
diff --git a/src/goRunTestCodelens.ts b/src/goRunTestCodelens.ts
index 77847ca..6450308 100644
--- a/src/goRunTestCodelens.ts
+++ b/src/goRunTestCodelens.ts
@@ -13,8 +13,26 @@
 import { GoBaseCodeLensProvider } from './goBaseCodelens';
 import { GoDocumentSymbolProvider } from './goDocumentSymbols';
 import { getBenchmarkFunctions, getTestFunctions } from './testUtils';
+import { GoExtensionContext } from './context';
+import { GO_MODE } from './goMode';
 
 export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
+	static activate(ctx: vscode.ExtensionContext) {
+		const testCodeLensProvider = new this();
+		ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, testCodeLensProvider));
+		ctx.subscriptions.push(
+			vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
+				if (!e.affectsConfiguration('go')) {
+					return;
+				}
+				const updatedGoConfig = getGoConfig();
+				if (updatedGoConfig['enableCodeLens']) {
+					testCodeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['runtest']);
+				}
+			})
+		);
+	}
+
 	private readonly benchmarkRegex = /^Benchmark.+/;
 
 	public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {