src/goLanguageServer: prompt users to file an issue if they opted out

If the user has explicitly disabled the language server when the default is set true, prompt the message, but never prompt it more often than every three months.
Currently, only enabled when the default is true, which means it's the nightly extension - guarded by isNightly from goMain.ts.

Fixes golang/vscode-go#738

Change-Id: I3f4b454a482df02feda03ca14808b147fb39a81a
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/273186
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Trust: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/src/goLanguageServer.ts b/src/goLanguageServer.ts
index f62fa4a..8a3348d 100644
--- a/src/goLanguageServer.ts
+++ b/src/goLanguageServer.ts
@@ -1114,8 +1114,8 @@
 
 export const goplsSurveyConfig = 'goplsSurveyConfig';
 
-function getSurveyConfig(): SurveyConfig {
-	const saved = getFromGlobalState(goplsSurveyConfig);
+function getSurveyConfig(surveyConfigKey = goplsSurveyConfig): SurveyConfig {
+	const saved = getFromGlobalState(surveyConfigKey);
 	if (saved === undefined) {
 		return {};
 	}
@@ -1390,16 +1390,19 @@
 }
 
 export async function promptForLanguageServerDefaultChange(cfg: vscode.WorkspaceConfiguration) {
+	const useLanguageServer = cfg.inspect<boolean>('useLanguageServer');
+	if (useLanguageServer.globalValue !== undefined || useLanguageServer.workspaceValue !== undefined) {
+		if (!cfg['useLanguageServer']) {  // ask users who explicitly disabled.
+			promptForLanguageServerOptOutSurvey();
+		}
+		return;  // user already explicitly set the field.
+	}
+
 	const promptedForLSDefaultChangeKey = `promptedForLSDefaultChange`;
 	if (getFromGlobalState(promptedForLSDefaultChangeKey, false)) {
 		return;
 	}
 
-	const useLanguageServer = cfg.inspect<boolean>('useLanguageServer');
-	if (useLanguageServer.globalValue !== undefined || useLanguageServer.workspaceValue !== undefined) {
-		return;  // user already explicitly set the field.
-	}
-
 	const selected = await vscode.window.showInformationMessage(
 		`"go.useLanguageServer" is enabled by default. If you need to disable it, please configure in the settings.`,
 		'Open Settings', 'OK');
@@ -1410,3 +1413,36 @@
 	}
 	updateGlobalState(promptedForLSDefaultChangeKey, true);
 }
+
+// Prompt users who disabled the language server and ask to file an issue.
+async function promptForLanguageServerOptOutSurvey() {
+	const promptedForLSOptOutSurveyKey = `promptedForLSOptOutSurvey`;
+	const value = getSurveyConfig(promptedForLSOptOutSurveyKey);  // We use only 'prompt' and 'lastDatePrompted' fields.
+
+	if (value?.prompt === false ||
+		(value?.lastDatePrompted && daysBetween(value.lastDatePrompted, new Date()) < 90)) {
+		return;
+	}
+
+	value.lastDatePrompted = new Date();
+
+	const selected = await vscode.window.showInformationMessage(
+		`Looks like you've disabled the language server. Would you be willing to file an issue and tell us why you had to disable it?`,
+		'Yes', 'Not now', 'Never');
+	switch (selected) {
+		case 'Yes':
+			const title = 'gopls: automated issue report (opt out)';
+			const body = `
+Please tell us why you had to disable the language server.
+
+`;
+			const url = `https://github.com/golang/vscode-go/issues/new?title=${title}&labels=upstream-tools&body=${body}`;
+			await vscode.env.openExternal(vscode.Uri.parse(url));
+			break;
+		case 'Never':
+			value.prompt = false;
+			break;
+		default:
+	}
+	updateGlobalState(promptedForLSOptOutSurveyKey, JSON.stringify(value));
+}