internal/lsp/source: be consistent about command identifiers

The primary identifier for gopls commands was changed from Command.Name
to Command.ID(), but this change was not made in the commands passed
back to the client via ExecuteCommandOptions.

Fix this, and ensure that our regtests actually check commands against
the list of supported commands that has been sent.

For golang/go#41985

Change-Id: If566584f157e8a86d26eac6353dbfd772b298cfc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/262597
Run-TryBot: Robert Findley <rfindley@google.com>
Trust: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
diff --git a/gopls/internal/regtest/codelens_test.go b/gopls/internal/regtest/codelens_test.go
index 2fb0f71..1c2e709 100644
--- a/gopls/internal/regtest/codelens_test.go
+++ b/gopls/internal/regtest/codelens_test.go
@@ -121,7 +121,7 @@
 		if found.Command.Command == "" {
 			t.Fatalf("did not find lens %q, got %v", want, lenses)
 		}
-		if _, err := env.Editor.Server.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{
+		if _, err := env.Editor.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{
 			Command:   found.Command.Command,
 			Arguments: found.Command.Arguments,
 		}); err != nil {
diff --git a/gopls/internal/regtest/wrappers.go b/gopls/internal/regtest/wrappers.go
index a31fe2a..12bb26b 100644
--- a/gopls/internal/regtest/wrappers.go
+++ b/gopls/internal/regtest/wrappers.go
@@ -283,7 +283,7 @@
 	if !found {
 		e.T.Fatalf("found no command with the ID %s", cmd.ID())
 	}
-	if _, err := e.Editor.Server.ExecuteCommand(e.Ctx, &protocol.ExecuteCommandParams{
+	if _, err := e.Editor.ExecuteCommand(e.Ctx, &protocol.ExecuteCommandParams{
 		Command:   lens.Command.Command,
 		Arguments: lens.Command.Arguments,
 	}); err != nil {
diff --git a/internal/lsp/command.go b/internal/lsp/command.go
index 7292642..959a2a5 100644
--- a/internal/lsp/command.go
+++ b/internal/lsp/command.go
@@ -34,7 +34,7 @@
 	}
 	var match bool
 	for _, name := range s.session.Options().SupportedCommands {
-		if command.Name == name {
+		if command.ID() == name {
 			match = true
 			break
 		}
diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go
index c2389a5..34f6826 100644
--- a/internal/lsp/fake/editor.go
+++ b/internal/lsp/fake/editor.go
@@ -713,7 +713,7 @@
 		// Execute any commands. The specification says that commands are
 		// executed after edits are applied.
 		if action.Command != nil {
-			if _, err := e.Server.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
+			if _, err := e.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
 				Command:   action.Command.Command,
 				Arguments: action.Command.Arguments,
 			}); err != nil {
@@ -724,6 +724,24 @@
 	return nil
 }
 
+func (e *Editor) ExecuteCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
+	if e.Server == nil {
+		return nil, nil
+	}
+	var match bool
+	// Ensure that this command was actually listed as a supported command.
+	for _, command := range e.serverCapabilities.ExecuteCommandProvider.Commands {
+		if command == params.Command {
+			match = true
+			break
+		}
+	}
+	if !match {
+		return nil, fmt.Errorf("unsupported command %q", params.Command)
+	}
+	return e.Server.ExecuteCommand(ctx, params)
+}
+
 func convertEdits(protocolEdits []protocol.TextEdit) []Edit {
 	var edits []Edit
 	for _, lspEdit := range protocolEdits {
@@ -785,7 +803,7 @@
 		Command:   source.CommandGenerate.ID(),
 		Arguments: jsonArgs,
 	}
-	if _, err := e.Server.ExecuteCommand(ctx, params); err != nil {
+	if _, err := e.ExecuteCommand(ctx, params); err != nil {
 		return fmt.Errorf("running generate: %v", err)
 	}
 	// Unfortunately we can't simply poll the workdir for file changes here,
diff --git a/internal/lsp/source/options.go b/internal/lsp/source/options.go
index 0324b3f..a80ab3b 100644
--- a/internal/lsp/source/options.go
+++ b/internal/lsp/source/options.go
@@ -68,7 +68,7 @@
 	optionsOnce.Do(func() {
 		var commands []string
 		for _, c := range Commands {
-			commands = append(commands, c.Name)
+			commands = append(commands, c.ID())
 		}
 		defaultOptions = &Options{
 			ClientOptions: ClientOptions{