1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:54:40 -07:00
go/internal/lsp/command.go
Heschi Kreinick f7b8cc7bd0 internal/span,lsp: disambiguate URIs, DocumentURIs, and paths
Create a real type for protocol.DocumentURIs. Remove span.NewURI in
favor of path/URI-specific constructors. Remove span.Parse's ability to
parse URI-based spans, which appears to be totally unused.

As a consequence, we no longer mangle non-file URIs to start with
file://, and crash all over the place when one is opened.

Updates golang/go#33699.

Change-Id: Ic7347c9768e38002b4ad9c84471329d0af7d2e05
Reviewed-on: https://go-review.googlesource.com/c/tools/+/219482
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-02-14 22:51:03 +00:00

53 lines
1.7 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.URIFromURI(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.
if _, err := source.InvokeGo(ctx, view.Folder().Filename(), snapshot.Config(ctx).Env, "mod", "tidy"); 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 := span.URIFromURI(params.Arguments[0].(string))
view, err := s.session.ViewOf(uri)
if err != nil {
return nil, err
}
dep := params.Arguments[1].(string)
// Run "go get" on the dependency to upgrade it to the latest version.
if _, err := source.InvokeGo(ctx, view.Folder().Filename(), view.Snapshot().Config(ctx).Env, "get", dep); err != nil {
return nil, err
}
}
return nil, nil
}