src/goMain.ts: call gc_details command through vscode API

Instead of calling through the low level LanguageClient's API.
And also show error messages when the command is invoked when the language
server is not running or the active document is not a go file.

Change-Id: I2f2e56aff96ec90226d4b129f5a88c4256656041
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/259838
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Peter Weinberger <pjw@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
diff --git a/src/goLanguageServer.ts b/src/goLanguageServer.ts
index 0fac38a..35da713 100644
--- a/src/goLanguageServer.ts
+++ b/src/goLanguageServer.ts
@@ -18,8 +18,6 @@
 	CloseAction,
 	CompletionItemKind,
 	ErrorAction,
-	ExecuteCommandParams,
-	ExecuteCommandRequest,
 	HandleDiagnosticsSignature,
 	InitializeError,
 	LanguageClient,
@@ -75,6 +73,7 @@
 let languageServerDisposable: vscode.Disposable;
 let latestConfig: LanguageServerConfig;
 export let serverOutputChannel: vscode.OutputChannel;
+export let languageServerIsRunning = false;
 let serverTraceChannel: vscode.OutputChannel;
 let crashCount = 0;
 
@@ -116,6 +115,7 @@
 		registerDefaultProviders(ctx);
 	}
 
+	languageServerIsRunning = started;
 	updateLanguageServerIconGoStatusBar(started, cfg.serverName);
 }
 
@@ -403,22 +403,6 @@
 	return c;
 }
 
-export function sendToggleCommand(cmd: string, file: vscode.Uri) {
-	if (languageClient === undefined || file === undefined) {
-		return;
-	}
-	const params: ExecuteCommandParams = {
-		command: cmd,
-		arguments: [file.toString()],
-	};
-	languageClient.sendRequest(ExecuteCommandRequest.type, params).then(
-		undefined,
-		(error: any) => {
-			return languageClient.logFailedRequest(ExecuteCommandRequest.type, error);
-		}
-	);
-}
-
 // createTestCodeLens adds the go.test.cursor and go.debug.cursor code lens
 function createTestCodeLens(lens: vscode.CodeLens): vscode.CodeLens[] {
 	// CodeLens argument signature in gopls is [fileName: string, testFunctions: string[], benchFunctions: string[]],
diff --git a/src/goMain.ts b/src/goMain.ts
index dea0b8a..44649ee 100644
--- a/src/goMain.ts
+++ b/src/goMain.ts
@@ -31,7 +31,8 @@
 	updateGoVarsFromConfig
 } from './goInstallTools';
 import {
-	promptForLanguageServerDefaultChange, sendToggleCommand,
+	languageServerIsRunning,
+	promptForLanguageServerDefaultChange,
 	startLanguageServerWithFallback, watchLanguageServerConfiguration
 } from './goLanguageServer';
 import { lintCode } from './goLint';
@@ -449,7 +450,16 @@
 	ctx.subscriptions.push(vscode.commands.registerCommand('go.install.package', installCurrentPackage));
 
 	ctx.subscriptions.push(vscode.commands.registerCommand('go.toggle.gc_details', () => {
-		sendToggleCommand('gc_details', vscode.window.activeTextEditor?.document.uri);
+		if (!languageServerIsRunning) {
+			vscode.window.showErrorMessage('"Go: Toggle gc details" command is available only when the language server is running');
+			return;
+		}
+		const doc = vscode.window.activeTextEditor?.document.uri.toString();
+		if (!doc || !doc.endsWith('.go')) {
+			vscode.window.showErrorMessage('"Go: Toggle gc details" command cannot run when no Go file is open.');
+			return;
+		}
+		vscode.commands.executeCommand('gc_details', doc);
 	}));
 
 	ctx.subscriptions.push(