x/tools/gopls: run go generate through CodeLens

This change adds support for recognizing a //go:generate directive
and offering a CodeLens that will then send a "generate" command to
the server to run "go generate" or "go generate ./...". Because
"go generate" can only be executed per package, there is no need to show
the CodeLens on top of every //go:generate comment. Therefore, only the
top directive will be considered.

The stdout/stderr of the go generate command will be piped to the logger
while stderr will also be sent to the editor as a window/showMessage

The user will only know when the process starts and when it ends so that they wouldn't
get bogged with a large number of message windows popping up. However, they can
check the logs for all the details.

If a user wants to cancel the "go generate" command, they will be able
to do so with a "Cancel" ActionItem that the server will offer to the client

Fixes golang/go#37680

Change-Id: I89a9617521eab20859cb2215db133f34fda856c7
Reviewed-on: https://go-review.googlesource.com/c/tools/+/222247
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/command.go b/internal/lsp/command.go
index 2c00b7e..6c5f51f 100644
--- a/internal/lsp/command.go
+++ b/internal/lsp/command.go
@@ -1,3 +1,7 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
 package lsp
 
 import (
@@ -7,11 +11,18 @@
 	"golang.org/x/tools/internal/gocommand"
 	"golang.org/x/tools/internal/lsp/protocol"
 	"golang.org/x/tools/internal/lsp/source"
+	"golang.org/x/tools/internal/xcontext"
 	errors "golang.org/x/xerrors"
 )
 
 func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
 	switch params.Command {
+	case "generate":
+		dir, recursive, err := getGenerateRequest(params.Arguments)
+		if err != nil {
+			return nil, err
+		}
+		go s.runGenerate(xcontext.Detach(ctx), dir, recursive)
 	case "tidy":
 		if len(params.Arguments) == 0 || len(params.Arguments) > 1 {
 			return nil, errors.Errorf("expected one file URI for call to `go mod tidy`, got %v", params.Arguments)
@@ -54,3 +65,18 @@
 	}
 	return nil, nil
 }
+
+func getGenerateRequest(args []interface{}) (string, bool, error) {
+	if len(args) != 2 {
+		return "", false, errors.Errorf("expected exactly 2 arguments but got %d", len(args))
+	}
+	dir, ok := args[0].(string)
+	if !ok {
+		return "", false, errors.Errorf("expected dir to be a string value but got %T", args[0])
+	}
+	recursive, ok := args[1].(bool)
+	if !ok {
+		return "", false, errors.Errorf("expected recursive to be a boolean but got %T", args[1])
+	}
+	return dir, recursive, nil
+}