2019-12-17 14:13:33 -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 mod provides core features related to go.mod file
|
|
|
|
// handling for use by Go editors and tools.
|
|
|
|
package mod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-01-24 08:14:25 -07:00
|
|
|
"golang.org/x/mod/modfile"
|
2020-03-10 21:09:39 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-12-17 14:13:33 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2020-03-07 19:28:21 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2019-12-17 14:13:33 -07:00
|
|
|
)
|
|
|
|
|
2020-03-31 21:53:42 -06:00
|
|
|
func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.FileIdentity][]*source.Diagnostic, map[string]*modfile.Require, error) {
|
2019-12-17 14:13:33 -07:00
|
|
|
// TODO: We will want to support diagnostics for go.mod files even when the -modfile flag is turned off.
|
2020-01-23 14:54:21 -07:00
|
|
|
realURI, tempURI := snapshot.View().ModFiles()
|
|
|
|
|
2019-12-17 14:13:33 -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-24 08:14:25 -07:00
|
|
|
return nil, nil, nil
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-03-10 21:52:14 -06:00
|
|
|
ctx, done := event.StartSpan(ctx, "mod.Diagnostics", tag.URI.Of(realURI))
|
2019-12-17 14:13:33 -07:00
|
|
|
defer done()
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
realfh, err := snapshot.GetFile(realURI)
|
|
|
|
if err != nil {
|
2020-01-24 08:14:25 -07:00
|
|
|
return nil, nil, err
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
2020-02-06 11:59:27 -07:00
|
|
|
mth, err := snapshot.ModTidyHandle(ctx, realfh)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
_, _, missingDeps, parseErrors, err := mth.Tidy(ctx)
|
2019-12-17 14:13:33 -07:00
|
|
|
if err != nil {
|
2020-01-24 08:14:25 -07:00
|
|
|
return nil, nil, err
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-03-31 21:53:42 -06:00
|
|
|
reports := map[source.FileIdentity][]*source.Diagnostic{
|
2020-03-27 10:52:40 -06:00
|
|
|
realfh.Identity(): {},
|
2020-01-10 11:20:44 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
for _, e := range parseErrors {
|
2020-03-31 21:53:42 -06:00
|
|
|
diag := &source.Diagnostic{
|
2020-01-16 12:32:09 -07:00
|
|
|
Message: e.Message,
|
|
|
|
Range: e.Range,
|
|
|
|
SuggestedFixes: e.SuggestedFixes,
|
|
|
|
Source: e.Category,
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
if e.Category == "syntax" {
|
|
|
|
diag.Severity = protocol.SeverityError
|
|
|
|
} else {
|
|
|
|
diag.Severity = protocol.SeverityWarning
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
reports[realfh.Identity()] = append(reports[realfh.Identity()], diag)
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-24 08:14:25 -07:00
|
|
|
return reports, missingDeps, nil
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
|
|
|
|
2020-04-01 19:31:43 -06:00
|
|
|
func SuggestedFixes(ctx context.Context, snapshot source.Snapshot, realfh source.FileHandle, diags []protocol.Diagnostic) ([]protocol.CodeAction, error) {
|
2020-02-06 11:59:27 -07:00
|
|
|
mth, err := snapshot.ModTidyHandle(ctx, realfh)
|
|
|
|
if err != nil {
|
2020-04-01 19:31:43 -06:00
|
|
|
return nil, err
|
2020-02-06 11:59:27 -07:00
|
|
|
}
|
|
|
|
_, _, _, parseErrors, err := mth.Tidy(ctx)
|
2020-01-16 12:32:09 -07:00
|
|
|
if err != nil {
|
2020-04-01 19:31:43 -06:00
|
|
|
return nil, err
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
errorsMap := make(map[string][]source.Error)
|
|
|
|
for _, e := range parseErrors {
|
|
|
|
if errorsMap[e.Message] == nil {
|
|
|
|
errorsMap[e.Message] = []source.Error{}
|
|
|
|
}
|
|
|
|
errorsMap[e.Message] = append(errorsMap[e.Message], e)
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
var actions []protocol.CodeAction
|
|
|
|
for _, diag := range diags {
|
2020-01-16 12:32:09 -07:00
|
|
|
for _, e := range errorsMap[diag.Message] {
|
|
|
|
if !sameDiagnostic(diag, e) {
|
2019-12-17 14:13:33 -07:00
|
|
|
continue
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
for _, fix := range e.SuggestedFixes {
|
|
|
|
action := protocol.CodeAction{
|
|
|
|
Title: fix.Title,
|
|
|
|
Kind: protocol.QuickFix,
|
|
|
|
Diagnostics: []protocol.Diagnostic{diag},
|
|
|
|
Edit: protocol.WorkspaceEdit{},
|
|
|
|
}
|
|
|
|
for uri, edits := range fix.Edits {
|
|
|
|
fh, err := snapshot.GetFile(uri)
|
|
|
|
if err != nil {
|
2020-04-01 19:31:43 -06:00
|
|
|
return nil, err
|
2020-01-16 12:32:09 -07:00
|
|
|
}
|
|
|
|
action.Edit.DocumentChanges = append(action.Edit.DocumentChanges, protocol.TextDocumentEdit{
|
2019-12-17 14:13:33 -07:00
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
|
|
Version: fh.Identity().Version,
|
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
2020-02-12 14:36:46 -07:00
|
|
|
URI: protocol.URIFromSpanURI(fh.Identity().URI),
|
2019-12-17 14:13:33 -07:00
|
|
|
},
|
|
|
|
},
|
2020-01-16 12:32:09 -07:00
|
|
|
Edits: edits,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
actions = append(actions, action)
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-04-01 19:31:43 -06:00
|
|
|
return actions, nil
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
|
2020-02-26 15:07:02 -07:00
|
|
|
func SuggestedGoFixes(ctx context.Context, snapshot source.Snapshot) (map[string]protocol.TextDocumentEdit, error) {
|
|
|
|
// TODO(rstambler): Support diagnostics for go.mod files even when the
|
|
|
|
// -modfile flag is turned off.
|
2020-01-22 12:23:12 -07:00
|
|
|
realURI, tempURI := snapshot.View().ModFiles()
|
|
|
|
if realURI == "" || tempURI == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 21:52:14 -06:00
|
|
|
ctx, done := event.StartSpan(ctx, "mod.SuggestedGoFixes", tag.URI.Of(realURI))
|
2020-01-22 12:23:12 -07:00
|
|
|
defer done()
|
|
|
|
|
|
|
|
realfh, err := snapshot.GetFile(realURI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 11:59:27 -07:00
|
|
|
mth, err := snapshot.ModTidyHandle(ctx, realfh)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
realFile, realMapper, missingDeps, _, err := mth.Tidy(ctx)
|
2020-01-22 12:23:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
if len(missingDeps) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-01-22 12:23:12 -07:00
|
|
|
// Get the contents of the go.mod file before we make any changes.
|
|
|
|
oldContents, _, err := realfh.Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
textDocumentEdits := make(map[string]protocol.TextDocumentEdit)
|
|
|
|
for dep, req := range missingDeps {
|
2020-01-22 12:23:12 -07:00
|
|
|
// Calculate the quick fix edits that need to be made to the go.mod file.
|
|
|
|
if err := realFile.AddRequire(req.Mod.Path, req.Mod.Version); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
realFile.Cleanup()
|
|
|
|
newContents, err := realFile.Format()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Reset the *modfile.File back to before we added the dependency.
|
|
|
|
if err := realFile.DropRequire(req.Mod.Path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Calculate the edits to be made due to the change.
|
|
|
|
diff := snapshot.View().Options().ComputeEdits(realfh.Identity().URI, string(oldContents), string(newContents))
|
|
|
|
edits, err := source.ToProtocolEdits(realMapper, diff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
textDocumentEdits[dep] = protocol.TextDocumentEdit{
|
|
|
|
TextDocument: protocol.VersionedTextDocumentIdentifier{
|
|
|
|
Version: realfh.Identity().Version,
|
|
|
|
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
|
|
|
|
URI: protocol.URIFromSpanURI(realfh.Identity().URI),
|
2020-01-22 12:23:12 -07:00
|
|
|
},
|
|
|
|
},
|
2020-02-26 15:07:02 -07:00
|
|
|
Edits: edits,
|
2020-01-22 12:23:12 -07:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
return textDocumentEdits, nil
|
2020-01-22 12:23:12 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
func sameDiagnostic(d protocol.Diagnostic, e source.Error) bool {
|
|
|
|
return d.Message == e.Message && protocol.CompareRange(d.Range, e.Range) == 0 && d.Source == e.Category
|
|
|
|
}
|