| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
pkgDiags, err := snapshot.PackageDiagnostics(ctx, mp.ID) |
| 29 |
if err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
diags := pkgDiags[uri] |
| 33 |
|
| 34 |
|
| 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 |
|
| 42 |
|
| 43 |
return CombineDiagnostics(diags, analysisDiags), nil |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
func Analyze(ctx context.Context, snapshot *cache.Snapshot, pkgIDs map[PackageID]*metadata.Package, tracker *progress.Tracker) (map[protocol.DocumentURI][]*cache.Diagnostic, error) { |
| 53 |
|
| 54 |
|
| 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 |
|
| 67 |
func byURI(d *cache.Diagnostic) protocol.DocumentURI { return d.URI } |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
func CombineDiagnostics(tdiags []*cache.Diagnostic, adiags []*cache.Diagnostic) []*cache.Diagnostic { |
| 84 |
|
| 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) |
| 91 |
for i, diag := range tdiags { |
| 92 |
index[key{diag.Range, diag.Message}] = i |
| 93 |
combined[i] = diag |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 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] = © |
| 104 |
continue |
| 105 |
} |
| 106 |
combined = append(combined, diag) |
| 107 |
} |
| 108 |
return combined |
| 109 |
} |