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"
|
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
|
|
|
|
switch params.Command {
|
|
|
|
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
|
|
|
|
}
|
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"},
|
|
|
|
Env: snapshot.Config(ctx).Env,
|
|
|
|
WorkingDir: snapshot.View().Folder().Filename(),
|
|
|
|
}
|
|
|
|
if _, err := inv.Run(ctx); 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
|
|
|
|
}
|
|
|
|
// 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-02-24 14:46:08 -07:00
|
|
|
Env: snapshot.Config(ctx).Env,
|
|
|
|
WorkingDir: snapshot.View().Folder().Filename(),
|
|
|
|
}
|
|
|
|
if _, err := inv.Run(ctx); err != nil {
|
2020-02-06 12:50:26 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-18 23:21:54 -06:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|