mirror of
https://github.com/golang/go
synced 2024-11-18 18:24:48 -07:00
b753a1ba74
This is the first in a series of changes to move overlay handling to the snapshot instead of the session. We may not be able to fully get away from managing overlays on the session, but we should be able to only use overlays when they are known to the snapshot. Change-Id: I88b125117cd2cfbd0ff9ef16a944a34297c81b10 Reviewed-on: https://go-review.googlesource.com/c/tools/+/218324 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
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))
|
|
view, err := s.session.ViewOf(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
snapshot := view.Snapshot()
|
|
fh, err := snapshot.GetFile(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if fh.Identity().Kind != source.Mod {
|
|
return nil, errors.Errorf("%s is not a mod file", uri)
|
|
}
|
|
// Run go.mod tidy on the view.
|
|
// TODO: This should go through the ModTidyHandle on the view.
|
|
// That will also allow us to move source.InvokeGo into internal/lsp/cache.
|
|
if _, err := source.InvokeGo(ctx, view.Folder().Filename(), snapshot.Config(ctx).Env, "mod", "tidy"); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|