Skip to content

Commit

Permalink
internal/lsp: return err if ExecuteCommand prerequisites aren't met
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
leitzler authored and findleyr committed Aug 19, 2020
1 parent 188abfa commit cf83efe
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions internal/lsp/command.go
Expand Up @@ -53,10 +53,12 @@ func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCom
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
Expand Down

0 comments on commit cf83efe

Please sign in to comment.