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"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2020-01-16 12:32:09 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-12-17 14:13:33 -07:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
|
|
|
)
|
|
|
|
|
2020-01-10 11:20:44 -07:00
|
|
|
func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.FileIdentity][]source.Diagnostic, 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-10 11:20:44 -07:00
|
|
|
return nil, nil
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
|
|
|
|
ctx, done := trace.StartSpan(ctx, "mod.Diagnostics", telemetry.File.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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, _, parseErrors, err := snapshot.ModTidyHandle(ctx, realfh).Tidy(ctx)
|
2019-12-17 14:13:33 -07:00
|
|
|
if err != nil {
|
2020-01-10 11:20:44 -07:00
|
|
|
return nil, err
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
|
|
|
|
2020-01-10 11:20:44 -07:00
|
|
|
reports := map[source.FileIdentity][]source.Diagnostic{
|
|
|
|
realfh.Identity(): []source.Diagnostic{},
|
|
|
|
}
|
2020-01-16 12:32:09 -07:00
|
|
|
for _, e := range parseErrors {
|
|
|
|
diag := source.Diagnostic{
|
|
|
|
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-10 11:20:44 -07:00
|
|
|
return reports, nil
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:32:09 -07:00
|
|
|
func SuggestedFixes(ctx context.Context, snapshot source.Snapshot, f source.FileHandle, diags []protocol.Diagnostic) []protocol.CodeAction {
|
|
|
|
_, _, parseErrors, err := snapshot.ModTidyHandle(ctx, f).Tidy(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Error(ctx, "no file", err, telemetry.URI.Of(uri))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
URI: protocol.NewURI(fh.Identity().URI),
|
|
|
|
},
|
|
|
|
},
|
2020-01-16 12:32:09 -07:00
|
|
|
Edits: edits,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
actions = append(actions, action)
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
}
|
|
|
|
return actions
|
|
|
|
}
|
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
|
|
|
|
}
|