1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:44:46 -07:00
go/internal/lsp/command.go
Marwan Sulaiman 63da46f303 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>
2020-03-17 04:34:34 +00:00

83 lines
2.5 KiB
Go

// 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 (
"context"
"strings"
"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)
}
uri := protocol.DocumentURI(params.Arguments[0].(string))
snapshot, _, ok, err := s.beginFileRequest(uri, source.Mod)
if !ok {
return nil, err
}
// Run go.mod tidy on the view.
inv := gocommand.Invocation{
Verb: "mod",
Args: []string{"tidy"},
Env: snapshot.Config(ctx).Env,
WorkingDir: snapshot.View().Folder().Filename(),
}
if _, err := inv.Run(ctx); err != nil {
return nil, err
}
case "upgrade.dependency":
if len(params.Arguments) < 2 {
return nil, errors.Errorf("expected one file URI and one dependency for call to `go get`, got %v", params.Arguments)
}
uri := protocol.DocumentURI(params.Arguments[0].(string))
deps := params.Arguments[1].(string)
snapshot, _, ok, err := s.beginFileRequest(uri, source.UnknownKind)
if !ok {
return nil, err
}
// Run "go get" on the dependency to upgrade it to the latest version.
inv := gocommand.Invocation{
Verb: "get",
Args: strings.Split(deps, " "),
Env: snapshot.Config(ctx).Env,
WorkingDir: snapshot.View().Folder().Filename(),
}
if _, err := inv.Run(ctx); err != nil {
return nil, err
}
}
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
}