Skip to content

Commit

Permalink
gopls/internal/lsp: eliminate source.RelatedInformation
Browse files Browse the repository at this point in the history
This type was trivially equivalent to the existing
protocol.DiagnosticRelatedInformation. Remove it.

Change-Id: Ie113098fcbb1cdabf0bbcd5e36ec2c4bfaa2f1f6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/467595
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
  • Loading branch information
findleyr committed Feb 14, 2023
1 parent fdfdda5 commit c8e8b3b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 51 deletions.
27 changes: 10 additions & 17 deletions gopls/internal/lsp/cache/errors.go
Expand Up @@ -129,10 +129,9 @@ func typeErrorDiagnostics(snapshot *snapshot, pkg *syntaxPackage, e extendedErro
if err != nil {
return nil, err
}
diag.Related = append(diag.Related, source.RelatedInformation{
URI: secondaryLoc.URI.SpanURI(),
Range: secondaryLoc.Range,
Message: secondary.Msg,
diag.Related = append(diag.Related, protocol.DiagnosticRelatedInformation{
Location: secondaryLoc,
Message: secondary.Msg,
})
}

Expand Down Expand Up @@ -186,6 +185,11 @@ func editGoDirectiveQuickFix(snapshot *snapshot, uri span.URI, version string) (

// toSourceDiagnostic converts a gobDiagnostic to "source" form.
func toSourceDiagnostic(srcAnalyzer *source.Analyzer, gobDiag *gobDiagnostic) *source.Diagnostic {
var related []protocol.DiagnosticRelatedInformation
for _, gobRelated := range gobDiag.Related {
related = append(related, protocol.DiagnosticRelatedInformation(gobRelated))
}

kinds := srcAnalyzer.ActionKind
if len(srcAnalyzer.ActionKind) == 0 {
kinds = append(kinds, protocol.QuickFix)
Expand All @@ -210,14 +214,15 @@ func toSourceDiagnostic(srcAnalyzer *source.Analyzer, gobDiag *gobDiagnostic) *s
if severity == 0 {
severity = protocol.SeverityWarning
}

diag := &source.Diagnostic{
// TODO(adonovan): is this sound? See dual conversion in posToLocation.
URI: span.URI(gobDiag.Location.URI),
Range: gobDiag.Location.Range,
Severity: severity,
Source: source.AnalyzerErrorKind(gobDiag.Category),
Message: gobDiag.Message,
Related: relatedInformation(gobDiag),
Related: related,
SuggestedFixes: fixes,
}
// If the fixes only delete code, assume that the diagnostic is reporting dead code.
Expand Down Expand Up @@ -275,18 +280,6 @@ func suggestedAnalysisFixes(diag *gobDiagnostic, kinds []protocol.CodeActionKind
return fixes
}

func relatedInformation(diag *gobDiagnostic) []source.RelatedInformation {
var out []source.RelatedInformation
for _, related := range diag.Related {
out = append(out, source.RelatedInformation{
URI: span.URI(related.Location.URI),
Range: related.Location.Range,
Message: related.Message,
})
}
return out
}

func typeErrorData(pkg *syntaxPackage, terr types.Error) (typesinternal.ErrorCode, protocol.Location, error) {
ecode, start, end, ok := typesinternal.ReadGo116ErrorData(terr)
if !ok {
Expand Down
16 changes: 3 additions & 13 deletions gopls/internal/lsp/diagnostics.go
Expand Up @@ -114,7 +114,7 @@ func hashDiagnostics(diags ...*source.Diagnostic) string {
fmt.Fprintf(h, "%s", t)
}
for _, r := range d.Related {
fmt.Fprintf(h, "%s%s%s", r.URI, r.Message, r.Range)
fmt.Fprintf(h, "%s%s%s", r.Location.URI.SpanURI(), r.Message, r.Location.Range)
}
fmt.Fprintf(h, "%s%s%s%s", d.Message, d.Range, d.Severity, d.Source)
}
Expand Down Expand Up @@ -696,24 +696,14 @@ func (s *Server) publishDiagnostics(ctx context.Context, final bool, snapshot so
func toProtocolDiagnostics(diagnostics []*source.Diagnostic) []protocol.Diagnostic {
reports := []protocol.Diagnostic{}
for _, diag := range diagnostics {
related := make([]protocol.DiagnosticRelatedInformation, 0, len(diag.Related))
for _, rel := range diag.Related {
related = append(related, protocol.DiagnosticRelatedInformation{
Location: protocol.Location{
URI: protocol.URIFromSpanURI(rel.URI),
Range: rel.Range,
},
Message: rel.Message,
})
}
pdiag := protocol.Diagnostic{
// diag.Message might start with \n or \t
Message: strings.TrimSpace(diag.Message),
Range: diag.Range,
Severity: diag.Severity,
Source: string(diag.Source),
Tags: diag.Tags,
RelatedInformation: related,
RelatedInformation: diag.Related,
}
if diag.Code != "" {
pdiag.Code = diag.Code
Expand Down Expand Up @@ -774,7 +764,7 @@ func auxStr(v *source.Diagnostic, d diagnosticReport, typ diagnosticSource) stri
msg := fmt.Sprintf("(%s)%q(source:%q,code:%q,severity:%s,snapshot:%d,type:%s)",
v.Range, v.Message, v.Source, v.Code, v.Severity, d.snapshotID, typ)
for _, r := range v.Related {
msg += fmt.Sprintf(" [%s:%s,%q]", r.URI.Filename(), r.Range, r.Message)
msg += fmt.Sprintf(" [%s:%s,%q]", r.Location.URI.SpanURI().Filename(), r.Location.Range, r.Message)
}
return msg
}
20 changes: 11 additions & 9 deletions gopls/internal/lsp/mod/diagnostics.go
Expand Up @@ -241,7 +241,7 @@ func ModVulnerabilityDiagnostics(ctx context.Context, snapshot source.Snapshot,
// Fixes will include only the upgrades for warning level diagnostics.
var warningFixes, infoFixes []source.SuggestedFix
var warning, info []string
var relatedInfo []source.RelatedInformation
var relatedInfo []protocol.DiagnosticRelatedInformation
for _, mv := range vulns {
mod, vuln := mv.mod, mv.vuln
// It is possible that the source code was changed since the last
Expand Down Expand Up @@ -348,7 +348,7 @@ func ModVulnerabilityDiagnostics(ctx context.Context, snapshot source.Snapshot,

stdlib := stdlibVulns[0].mod.FoundVersion
var warning, info []string
var relatedInfo []source.RelatedInformation
var relatedInfo []protocol.DiagnosticRelatedInformation
for _, mv := range stdlibVulns {
vuln := mv.vuln
stdlib = mv.mod.FoundVersion
Expand Down Expand Up @@ -442,8 +442,8 @@ func getVulnMessage(mod string, vulns []string, used, fromGovulncheck bool) stri
return b.String()
}

func listRelatedInfo(ctx context.Context, snapshot source.Snapshot, vuln *govulncheck.Vuln) []source.RelatedInformation {
var ri []source.RelatedInformation
func listRelatedInfo(ctx context.Context, snapshot source.Snapshot, vuln *govulncheck.Vuln) []protocol.DiagnosticRelatedInformation {
var ri []protocol.DiagnosticRelatedInformation
for _, m := range vuln.Modules {
for _, p := range m.Packages {
for _, c := range p.CallStacks {
Expand All @@ -469,11 +469,13 @@ func listRelatedInfo(ctx context.Context, snapshot source.Snapshot, vuln *govuln
// position computation.
Character: 0,
}
ri = append(ri, source.RelatedInformation{
URI: uri,
Range: protocol.Range{
Start: startPos,
End: startPos,
ri = append(ri, protocol.DiagnosticRelatedInformation{
Location: protocol.Location{
URI: protocol.URIFromSpanURI(uri),
Range: protocol.Range{
Start: startPos,
End: startPos,
},
},
Message: fmt.Sprintf("[%v] %v -> %v.%v", vuln.OSV.ID, entry.Name(), p.Path, c.Symbol),
})
Expand Down
7 changes: 0 additions & 7 deletions gopls/internal/lsp/source/diagnostics.go
Expand Up @@ -18,13 +18,6 @@ type SuggestedFix struct {
ActionKind protocol.CodeActionKind
}

type RelatedInformation struct {
// TODO(adonovan): replace these two fields by a protocol.Location.
URI span.URI
Range protocol.Range
Message string
}

// Analyze reports go/analysis-framework diagnostics in the specified package.
func Analyze(ctx context.Context, snapshot Snapshot, pkgid PackageID, includeConvenience bool) (map[span.URI][]*Diagnostic, error) {
// Exit early if the context has been canceled. This also protects us
Expand Down
15 changes: 11 additions & 4 deletions gopls/internal/lsp/source/gc_annotations.go
Expand Up @@ -136,11 +136,18 @@ func parseDetailsFile(filename string, options *Options) (span.URI, []*Diagnosti
if !showDiagnostic(msg, d.Source, options) {
continue
}
var related []RelatedInformation
var related []protocol.DiagnosticRelatedInformation
for _, ri := range d.RelatedInformation {
related = append(related, RelatedInformation{
URI: ri.Location.URI.SpanURI(),
Range: zeroIndexedRange(ri.Location.Range),
// TODO(rfindley): The compiler uses LSP-like JSON to encode gc details,
// however the positions it uses are 1-based UTF-8:
// https://github.com/golang/go/blob/master/src/cmd/compile/internal/logopt/log_opts.go
//
// Here, we adjust for 0-based positions, but do not translate UTF-8 to UTF-16.
related = append(related, protocol.DiagnosticRelatedInformation{
Location: protocol.Location{
URI: ri.Location.URI,
Range: zeroIndexedRange(ri.Location.Range),
},
Message: ri.Message,
})
}
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/source/view.go
Expand Up @@ -827,7 +827,7 @@ type Diagnostic struct {
Message string

Tags []protocol.DiagnosticTag
Related []RelatedInformation
Related []protocol.DiagnosticRelatedInformation

// Fields below are used internally to generate quick fixes. They aren't
// part of the LSP spec and don't leave the server.
Expand Down

0 comments on commit c8e8b3b

Please sign in to comment.