2020-03-10 22:49:10 -06:00
|
|
|
// 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.
|
|
|
|
|
2019-09-18 23:21:54 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-26 12:11:30 -07:00
|
|
|
"strings"
|
2019-09-18 23:21:54 -06:00
|
|
|
|
2020-02-24 14:46:08 -07:00
|
|
|
"golang.org/x/tools/internal/gocommand"
|
2019-09-18 23:21:54 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2020-03-23 06:35:36 -06:00
|
|
|
"golang.org/x/tools/internal/packagesinternal"
|
2020-03-10 22:49:10 -06:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
2019-09-18 23:21:54 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
|
|
|
|
switch params.Command {
|
2020-03-10 22:49:10 -06:00
|
|
|
case "generate":
|
|
|
|
dir, recursive, err := getGenerateRequest(params.Arguments)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go s.runGenerate(xcontext.Detach(ctx), dir, recursive)
|
2019-09-18 23:21:54 -06:00
|
|
|
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)
|
|
|
|
}
|
2020-02-13 11:46:49 -07:00
|
|
|
uri := protocol.DocumentURI(params.Arguments[0].(string))
|
|
|
|
snapshot, _, ok, err := s.beginFileRequest(uri, source.Mod)
|
|
|
|
if !ok {
|
2019-11-15 10:43:45 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-23 06:35:36 -06:00
|
|
|
cfg := snapshot.Config(ctx)
|
2019-09-18 23:21:54 -06:00
|
|
|
// Run go.mod tidy on the view.
|
2020-02-24 14:46:08 -07:00
|
|
|
inv := gocommand.Invocation{
|
|
|
|
Verb: "mod",
|
|
|
|
Args: []string{"tidy"},
|
2020-03-23 06:35:36 -06:00
|
|
|
Env: cfg.Env,
|
2020-02-24 14:46:08 -07:00
|
|
|
WorkingDir: snapshot.View().Folder().Filename(),
|
|
|
|
}
|
2020-03-23 06:35:36 -06:00
|
|
|
gocmdRunner := packagesinternal.GetGoCmdRunner(cfg)
|
|
|
|
if _, err := gocmdRunner.Run(ctx, inv); err != nil {
|
2019-09-18 23:21:54 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 12:50:26 -07:00
|
|
|
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)
|
|
|
|
}
|
2020-02-13 11:46:49 -07:00
|
|
|
uri := protocol.DocumentURI(params.Arguments[0].(string))
|
2020-02-26 12:11:30 -07:00
|
|
|
deps := params.Arguments[1].(string)
|
2020-02-13 11:46:49 -07:00
|
|
|
snapshot, _, ok, err := s.beginFileRequest(uri, source.UnknownKind)
|
|
|
|
if !ok {
|
2020-02-06 12:50:26 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-23 06:35:36 -06:00
|
|
|
cfg := snapshot.Config(ctx)
|
2020-02-06 12:50:26 -07:00
|
|
|
// Run "go get" on the dependency to upgrade it to the latest version.
|
2020-02-24 14:46:08 -07:00
|
|
|
inv := gocommand.Invocation{
|
|
|
|
Verb: "get",
|
2020-02-26 12:11:30 -07:00
|
|
|
Args: strings.Split(deps, " "),
|
2020-03-23 06:35:36 -06:00
|
|
|
Env: cfg.Env,
|
2020-02-24 14:46:08 -07:00
|
|
|
WorkingDir: snapshot.View().Folder().Filename(),
|
|
|
|
}
|
2020-03-23 06:35:36 -06:00
|
|
|
gocmdRunner := packagesinternal.GetGoCmdRunner(cfg)
|
|
|
|
if _, err := gocmdRunner.Run(ctx, inv); err != nil {
|
2020-02-06 12:50:26 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-10 22:49:10 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|