1
0
mirror of https://github.com/golang/go synced 2024-10-01 06:08:32 -06:00
go/internal/lsp/source/tidy.go
Rebecca Stambler 1081e67f6b internal/lsp: support running go mod tidy as a code action
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>
2019-09-20 13:08:46 +00:00

20 lines
490 B
Go

package source
import (
"context"
)
func ModTidy(ctx context.Context, view View) error {
cfg := view.Config(ctx)
// Running `go mod tidy` modifies the file on disk directly.
// Ideally, we should return modules that could possibly be removed
// and apply each action as an edit.
//
// TODO(rstambler): This will be possible when golang/go#27005 is resolved.
if _, err := invokeGo(ctx, view.Folder().Filename(), cfg.Env, "mod", "tidy"); err != nil {
return err
}
return nil
}