src/goVulncheck: add go.vulncheck.toggle command

This updates "go.diagnostic.vulncheck" setting.

Change-Id: I73f9d613f61e98f0e4c90e169da0652bd67b87f5
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/456039
Reviewed-by: Suzy Mueller <suzmue@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Auto-Submit: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/docs/commands.md b/docs/commands.md
index b3d2ee6..67255a6 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -151,6 +151,10 @@
 
 Toggles between file in current active editor and the corresponding test file.
 
+### `Go: Toggle Vulncheck`
+
+Toggle the display of vulnerability analysis in dependencies.
+
 ### `Go: Add Tags To Struct Fields`
 
 Add tags configured in go.addTags setting to selected struct using gomodifytags
diff --git a/package.json b/package.json
index b731ec4..efe025a 100644
--- a/package.json
+++ b/package.json
@@ -386,6 +386,11 @@
         "description": "Toggles between file in current active editor and the corresponding test file."
       },
       {
+        "command": "go.vulncheck.toggle",
+        "title": "Go: Toggle Vulncheck",
+        "description": "Toggle the display of vulnerability analysis in dependencies."
+      },
+      {
         "command": "go.add.tags",
         "title": "Go: Add Tags To Struct Fields",
         "description": "Add tags configured in go.addTags setting to selected struct using gomodifytags"
diff --git a/src/goMain.ts b/src/goMain.ts
index e21c41b..08f36a9 100644
--- a/src/goMain.ts
+++ b/src/goMain.ts
@@ -66,7 +66,7 @@
 import { GoExplorerProvider } from './goExplorer';
 import { GoExtensionContext } from './context';
 import * as commands from './commands';
-import { VulncheckOutputLinkProvider } from './goVulncheck';
+import { toggleVulncheckCommandFactory, VulncheckOutputLinkProvider } from './goVulncheck';
 
 const goCtx: GoExtensionContext = {};
 
@@ -211,6 +211,7 @@
 
 	// Vulncheck output link provider.
 	VulncheckOutputLinkProvider.activate(ctx);
+	registerCommand('go.vulncheck.toggle', toggleVulncheckCommandFactory);
 
 	return extensionAPI;
 }
diff --git a/src/goVulncheck.ts b/src/goVulncheck.ts
index cbf2890..c835864 100644
--- a/src/goVulncheck.ts
+++ b/src/goVulncheck.ts
@@ -4,6 +4,9 @@
  *--------------------------------------------------------*/
 import path = require('path');
 import vscode = require('vscode');
+import { URI } from 'vscode-uri';
+import { getGoConfig } from './config';
+
 function moduleVersion(mod: string, ver: string | undefined) {
 	if (!ver) {
 		return 'N/A';
@@ -329,7 +332,7 @@
 					const line = filePosPattern[2];
 					const col = filePosPattern[3];
 					const fragment = col ? { fragment: `L${line},${col}` } : { fragment: `L${line}` };
-					const uri = vscode.Uri.file(fname).with(fragment);
+					const uri = URI.file(fname).with(fragment);
 					const start = readLine.text.indexOf(filePosPattern[1]);
 					const end = readLine.text.indexOf(filePosPattern[0]) + filePosPattern[0].length;
 					const link = new vscode.DocumentLink(new vscode.Range(i, start, i, end), uri);
@@ -341,3 +344,34 @@
 		return ret;
 	}
 }
+
+export const toggleVulncheckCommandFactory = () => () => {
+	const editor = vscode.window.activeTextEditor;
+	if (!editor) {
+		return;
+	}
+	const documentUri = editor?.document.uri;
+	toggleVulncheckCommand(documentUri);
+};
+
+function toggleVulncheckCommand(uri?: URI) {
+	const goCfgName = 'diagnostic.vulncheck';
+	const cfg = getGoConfig(uri);
+	const { globalValue, workspaceValue, workspaceFolderValue } = cfg.inspect(goCfgName) || {};
+	if (workspaceFolderValue) {
+		const newValue = workspaceFolderValue === 'Imports' ? 'Off' : 'Imports';
+		cfg.update(goCfgName, newValue);
+		return;
+	}
+	if (workspaceValue) {
+		const newValue = workspaceValue === 'Imports' ? 'Off' : 'Imports';
+		cfg.update(goCfgName, newValue, false);
+		return;
+	}
+	if (globalValue) {
+		const newValue = globalValue === 'Imports' ? 'Off' : 'Imports';
+		cfg.update(goCfgName, newValue, true);
+		return;
+	}
+	cfg.update(goCfgName, 'Imports');
+}