2019-12-11 11:44:39 -07:00
|
|
|
// Copyright 2019 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"
|
2020-01-16 12:32:09 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-12-17 14:13:33 -07:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-12-11 11:44:39 -07:00
|
|
|
|
|
|
|
"golang.org/x/mod/modfile"
|
2020-01-16 12:32:09 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-12-17 14:13:33 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-12-11 11:44:39 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
|
|
|
"golang.org/x/tools/internal/memoize"
|
2020-01-14 09:10:54 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-12-17 14:13:33 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-12-11 11:44:39 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
const ModTidyError = "go mod tidy"
|
|
|
|
const SyntaxError = "syntax"
|
|
|
|
|
|
|
|
type parseModKey struct {
|
2020-02-03 18:18:59 -07:00
|
|
|
view string
|
|
|
|
snapshotID uint64
|
|
|
|
cfg string
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
type modTidyHandle struct {
|
2019-12-11 11:44:39 -07:00
|
|
|
handle *memoize.Handle
|
|
|
|
file source.FileHandle
|
2020-01-16 12:32:09 -07:00
|
|
|
cfg *packages.Config
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 14:53:48 -07:00
|
|
|
type modTidyData struct {
|
2019-12-11 11:44:39 -07:00
|
|
|
memoize.NoCopy
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// origfh is the file handle for the original go.mod file.
|
|
|
|
origfh source.FileHandle
|
|
|
|
|
|
|
|
// origParsedFile contains the parsed contents that are used to diff with
|
|
|
|
// the ideal contents.
|
|
|
|
origParsedFile *modfile.File
|
|
|
|
|
|
|
|
// origMapper is the column mapper for the original go.mod file.
|
|
|
|
origMapper *protocol.ColumnMapper
|
|
|
|
|
|
|
|
// idealParsedFile contains the parsed contents for the go.mod file
|
|
|
|
// after it has been "tidied".
|
|
|
|
idealParsedFile *modfile.File
|
2020-01-14 14:53:48 -07:00
|
|
|
|
|
|
|
// unusedDeps is the map containing the dependencies that are left after
|
|
|
|
// removing the ones that are identical in the original and ideal go.mods.
|
|
|
|
unusedDeps map[string]*modfile.Require
|
|
|
|
|
|
|
|
// missingDeps is the map containing the dependencies that are left after
|
|
|
|
// removing the ones that are identical in the original and ideal go.mods.
|
|
|
|
missingDeps map[string]*modfile.Require
|
|
|
|
|
|
|
|
// parseErrors are the errors that arise when we diff between a user's go.mod
|
|
|
|
// and the "tidied" go.mod.
|
|
|
|
parseErrors []source.Error
|
|
|
|
|
|
|
|
// err is any error that occurs while we are calculating the parseErrors.
|
|
|
|
err error
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mth *modTidyHandle) String() string {
|
|
|
|
return mth.File().Identity().URI.Filename()
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
func (mth *modTidyHandle) File() source.FileHandle {
|
|
|
|
return mth.file
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 14:53:48 -07:00
|
|
|
func (mth *modTidyHandle) Tidy(ctx context.Context) (*modfile.File, *protocol.ColumnMapper, map[string]*modfile.Require, []source.Error, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
v := mth.handle.Get(ctx)
|
2020-01-16 12:32:09 -07:00
|
|
|
if v == nil {
|
2020-01-14 14:53:48 -07:00
|
|
|
return nil, nil, nil, nil, errors.Errorf("no parsed file for %s", mth.File().Identity().URI)
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-01-14 14:53:48 -07:00
|
|
|
data := v.(*modTidyData)
|
|
|
|
return data.origParsedFile, data.origMapper, data.missingDeps, data.parseErrors, data.err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
func (s *snapshot) ModTidyHandle(ctx context.Context, realfh source.FileHandle) source.ModTidyHandle {
|
2020-01-23 14:54:21 -07:00
|
|
|
realURI, tempURI := s.view.ModFiles()
|
2020-01-16 12:32:09 -07:00
|
|
|
cfg := s.View().Config(ctx)
|
2020-01-16 12:32:09 -07:00
|
|
|
options := s.View().Options()
|
2020-01-16 12:32:09 -07:00
|
|
|
folder := s.View().Folder().Filename()
|
|
|
|
|
|
|
|
key := parseModKey{
|
2020-02-03 18:18:59 -07:00
|
|
|
view: folder,
|
|
|
|
snapshotID: s.ID(),
|
|
|
|
cfg: hashConfig(cfg),
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
h := s.view.session.cache.store.Bind(key, func(ctx context.Context) interface{} {
|
2020-01-14 14:53:48 -07:00
|
|
|
data := &modTidyData{}
|
2020-01-23 14:54:21 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// Check the case when the tempModfile flag is turned off.
|
2020-01-16 12:32:09 -07:00
|
|
|
if realURI == "" || tempURI == "" {
|
2020-01-16 12:32:09 -07:00
|
|
|
return data
|
|
|
|
}
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
ctx, done := trace.StartSpan(ctx, "cache.ModTidyHandle", telemetry.File.Of(realURI))
|
|
|
|
defer done()
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// Copy the real go.mod file content into the temp go.mod file.
|
|
|
|
realContents, _, err := realfh.Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
data.err = err
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(tempURI.Filename(), realContents, os.ModePerm); err != nil {
|
|
|
|
data.err = err
|
|
|
|
return data
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// We want to run "go mod tidy" to be able to diff between the real and the temp files.
|
|
|
|
args := append([]string{"mod", "tidy"}, cfg.BuildFlags...)
|
|
|
|
if _, err := source.InvokeGo(ctx, folder, cfg.Env, args...); err != nil {
|
|
|
|
// Ignore parse errors here. They'll be handled below.
|
|
|
|
if !strings.Contains(err.Error(), "errors parsing go.mod") {
|
|
|
|
data.err = err
|
|
|
|
return data
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
realMapper := &protocol.ColumnMapper{
|
|
|
|
URI: realURI,
|
|
|
|
Converter: span.NewContentConverter(realURI.Filename(), realContents),
|
|
|
|
Content: realContents,
|
|
|
|
}
|
|
|
|
origParsedFile, err := modfile.Parse(realURI.Filename(), realContents, nil)
|
|
|
|
if err != nil {
|
|
|
|
if parseErr, err := extractModParseErrors(ctx, realURI, realMapper, err, realContents); err == nil {
|
|
|
|
data.parseErrors = []source.Error{parseErr}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
data.err = err
|
|
|
|
return data
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// Go directly to disk to get the temporary mod file, since it is always on disk.
|
|
|
|
tempContents, err := ioutil.ReadFile(tempURI.Filename())
|
|
|
|
if err != nil {
|
|
|
|
data.err = err
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
idealParsedFile, err := modfile.Parse(tempURI.Filename(), tempContents, nil)
|
|
|
|
if err != nil {
|
|
|
|
// We do not need to worry about the temporary file's parse errors since it has been "tidied".
|
|
|
|
data.err = err
|
|
|
|
return data
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-01-14 14:53:48 -07:00
|
|
|
data = &modTidyData{
|
2020-01-16 12:32:09 -07:00
|
|
|
origfh: realfh,
|
|
|
|
origParsedFile: origParsedFile,
|
|
|
|
origMapper: realMapper,
|
|
|
|
idealParsedFile: idealParsedFile,
|
2020-01-14 14:53:48 -07:00
|
|
|
unusedDeps: make(map[string]*modfile.Require, len(origParsedFile.Require)),
|
|
|
|
missingDeps: make(map[string]*modfile.Require, len(idealParsedFile.Require)),
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-01-14 14:53:48 -07:00
|
|
|
// Get the dependencies that are different between the original and ideal mod files.
|
|
|
|
for _, req := range origParsedFile.Require {
|
|
|
|
data.unusedDeps[req.Mod.Path] = req
|
|
|
|
}
|
|
|
|
for _, req := range idealParsedFile.Require {
|
|
|
|
origDep := data.unusedDeps[req.Mod.Path]
|
|
|
|
if origDep != nil && origDep.Indirect == req.Indirect {
|
|
|
|
delete(data.unusedDeps, req.Mod.Path)
|
|
|
|
} else {
|
|
|
|
data.missingDeps[req.Mod.Path] = req
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.parseErrors, data.err = modRequireErrors(ctx, options, data)
|
|
|
|
|
|
|
|
for _, req := range data.missingDeps {
|
|
|
|
if data.unusedDeps[req.Mod.Path] != nil {
|
|
|
|
delete(data.missingDeps, req.Mod.Path)
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
})
|
|
|
|
return &modTidyHandle{
|
|
|
|
handle: h,
|
|
|
|
file: realfh,
|
|
|
|
cfg: cfg,
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// extractModParseErrors processes the raw errors returned by modfile.Parse,
|
|
|
|
// extracting the filenames and line numbers that correspond to the errors.
|
|
|
|
func extractModParseErrors(ctx context.Context, uri span.URI, m *protocol.ColumnMapper, parseErr error, content []byte) (source.Error, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
re := regexp.MustCompile(`.*:([\d]+): (.+)`)
|
2020-01-16 12:32:09 -07:00
|
|
|
matches := re.FindStringSubmatch(strings.TrimSpace(parseErr.Error()))
|
2020-01-16 12:32:09 -07:00
|
|
|
if len(matches) < 3 {
|
2020-01-16 12:32:09 -07:00
|
|
|
log.Error(ctx, "could not parse golang/x/mod error message", parseErr)
|
|
|
|
return source.Error{}, parseErr
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
line, err := strconv.Atoi(matches[1])
|
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{}, parseErr
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
lines := strings.Split(string(content), "\n")
|
2020-01-16 12:32:09 -07:00
|
|
|
if len(lines) <= line {
|
|
|
|
return source.Error{}, errors.Errorf("could not parse goland/x/mod error message, line number out of range")
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// The error returned from the modfile package only returns a line number,
|
|
|
|
// so we assume that the diagnostic should be for the entire line.
|
2020-01-16 12:32:09 -07:00
|
|
|
endOfLine := len(lines[line-1])
|
|
|
|
sOffset, err := m.Converter.ToOffset(line, 0)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
eOffset, err := m.Converter.ToOffset(line, endOfLine)
|
2019-12-11 11:44:39 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{}, err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
spn := span.New(uri, span.NewPoint(line, 0, sOffset), span.NewPoint(line, endOfLine, eOffset))
|
|
|
|
rng, err := m.Range(spn)
|
2019-12-11 11:44:39 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
return source.Error{
|
|
|
|
Category: SyntaxError,
|
|
|
|
Message: matches[2],
|
|
|
|
Range: rng,
|
|
|
|
URI: uri,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// modRequireErrors extracts the errors that occur on the require directives.
|
|
|
|
// It checks for directness issues and unused dependencies.
|
2020-01-14 14:53:48 -07:00
|
|
|
func modRequireErrors(ctx context.Context, options source.Options, modData *modTidyData) ([]source.Error, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
var errors []source.Error
|
2020-01-14 14:53:48 -07:00
|
|
|
for dep, req := range modData.unusedDeps {
|
2020-01-16 12:32:09 -07:00
|
|
|
if req.Syntax == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Handle dependencies that are incorrectly labeled indirect and vice versa.
|
2020-01-14 14:53:48 -07:00
|
|
|
if modData.missingDeps[dep] != nil && req.Indirect != modData.missingDeps[dep].Indirect {
|
2020-01-16 12:32:09 -07:00
|
|
|
directErr, err := modDirectnessErrors(ctx, options, modData, req)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, directErr)
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// Handle unused dependencies.
|
2020-01-14 14:53:48 -07:00
|
|
|
if modData.missingDeps[dep] == nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
rng, err := rangeFromPositions(modData.origfh.Identity().URI, modData.origMapper, req.Syntax.Start, req.Syntax.End)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
edits, err := dropDependencyEdits(ctx, options, modData, req)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
errors = append(errors, source.Error{
|
|
|
|
Category: ModTidyError,
|
|
|
|
Message: fmt.Sprintf("%s is not used in this module.", dep),
|
|
|
|
Range: rng,
|
2020-01-16 12:32:09 -07:00
|
|
|
URI: modData.origfh.Identity().URI,
|
2020-01-14 14:53:48 -07:00
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
|
|
|
Title: fmt.Sprintf("Remove dependency: %s", dep),
|
|
|
|
Edits: map[span.URI][]protocol.TextEdit{modData.origfh.Identity().URI: edits},
|
|
|
|
}},
|
2020-01-16 12:32:09 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// modDirectnessErrors extracts errors when a dependency is labeled indirect when it should be direct and vice versa.
|
2020-01-14 14:53:48 -07:00
|
|
|
func modDirectnessErrors(ctx context.Context, options source.Options, modData *modTidyData, req *modfile.Require) (source.Error, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
rng, err := rangeFromPositions(modData.origfh.Identity().URI, modData.origMapper, req.Syntax.Start, req.Syntax.End)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
|
|
|
if req.Indirect {
|
|
|
|
// 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-01-16 12:32:09 -07:00
|
|
|
rng, err = rangeFromPositions(modData.origfh.Identity().URI, modData.origMapper, comments.Suffix[0].Start, end)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
edits, err := changeDirectnessEdits(ctx, options, modData, req, false)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{
|
|
|
|
Category: ModTidyError,
|
|
|
|
Message: fmt.Sprintf("%s should be a direct dependency.", req.Mod.Path),
|
|
|
|
Range: rng,
|
2020-01-16 12:32:09 -07:00
|
|
|
URI: modData.origfh.Identity().URI,
|
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
|
|
|
Title: fmt.Sprintf("Make %s direct", req.Mod.Path),
|
|
|
|
Edits: map[span.URI][]protocol.TextEdit{modData.origfh.Identity().URI: edits},
|
|
|
|
}},
|
2020-01-16 12:32:09 -07:00
|
|
|
}, nil
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// If the dependency should be indirect, add the // indirect.
|
|
|
|
edits, err := changeDirectnessEdits(ctx, options, modData, req, true)
|
|
|
|
if err != nil {
|
|
|
|
return source.Error{}, err
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return source.Error{
|
|
|
|
Category: ModTidyError,
|
|
|
|
Message: fmt.Sprintf("%s should be an indirect dependency.", req.Mod.Path),
|
|
|
|
Range: rng,
|
2020-01-16 12:32:09 -07:00
|
|
|
URI: modData.origfh.Identity().URI,
|
|
|
|
SuggestedFixes: []source.SuggestedFix{{
|
|
|
|
Title: fmt.Sprintf("Make %s indirect", req.Mod.Path),
|
|
|
|
Edits: map[span.URI][]protocol.TextEdit{modData.origfh.Identity().URI: edits},
|
|
|
|
}},
|
2020-01-16 12:32:09 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
// dropDependencyEdits gets the edits needed to remove the dependency from the go.mod file.
|
|
|
|
// As an example, this function will codify the edits needed to convert the before go.mod file to the after.
|
|
|
|
// Before:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
|
|
|
//
|
|
|
|
// require golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee
|
|
|
|
// After:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
2020-01-14 14:53:48 -07:00
|
|
|
func dropDependencyEdits(ctx context.Context, options source.Options, modData *modTidyData, req *modfile.Require) ([]protocol.TextEdit, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
if err := modData.origParsedFile.DropRequire(req.Mod.Path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
modData.origParsedFile.Cleanup()
|
|
|
|
newContents, err := modData.origParsedFile.Format()
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return nil, err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
// Reset the *modfile.File back to before we dropped the dependency.
|
|
|
|
modData.origParsedFile.AddNewRequire(req.Mod.Path, req.Mod.Version, req.Indirect)
|
|
|
|
// Calculate the edits to be made due to the change.
|
|
|
|
diff := options.ComputeEdits(modData.origfh.Identity().URI, string(modData.origMapper.Content), string(newContents))
|
|
|
|
edits, err := source.ToProtocolEdits(modData.origMapper, diff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return edits, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeDirectnessEdits gets the edits needed to change an indirect dependency to direct and vice versa.
|
|
|
|
// As an example, this function will codify the edits needed to convert the before go.mod file to the after.
|
|
|
|
// Before:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
|
|
|
//
|
|
|
|
// require golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee
|
|
|
|
// After:
|
|
|
|
// module t
|
|
|
|
//
|
|
|
|
// go 1.11
|
|
|
|
//
|
|
|
|
// require golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee // indirect
|
2020-01-14 14:53:48 -07:00
|
|
|
func changeDirectnessEdits(ctx context.Context, options source.Options, modData *modTidyData, req *modfile.Require, indirect bool) ([]protocol.TextEdit, error) {
|
2020-01-16 12:32:09 -07:00
|
|
|
var newReq []*modfile.Require
|
|
|
|
prevIndirect := false
|
|
|
|
// Change the directness in the matching require statement.
|
|
|
|
for _, r := range modData.origParsedFile.Require {
|
|
|
|
if req.Mod.Path == r.Mod.Path {
|
|
|
|
prevIndirect = req.Indirect
|
|
|
|
req.Indirect = indirect
|
|
|
|
}
|
|
|
|
newReq = append(newReq, r)
|
|
|
|
}
|
|
|
|
modData.origParsedFile.SetRequire(newReq)
|
|
|
|
modData.origParsedFile.Cleanup()
|
|
|
|
newContents, err := modData.origParsedFile.Format()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Change the dependency back to the way it was before we got the newContents.
|
|
|
|
for _, r := range modData.origParsedFile.Require {
|
|
|
|
if req.Mod.Path == r.Mod.Path {
|
|
|
|
req.Indirect = prevIndirect
|
|
|
|
}
|
|
|
|
newReq = append(newReq, r)
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
modData.origParsedFile.SetRequire(newReq)
|
|
|
|
// Calculate the edits to be made due to the change.
|
|
|
|
diff := options.ComputeEdits(modData.origfh.Identity().URI, string(modData.origMapper.Content), string(newContents))
|
|
|
|
edits, err := source.ToProtocolEdits(modData.origMapper, diff)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-01-16 12:32:09 -07:00
|
|
|
return nil, err
|
2020-01-14 09:10:54 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return edits, nil
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
func rangeFromPositions(uri span.URI, m *protocol.ColumnMapper, s, e modfile.Position) (protocol.Range, error) {
|
|
|
|
line, col, err := m.Converter.ToPosition(s.Byte)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
start := span.NewPoint(line, col, s.Byte)
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
line, col, err = m.Converter.ToPosition(e.Byte)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
end := span.NewPoint(line, col, e.Byte)
|
2019-12-11 11:44:39 -07:00
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
spn := span.New(uri, start, end)
|
|
|
|
rng, err := m.Range(spn)
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
return rng, nil
|
2019-12-11 11:44:39 -07:00
|
|
|
}
|