Debian Codegraph @

gopls/internal/golang/diagnostics.go

raw symbols SCIP
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package golang
6
7 import (
8 "context"
9
10 "golang.org/x/tools/gopls/internal/cache"
11 "golang.org/x/tools/gopls/internal/cache/metadata"
12 "golang.org/x/tools/gopls/internal/progress"
13 "golang.org/x/tools/gopls/internal/protocol"
14 "golang.org/x/tools/gopls/internal/util/moremaps"
15 )
16
17 // DiagnoseFile returns pull-based diagnostics for the given file.
18 func DiagnoseFile(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) ([]*cache.Diagnostic, error) {
19 mp, err := snapshot.NarrowestMetadataForFile(ctx, uri)
20 if err != nil {
21 return nil, err
22 }
23
24 // TODO(rfindley): consider analysing the package concurrently to package
25 // diagnostics.
26
27 // Get package (list/parse/type check) diagnostics.
28 pkgDiags, err := snapshot.PackageDiagnostics(ctx, mp.ID)
29 if err != nil {
30 return nil, err
31 }
32 diags := pkgDiags[uri]
33
34 // Get analysis diagnostics.
35 pkgAnalysisDiags, err := snapshot.Analyze(ctx, map[PackageID]*metadata.Package{mp.ID: mp}, nil)
36 if err != nil {
37 return nil, err
38 }
39 analysisDiags := moremaps.Group(pkgAnalysisDiags, byURI)[uri]
40
41 // Return the merged set of file diagnostics, combining type error analyses
42 // with type error diagnostics.
43 return CombineDiagnostics(diags, analysisDiags), nil
44 }
45
46 // Analyze reports go/analysis-framework diagnostics in the specified package.
47 //
48 // If the provided tracker is non-nil, it may be used to provide notifications
49 // of the ongoing analysis pass.
50 //
51 // TODO(rfindley): merge this with snapshot.Analyze.
52 func Analyze(ctx context.Context, snapshot *cache.Snapshot, pkgIDs map[PackageID]*metadata.Package, tracker *progress.Tracker) (map[protocol.DocumentURI][]*cache.Diagnostic, error) {
53 // Exit early if the context has been canceled. This also protects us
54 // from a race on Options, see golang/go#36699.
55 if ctx.Err() != nil {
56 return nil, ctx.Err()
57 }
58
59 analysisDiagnostics, err := snapshot.Analyze(ctx, pkgIDs, tracker)
60 if err != nil {
61 return nil, err
62 }
63 return moremaps.Group(analysisDiagnostics, byURI), nil
64 }
65
66 // byURI is used for grouping diagnostics.
67 func byURI(d *cache.Diagnostic) protocol.DocumentURI { return d.URI }
68
69 // CombineDiagnostics combines and filters list/parse/type diagnostics from
70 // tdiags with the analysis adiags, returning the resulting combined set.
71 //
72 // Type-error analyzers produce diagnostics that are redundant with type
73 // checker diagnostics, but more detailed (e.g. fixes). Rather than report two
74 // diagnostics for the same problem, we combine them by augmenting the
75 // type-checker diagnostic and discarding the analyzer diagnostic.
76 //
77 // If an analysis diagnostic has the same range and message as a
78 // list/parse/type diagnostic, the suggested fix information (et al) of the
79 // latter is merged into a copy of the former. This handles the case where a
80 // type-error analyzer suggests a fix to a type error, and avoids duplication.
81 //
82 // The arguments are not modified.
83 func CombineDiagnostics(tdiags []*cache.Diagnostic, adiags []*cache.Diagnostic) []*cache.Diagnostic {
84 // Build index of (list+parse+)type errors.
85 type key struct {
86 Range protocol.Range
87 message string
88 }
89 combined := make([]*cache.Diagnostic, len(tdiags))
90 index := make(map[key]int) // maps (Range,Message) to index in tdiags slice
91 for i, diag := range tdiags {
92 index[key{diag.Range, diag.Message}] = i
93 combined[i] = diag
94 }
95
96 // Filter out analysis diagnostics that match type errors,
97 // retaining their suggested fix (etc) fields.
98 for _, diag := range adiags {
99 if i, ok := index[key{diag.Range, diag.Message}]; ok {
100 copy := *tdiags[i]
101 copy.SuggestedFixes = diag.SuggestedFixes
102 copy.Tags = diag.Tags
103 combined[i] = &copy
104 continue
105 }
106 combined = append(combined, diag)
107 }
108 return combined
109 }