internal/lsp: return err if ExecuteCommand prerequisites aren't met

Some Commands require that the buffer is saved before running them (i.e.
generate, test and toggle details). If the buffer isn't saved gopls
sends a ShowMessage request to the client. Before this change it
did not return any error from the ExecuteCommand request itself (unless
ShowMessage failed).

A progress token can be provided by the client as a part of the
ExecuteCommand request, and that is, according to the LSP spec one way
to start a WorkDoneProgress. At this point the client expects
that gopls send progress updates.

With this change, ExecuteCommand now return an error if the buffer
isn't saved, so that the client know that it shouldn't expect any
progress updates with this specific token.

Change-Id: I8a7af20a0c1532fc4ad03efd4e04bfb1a6871644
Reviewed-on: https://go-review.googlesource.com/c/tools/+/248766
Run-TryBot: Pontus Leitzler <leitzler@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/internal/lsp/command.go b/internal/lsp/command.go
index 537f94d..c8cb448 100644
--- a/internal/lsp/command.go
+++ b/internal/lsp/command.go
@@ -53,10 +53,12 @@
 		switch params.Command {
 		case source.CommandTest.Name, source.CommandGenerate.Name, source.CommandToggleDetails.Name:
 			// TODO(PJW): for Toggle, not an error if it is being disabled
-			return nil, s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
+			err := fmt.Errorf("cannot run command %s: unsaved files in the view", params.Command)
+			s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
 				Type:    protocol.Error,
-				Message: fmt.Sprintf("cannot run command %s: unsaved files in the view", params.Command),
+				Message: err.Error(),
 			})
+			return nil, err
 		}
 	}
 	// If the command has a suggested fix function available, use it and apply