2019-09-18 23:21:54 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
// Confirm that this action is being taken on a go.mod file.
|
|
|
|
uri := span.NewURI(params.Arguments[0].(string))
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := view.Snapshot().GetFile(uri)
|
2019-09-18 23:21:54 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
if fh.Identity().Kind != source.Mod {
|
2019-09-18 23:21:54 -06:00
|
|
|
return nil, errors.Errorf("%s is not a mod file", uri)
|
|
|
|
}
|
|
|
|
// Run go.mod tidy on the view.
|
2019-12-17 14:13:33 -07:00
|
|
|
if _, err := source.InvokeGo(ctx, view.Folder().Filename(), view.Config(ctx).Env, "mod", "tidy"); err != nil {
|
2019-09-18 23:21:54 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|