.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / internal / lsp / source / diagnostics.go
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 source
6
7 import (
8         "context"
9
10         "golang.org/x/tools/internal/lsp/protocol"
11         "golang.org/x/tools/internal/span"
12 )
13
14 type SuggestedFix struct {
15         Title   string
16         Edits   map[span.URI][]protocol.TextEdit
17         Command *protocol.Command
18 }
19
20 type RelatedInformation struct {
21         URI     span.URI
22         Range   protocol.Range
23         Message string
24 }
25
26 func Analyze(ctx context.Context, snapshot Snapshot, pkg Package, includeConvenience bool) (map[span.URI][]*Diagnostic, error) {
27         // Exit early if the context has been canceled. This also protects us
28         // from a race on Options, see golang/go#36699.
29         if ctx.Err() != nil {
30                 return nil, ctx.Err()
31         }
32
33         categories := []map[string]*Analyzer{}
34         if includeConvenience {
35                 categories = append(categories, snapshot.View().Options().ConvenienceAnalyzers)
36         }
37         // If we had type errors, don't run any other analyzers.
38         if !pkg.HasTypeErrors() {
39                 categories = append(categories, snapshot.View().Options().DefaultAnalyzers, snapshot.View().Options().StaticcheckAnalyzers)
40         }
41         var analyzers []*Analyzer
42         for _, cat := range categories {
43                 for _, a := range cat {
44                         analyzers = append(analyzers, a)
45                 }
46         }
47
48         analysisDiagnostics, err := snapshot.Analyze(ctx, pkg.ID(), analyzers)
49         if err != nil {
50                 return nil, err
51         }
52
53         reports := map[span.URI][]*Diagnostic{}
54         // Report diagnostics and errors from root analyzers.
55         for _, diag := range analysisDiagnostics {
56                 reports[diag.URI] = append(reports[diag.URI], diag)
57         }
58         return reports, nil
59 }
60
61 func FileDiagnostics(ctx context.Context, snapshot Snapshot, uri span.URI) (VersionedFileIdentity, []*Diagnostic, error) {
62         fh, err := snapshot.GetVersionedFile(ctx, uri)
63         if err != nil {
64                 return VersionedFileIdentity{}, nil, err
65         }
66         pkg, _, err := GetParsedFile(ctx, snapshot, fh, NarrowestPackage)
67         if err != nil {
68                 return VersionedFileIdentity{}, nil, err
69         }
70         diagnostics, err := snapshot.DiagnosePackage(ctx, pkg)
71         if err != nil {
72                 return VersionedFileIdentity{}, nil, err
73         }
74         fileDiags := diagnostics[fh.URI()]
75         if !pkg.HasListOrParseErrors() {
76                 analysisDiags, err := Analyze(ctx, snapshot, pkg, false)
77                 if err != nil {
78                         return VersionedFileIdentity{}, nil, err
79                 }
80                 fileDiags = append(fileDiags, analysisDiags[fh.URI()]...)
81         }
82         return fh.VersionedFileIdentity(), fileDiags, nil
83 }
84
85 func isConvenienceAnalyzer(category string) bool {
86         for _, a := range DefaultOptions().ConvenienceAnalyzers {
87                 if category == a.Analyzer.Name {
88                         return true
89                 }
90         }
91         return false
92 }