2020-06-16 20:10:27 -06:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-07-14 22:19:10 -06:00
|
|
|
"go/ast"
|
2020-06-16 20:10:27 -06:00
|
|
|
"io/ioutil"
|
2020-07-14 22:19:10 -06:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-06-16 20:10:27 -06:00
|
|
|
|
|
|
|
"golang.org/x/mod/modfile"
|
|
|
|
"golang.org/x/tools/internal/event"
|
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2020-07-26 00:55:12 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
2020-06-16 20:10:27 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/memoize"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
2020-06-19 17:07:57 -06:00
|
|
|
type modTidyKey struct {
|
|
|
|
sessionID string
|
|
|
|
cfg string
|
2020-07-26 16:01:39 -06:00
|
|
|
gomod source.FileIdentity
|
2020-06-19 17:07:57 -06:00
|
|
|
imports string
|
|
|
|
unsavedOverlays string
|
|
|
|
view string
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:10:27 -06:00
|
|
|
type modTidyHandle struct {
|
|
|
|
handle *memoize.Handle
|
|
|
|
}
|
|
|
|
|
|
|
|
type modTidyData struct {
|
|
|
|
memoize.NoCopy
|
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
tidied *source.TidiedModule
|
|
|
|
err error
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
func (mth *modTidyHandle) tidy(ctx context.Context, s source.Snapshot) (*source.TidiedModule, error) {
|
2020-07-16 15:37:12 -06:00
|
|
|
v, err := mth.handle.Get(ctx, s.(*snapshot))
|
2020-07-20 14:56:12 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data := v.(*modTidyData)
|
2020-07-24 11:39:58 -06:00
|
|
|
return data.tidied, data.err
|
2020-07-20 14:56:12 -06:00
|
|
|
}
|
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
func (s *snapshot) ModTidy(ctx context.Context) (*source.TidiedModule, error) {
|
2020-06-19 17:07:57 -06:00
|
|
|
if !s.view.tmpMod {
|
|
|
|
return nil, source.ErrTmpModfileUnsupported
|
|
|
|
}
|
2020-06-16 20:10:27 -06:00
|
|
|
if handle := s.getModTidyHandle(); handle != nil {
|
2020-07-24 11:39:58 -06:00
|
|
|
return handle.tidy(ctx, s)
|
2020-06-19 17:07:57 -06:00
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
modFH, err := s.GetFile(ctx, s.view.modURI)
|
2020-06-19 17:07:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
workspacePkgs, err := s.WorkspacePackages(ctx)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-14 22:19:10 -06:00
|
|
|
importHash, err := hashImports(ctx, workspacePkgs)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
overlayHash := hashUnsavedOverlays(s.files)
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
var (
|
2020-07-26 00:55:12 -06:00
|
|
|
modURI = s.view.modURI
|
|
|
|
cfg = s.config(ctx)
|
2020-06-16 20:10:27 -06:00
|
|
|
)
|
|
|
|
key := modTidyKey{
|
|
|
|
sessionID: s.view.session.id,
|
2020-07-20 23:34:22 -06:00
|
|
|
view: s.view.root.Filename(),
|
2020-07-14 22:19:10 -06:00
|
|
|
imports: importHash,
|
2020-06-16 20:10:27 -06:00
|
|
|
unsavedOverlays: overlayHash,
|
2020-07-26 16:01:39 -06:00
|
|
|
gomod: modFH.FileIdentity(),
|
2020-06-16 20:10:27 -06:00
|
|
|
cfg: hashConfig(cfg),
|
|
|
|
}
|
2020-07-16 15:37:12 -06:00
|
|
|
h := s.view.session.cache.store.Bind(key, func(ctx context.Context, arg memoize.Arg) interface{} {
|
2020-06-16 20:10:27 -06:00
|
|
|
ctx, done := event.Start(ctx, "cache.ModTidyHandle", tag.URI.Of(modURI))
|
|
|
|
defer done()
|
|
|
|
|
2020-07-16 15:37:12 -06:00
|
|
|
snapshot := arg.(*snapshot)
|
2020-07-24 11:39:58 -06:00
|
|
|
pm, err := snapshot.ParseMod(ctx, modFH)
|
|
|
|
if err != nil {
|
|
|
|
return &modTidyData{err: err}
|
|
|
|
}
|
|
|
|
if len(pm.ParseErrors) > 0 {
|
2020-06-19 17:07:57 -06:00
|
|
|
return &modTidyData{
|
2020-07-24 11:39:58 -06:00
|
|
|
tidied: &source.TidiedModule{
|
|
|
|
Parsed: pm,
|
|
|
|
},
|
|
|
|
err: fmt.Errorf("could not parse module to tidy: %v", pm.ParseErrors),
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 11:39:58 -06:00
|
|
|
tmpURI, runner, inv, cleanup, err := snapshot.goCommandInvocation(ctx, true, "mod", []string{"tidy"})
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return &modTidyData{err: err}
|
|
|
|
}
|
|
|
|
// Keep the temporary go.mod file around long enough to parse it.
|
|
|
|
defer cleanup()
|
|
|
|
|
2020-07-24 11:39:58 -06:00
|
|
|
if _, err := runner.Run(ctx, *inv); err != nil {
|
2020-06-16 20:10:27 -06:00
|
|
|
return &modTidyData{err: err}
|
|
|
|
}
|
|
|
|
// Go directly to disk to get the temporary mod file, since it is
|
|
|
|
// always on disk.
|
|
|
|
tempContents, err := ioutil.ReadFile(tmpURI.Filename())
|
|
|
|
if err != nil {
|
|
|
|
return &modTidyData{err: err}
|
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
ideal, err := modfile.Parse(tmpURI.Filename(), tempContents, nil)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
// We do not need to worry about the temporary file's parse errors
|
|
|
|
// since it has been "tidied".
|
|
|
|
return &modTidyData{err: err}
|
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
// Compare the original and tidied go.mod files to compute errors and
|
|
|
|
// suggested fixes.
|
|
|
|
errors, err := modTidyErrors(ctx, snapshot, pm, ideal, workspacePkgs)
|
2020-07-14 22:19:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return &modTidyData{err: err}
|
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
return &modTidyData{
|
2020-07-24 11:39:58 -06:00
|
|
|
tidied: &source.TidiedModule{
|
2020-07-26 00:55:12 -06:00
|
|
|
Errors: errors,
|
2020-07-24 11:39:58 -06:00
|
|
|
Parsed: pm,
|
|
|
|
TidiedContent: tempContents,
|
|
|
|
},
|
2020-06-19 17:07:57 -06:00
|
|
|
}
|
2020-06-16 20:10:27 -06:00
|
|
|
})
|
2020-07-24 11:39:58 -06:00
|
|
|
|
|
|
|
mth := &modTidyHandle{handle: h}
|
2020-06-16 20:10:27 -06:00
|
|
|
s.mu.Lock()
|
2020-07-24 11:39:58 -06:00
|
|
|
s.modTidyHandle = mth
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
return mth.tidy(ctx, s)
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
|
2020-07-14 22:19:10 -06:00
|
|
|
func hashImports(ctx context.Context, wsPackages []source.Package) (string, error) {
|
|
|
|
results := make(map[string]bool)
|
|
|
|
var imports []string
|
|
|
|
for _, pkg := range wsPackages {
|
|
|
|
for _, path := range pkg.Imports() {
|
|
|
|
imp := path.PkgPath()
|
|
|
|
if _, ok := results[imp]; !ok {
|
|
|
|
results[imp] = true
|
|
|
|
imports = append(imports, imp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(imports)
|
|
|
|
hashed := strings.Join(imports, ",")
|
|
|
|
return hashContents([]byte(hashed)), nil
|
|
|
|
}
|
|
|
|
|
2020-07-26 00:55:12 -06:00
|
|
|
// modTidyErrors computes the differences between the original and tidied
|
|
|
|
// go.mod files to produce diagnostic and suggested fixes. Some diagnostics
|
|
|
|
// may appear on the Go files that import packages from missing modules.
|
|
|
|
func modTidyErrors(ctx context.Context, snapshot source.Snapshot, pm *source.ParsedModule, ideal *modfile.File, workspacePkgs []source.Package) (errors []source.Error, err error) {
|
|
|
|
// First, determine which modules are unused and which are missing from the
|
|
|
|
// original go.mod file.
|
|
|
|
var (
|
|
|
|
unused = make(map[string]*modfile.Require, len(pm.File.Require))
|
|
|
|
missing = make(map[string]*modfile.Require, len(ideal.Require))
|
|
|
|
wrongDirectness = make(map[string]*modfile.Require, len(pm.File.Require))
|
|
|
|
)
|
|
|
|
for _, req := range pm.File.Require {
|
|
|
|
unused[req.Mod.Path] = req
|
|
|
|
}
|
|
|
|
for _, req := range ideal.Require {
|
|
|
|
origReq := unused[req.Mod.Path]
|
|
|
|
if origReq == nil {
|
|
|
|
missing[req.Mod.Path] = req
|
2020-06-16 20:10:27 -06:00
|
|
|
continue
|
2020-07-26 00:55:12 -06:00
|
|
|
} else if origReq.Indirect != req.Indirect {
|
|
|
|
wrongDirectness[req.Mod.Path] = origReq
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
delete(unused, req.Mod.Path)
|
|
|
|
}
|
|
|
|
for _, req := range unused {
|
|
|
|
srcErr, err := unusedError(pm.Mapper, req, snapshot.View().Options().ComputeEdits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, srcErr)
|
|
|
|
}
|
|
|
|
for _, req := range wrongDirectness {
|
|
|
|
// Handle dependencies that are incorrectly labeled indirect and
|
|
|
|
// vice versa.
|
|
|
|
srcErr, err := directnessError(pm.Mapper, req, snapshot.View().Options().ComputeEdits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, srcErr)
|
|
|
|
}
|
|
|
|
// Next, compute any diagnostics for modules that are missing from the
|
|
|
|
// go.mod file. The fixes will be for the go.mod file, but the
|
|
|
|
// diagnostics should also appear in both the go.mod file and the import
|
|
|
|
// statements in the Go files in which the dependencies are used.
|
|
|
|
missingModuleFixes := map[*modfile.Require][]source.SuggestedFix{}
|
|
|
|
for _, req := range missing {
|
|
|
|
srcErr, err := missingModuleError(snapshot, pm, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
missingModuleFixes[req] = srcErr.SuggestedFixes
|
|
|
|
errors = append(errors, srcErr)
|
|
|
|
}
|
|
|
|
// Add diagnostics for missing modules anywhere they are imported in the
|
|
|
|
// workspace.
|
|
|
|
for _, pkg := range workspacePkgs {
|
|
|
|
missingImports := map[string]*modfile.Require{}
|
|
|
|
for _, imp := range pkg.Imports() {
|
|
|
|
if req, ok := missing[imp.PkgPath()]; ok {
|
|
|
|
missingImports[imp.PkgPath()] = req
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// If the import is a package of the dependency, then add the
|
|
|
|
// package to the map, this will eliminate the need to do this
|
|
|
|
// prefix package search on each import for each file.
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// import (
|
|
|
|
// "golang.org/x/tools/go/expect"
|
|
|
|
// "golang.org/x/tools/go/packages"
|
|
|
|
// )
|
|
|
|
// They both are related to the same module: "golang.org/x/tools".
|
|
|
|
var match string
|
|
|
|
for _, req := range ideal.Require {
|
|
|
|
if strings.HasPrefix(imp.PkgPath(), req.Mod.Path) && len(req.Mod.Path) > len(match) {
|
|
|
|
match = req.Mod.Path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if req, ok := missing[match]; ok {
|
|
|
|
missingImports[imp.PkgPath()] = req
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
// None of this package's imports are from missing modules.
|
|
|
|
if len(missingImports) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, pgf := range pkg.CompiledGoFiles() {
|
|
|
|
file, m := pgf.File, pgf.Mapper
|
|
|
|
if file == nil || m == nil {
|
|
|
|
continue
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
imports := make(map[string]*ast.ImportSpec)
|
|
|
|
for _, imp := range file.Imports {
|
|
|
|
if imp.Path == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if target, err := strconv.Unquote(imp.Path.Value); err == nil {
|
|
|
|
imports[target] = imp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(imports) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for importPath, req := range missingImports {
|
|
|
|
imp, ok := imports[importPath]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fixes, ok := missingModuleFixes[req]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no missing module fix for %q (%q)", importPath, req.Mod.Path)
|
|
|
|
}
|
|
|
|
srcErr, err := missingModuleForImport(snapshot, m, imp, req, fixes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, srcErr)
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors, nil
|
|
|
|
}
|
|
|
|
|
2020-07-26 00:55:12 -06:00
|
|
|
// unusedError returns a source.Error for an unused require.
|
|
|
|
func unusedError(m *protocol.ColumnMapper, req *modfile.Require, computeEdits diff.ComputeEdits) (source.Error, error) {
|
|
|
|
rng, err := rangeFromPositions(m, req.Syntax.Start, req.Syntax.End)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
edits, err := dropDependency(req, m, computeEdits)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
return source.Error{
|
|
|
|
Category: source.GoModTidy,
|
|
|
|
Message: fmt.Sprintf("%s is not used in this module", req.Mod.Path),
|
|
|
|
Range: rng,
|
|
|
|
URI: m.URI,
|
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
|
|
|
Title: fmt.Sprintf("Remove dependency: %s", req.Mod.Path),
|
|
|
|
Edits: map[span.URI][]protocol.TextEdit{
|
|
|
|
m.URI: edits,
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}, nil
|
|
|
|
}
|
2020-06-19 17:07:57 -06:00
|
|
|
|
2020-07-26 00:55:12 -06:00
|
|
|
// directnessError extracts errors when a dependency is labeled indirect when
|
|
|
|
// it should be direct and vice versa.
|
|
|
|
func directnessError(m *protocol.ColumnMapper, req *modfile.Require, computeEdits diff.ComputeEdits) (source.Error, error) {
|
2020-07-16 22:40:01 -06:00
|
|
|
rng, err := rangeFromPositions(m, req.Syntax.Start, req.Syntax.End)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
direction := "indirect"
|
2020-06-16 20:10:27 -06:00
|
|
|
if req.Indirect {
|
2020-07-26 00:55:12 -06:00
|
|
|
direction = "direct"
|
|
|
|
|
2020-06-16 20:10:27 -06:00
|
|
|
// If the dependency should be direct, just highlight the // indirect.
|
|
|
|
if comments := req.Syntax.Comment(); comments != nil && len(comments.Suffix) > 0 {
|
|
|
|
end := comments.Suffix[0].Start
|
|
|
|
end.LineRune += len(comments.Suffix[0].Token)
|
|
|
|
end.Byte += len([]byte(comments.Suffix[0].Token))
|
2020-07-16 22:40:01 -06:00
|
|
|
rng, err = rangeFromPositions(m, comments.Suffix[0].Start, end)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the dependency should be indirect, add the // indirect.
|
2020-07-26 00:55:12 -06:00
|
|
|
edits, err := switchDirectness(req, m, computeEdits)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
return source.Error{
|
2020-07-26 00:55:12 -06:00
|
|
|
Message: fmt.Sprintf("%s should be %s", req.Mod.Path, direction),
|
2020-06-16 20:10:27 -06:00
|
|
|
Range: rng,
|
2020-07-16 22:40:01 -06:00
|
|
|
URI: m.URI,
|
2020-07-26 00:55:12 -06:00
|
|
|
Category: source.GoModTidy,
|
2020-06-16 20:10:27 -06:00
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
2020-07-26 00:55:12 -06:00
|
|
|
Title: fmt.Sprintf("Change %s to %s", req.Mod.Path, direction),
|
2020-06-19 17:07:57 -06:00
|
|
|
Edits: map[span.URI][]protocol.TextEdit{
|
2020-07-16 22:40:01 -06:00
|
|
|
m.URI: edits,
|
2020-06-19 17:07:57 -06:00
|
|
|
},
|
2020-06-16 20:10:27 -06:00
|
|
|
}},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-26 00:55:12 -06:00
|
|
|
func missingModuleError(snapshot source.Snapshot, pm *source.ParsedModule, req *modfile.Require) (source.Error, error) {
|
|
|
|
start, end := pm.File.Module.Syntax.Span()
|
|
|
|
rng, err := rangeFromPositions(pm.Mapper, start, end)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
edits, err := addRequireFix(pm.Mapper, req, snapshot.View().Options().ComputeEdits)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
fix := &source.SuggestedFix{
|
|
|
|
Title: "Add %s to your go.mod file",
|
|
|
|
Edits: map[span.URI][]protocol.TextEdit{
|
|
|
|
pm.Mapper.URI: edits,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return source.Error{
|
|
|
|
URI: pm.Mapper.URI,
|
|
|
|
Range: rng,
|
|
|
|
Message: fmt.Sprintf("%s is not in your go.mod file", req.Mod.Path),
|
|
|
|
Category: source.GoModTidy,
|
|
|
|
Kind: source.ModTidyError,
|
|
|
|
SuggestedFixes: []source.SuggestedFix{*fix},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dropDependency returns the edits to remove the given require from the go.mod
|
|
|
|
// file.
|
|
|
|
func dropDependency(req *modfile.Require, m *protocol.ColumnMapper, computeEdits diff.ComputeEdits) ([]protocol.TextEdit, error) {
|
2020-07-09 00:02:35 -06:00
|
|
|
// We need a private copy of the parsed go.mod file, since we're going to
|
|
|
|
// modify it.
|
|
|
|
copied, err := modfile.Parse("", m.Content, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := copied.DropRequire(req.Mod.Path); err != nil {
|
2020-06-16 20:10:27 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-09 00:02:35 -06:00
|
|
|
copied.Cleanup()
|
|
|
|
newContent, err := copied.Format()
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Calculate the edits to be made due to the change.
|
2020-07-26 00:55:12 -06:00
|
|
|
diff := computeEdits(m.URI, string(m.Content), string(newContent))
|
|
|
|
return source.ToProtocolEdits(m, diff)
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
|
2020-07-26 00:55:12 -06:00
|
|
|
// switchDirectness gets the edits needed to change an indirect dependency to
|
|
|
|
// direct and vice versa.
|
|
|
|
func switchDirectness(req *modfile.Require, m *protocol.ColumnMapper, computeEdits diff.ComputeEdits) ([]protocol.TextEdit, error) {
|
2020-07-09 00:02:35 -06:00
|
|
|
// We need a private copy of the parsed go.mod file, since we're going to
|
|
|
|
// modify it.
|
|
|
|
copied, err := modfile.Parse("", m.Content, nil)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-09 00:02:35 -06:00
|
|
|
// Change the directness in the matching require statement. To avoid
|
|
|
|
// reordering the require statements, rewrite all of them.
|
|
|
|
var requires []*modfile.Require
|
|
|
|
for _, r := range copied.Require {
|
|
|
|
if r.Mod.Path == req.Mod.Path {
|
|
|
|
requires = append(requires, &modfile.Require{
|
|
|
|
Mod: r.Mod,
|
|
|
|
Syntax: r.Syntax,
|
2020-07-26 00:55:12 -06:00
|
|
|
Indirect: !r.Indirect,
|
2020-07-09 00:02:35 -06:00
|
|
|
})
|
|
|
|
continue
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
2020-07-09 00:02:35 -06:00
|
|
|
requires = append(requires, r)
|
|
|
|
}
|
|
|
|
copied.SetRequire(requires)
|
|
|
|
newContent, err := copied.Format()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
// Calculate the edits to be made due to the change.
|
2020-07-26 00:55:12 -06:00
|
|
|
diff := computeEdits(m.URI, string(m.Content), string(newContent))
|
|
|
|
return source.ToProtocolEdits(m, diff)
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
|
|
|
|
2020-07-26 00:55:12 -06:00
|
|
|
// missingModuleForImport creates an error for a given import path that comes
|
|
|
|
// from a missing module.
|
|
|
|
func missingModuleForImport(snapshot source.Snapshot, m *protocol.ColumnMapper, imp *ast.ImportSpec, req *modfile.Require, fixes []source.SuggestedFix) (source.Error, error) {
|
|
|
|
if req.Syntax == nil {
|
|
|
|
return source.Error{}, fmt.Errorf("no syntax for %v", req)
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
spn, err := span.NewRange(snapshot.FileSet(), imp.Path.Pos(), imp.Path.End()).Span()
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
2020-07-26 00:55:12 -06:00
|
|
|
return source.Error{}, err
|
2020-06-16 20:10:27 -06:00
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
rng, err := m.Range(spn)
|
2020-06-16 20:10:27 -06:00
|
|
|
if err != nil {
|
2020-07-26 00:55:12 -06:00
|
|
|
return source.Error{}, err
|
2020-07-14 22:19:10 -06:00
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
return source.Error{
|
|
|
|
Category: source.GoModTidy,
|
|
|
|
URI: m.URI,
|
|
|
|
Range: rng,
|
|
|
|
Message: fmt.Sprintf("%s is not in your go.mod file", req.Mod.Path),
|
|
|
|
Kind: source.ModTidyError,
|
|
|
|
SuggestedFixes: fixes,
|
|
|
|
}, nil
|
2020-07-14 22:19:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// addRequireFix creates edits for adding a given require to a go.mod file.
|
2020-07-26 00:55:12 -06:00
|
|
|
func addRequireFix(m *protocol.ColumnMapper, req *modfile.Require, computeEdits diff.ComputeEdits) ([]protocol.TextEdit, error) {
|
2020-07-14 22:19:10 -06:00
|
|
|
// We need a private copy of the parsed go.mod file, since we're going to
|
|
|
|
// modify it.
|
|
|
|
copied, err := modfile.Parse("", m.Content, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Calculate the quick fix edits that need to be made to the go.mod file.
|
|
|
|
if err := copied.AddRequire(req.Mod.Path, req.Mod.Version); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copied.SortBlocks()
|
|
|
|
newContents, err := copied.Format()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Calculate the edits to be made due to the change.
|
2020-07-26 00:55:12 -06:00
|
|
|
diff := computeEdits(m.URI, string(m.Content), string(newContents))
|
|
|
|
return source.ToProtocolEdits(m, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rangeFromPositions(m *protocol.ColumnMapper, s, e modfile.Position) (protocol.Range, error) {
|
|
|
|
toPoint := func(offset int) (span.Point, error) {
|
|
|
|
l, c, err := m.Converter.ToPosition(offset)
|
|
|
|
if err != nil {
|
|
|
|
return span.Point{}, err
|
|
|
|
}
|
|
|
|
return span.NewPoint(l, c, offset), nil
|
|
|
|
}
|
|
|
|
start, err := toPoint(s.Byte)
|
2020-07-14 22:19:10 -06:00
|
|
|
if err != nil {
|
2020-07-26 00:55:12 -06:00
|
|
|
return protocol.Range{}, err
|
2020-07-14 22:19:10 -06:00
|
|
|
}
|
2020-07-26 00:55:12 -06:00
|
|
|
end, err := toPoint(e.Byte)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
return m.Range(span.New(m.URI, start, end))
|
2020-07-14 22:19:10 -06:00
|
|
|
}
|