mirror of
https://github.com/golang/go
synced 2024-11-18 22:34:45 -07:00
1081e67f6b
This changes adds basic support for running `go mod tidy` as a code action when a user opens a go.mod file. When we have a command available like `go mod tidy -check`, we will be able to return edits as part of the codeAction. For now, we execute the command directly. This change also required a few modifications to our handling of file kinds so that we could distinguish between a Go file and a go.mod file. Change-Id: I343079b8886724b67f90a314e45639545a34f21e Reviewed-on: https://go-review.googlesource.com/c/tools/+/196322 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
35 lines
972 B
Go
35 lines
972 B
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 := s.session.ViewOf(uri)
|
|
f, err := view.GetFile(ctx, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, ok := f.(source.ModFile); !ok {
|
|
return nil, errors.Errorf("%s is not a mod file", uri)
|
|
}
|
|
// Run go.mod tidy on the view.
|
|
if err := source.ModTidy(ctx, view); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|