extension/src/language: raise window after gopls showDocument

When the user clicks a source link in one of gopls' web pages
(e.g. the assembly listing), gopls sends a window/showDocument
request with takeFocus=true. VS Code opens the document but,
since the browser is the foreground application, the editor
window stays hidden behind it.

VS Code 1.128 added the workbench.action.focusWindow command
(microsoft/vscode#208093). Add showDocument middleware that
invokes it after a successful non-external showDocument with
takeFocus set. On older versions of VS Code the command does
not exist, and the error is ignored.

Updates golang/go#67478

Change-Id: Ie382f73932047676ed391bb4d9b175d8d8c6ad2e
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/862186
Auto-Submit: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
diff --git a/extension/src/language/goLanguageServer.ts b/extension/src/language/goLanguageServer.ts
index 6253d1b..38810b9 100644
--- a/extension/src/language/goLanguageServer.ts
+++ b/extension/src/language/goLanguageServer.ts
@@ -566,6 +566,32 @@
 					}
 					next(token, params);
 				},
+				window: {
+					// gopls sends window/showDocument requests in response to
+					// clicks on source links in its web pages (e.g. the assembly
+					// listing of golang/go#67478), in which case the browser,
+					// not VS Code, is the foreground application. VS Code's
+					// showDocument handler honors takeFocus only within the
+					// workbench, so we must explicitly raise the window.
+					// See microsoft/vscode#208093.
+					showDocument: async (params, token, next) => {
+						const result = await next(params, token);
+						if (
+							!(result instanceof ResponseError) &&
+							result.success &&
+							params.takeFocus &&
+							!params.external
+						) {
+							try {
+								// Added in VS Code 1.128; fails on older versions.
+								await vscode.commands.executeCommand('workbench.action.focusWindow');
+							} catch {
+								// ignore
+							}
+						}
+						return result;
+					}
+				},
 				resolveCommand: async (
 					param: InteractiveExecuteCommandParams,
 					next: InteractiveResolveCommandSignature