.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.0 / 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/go/analysis"
11         "golang.org/x/tools/internal/event"
12         "golang.org/x/tools/internal/lsp/debug/tag"
13         "golang.org/x/tools/internal/lsp/protocol"
14         "golang.org/x/tools/internal/span"
15 )
16
17 type Diagnostic struct {
18         Range    protocol.Range
19         Message  string
20         Source   string
21         Code     string
22         CodeHref string
23         Severity protocol.DiagnosticSeverity
24         Tags     []protocol.DiagnosticTag
25
26         Related []RelatedInformation
27 }
28
29 type SuggestedFix struct {
30         Title   string
31         Edits   map[span.URI][]protocol.TextEdit
32         Command *protocol.Command
33 }
34
35 type RelatedInformation struct {
36         URI     span.URI
37         Range   protocol.Range
38         Message string
39 }
40
41 func GetTypeCheckDiagnostics(ctx context.Context, snapshot Snapshot, pkg Package) TypeCheckDiagnostics {
42         onlyIgnoredFiles := true
43         for _, pgf := range pkg.CompiledGoFiles() {
44                 onlyIgnoredFiles = onlyIgnoredFiles && snapshot.IgnoredFile(pgf.URI)
45         }
46         if onlyIgnoredFiles {
47                 return TypeCheckDiagnostics{}
48         }
49
50         // Prepare any additional reports for the errors in this package.
51         for _, e := range pkg.GetErrors() {
52                 // We only need to handle lower-level errors.
53                 if e.Kind != ListError {
54                         continue
55                 }
56                 // If no file is associated with the error, pick an open file from the package.
57                 if e.URI.Filename() == "" {
58                         for _, pgf := range pkg.CompiledGoFiles() {
59                                 if snapshot.IsOpen(pgf.URI) {
60                                         e.URI = pgf.URI
61                                 }
62                         }
63                 }
64         }
65         return typeCheckDiagnostics(ctx, snapshot, pkg)
66 }
67
68 func Analyze(ctx context.Context, snapshot Snapshot, pkg Package, typeCheckResult TypeCheckDiagnostics) (map[span.URI][]*Diagnostic, error) {
69         // Exit early if the context has been canceled. This also protects us
70         // from a race on Options, see golang/go#36699.
71         if ctx.Err() != nil {
72                 return nil, ctx.Err()
73         }
74         // If we don't have any list or parse errors, run analyses.
75         analyzers := pickAnalyzers(snapshot, typeCheckResult.HasTypeErrors)
76         analysisErrors, err := snapshot.Analyze(ctx, pkg.ID(), analyzers...)
77         if err != nil {
78                 return nil, err
79         }
80
81         reports := emptyDiagnostics(pkg)
82         // Report diagnostics and errors from root analyzers.
83         for _, e := range analysisErrors {
84                 // If the diagnostic comes from a "convenience" analyzer, it is not
85                 // meant to provide diagnostics, but rather only suggested fixes.
86                 // Skip these types of errors in diagnostics; we will use their
87                 // suggested fixes when providing code actions.
88                 if isConvenienceAnalyzer(e.Category) {
89                         continue
90                 }
91                 // This is a bit of a hack, but clients > 3.15 will be able to grey out unnecessary code.
92                 // If we are deleting code as part of all of our suggested fixes, assume that this is dead code.
93                 // TODO(golang/go#34508): Return these codes from the diagnostics themselves.
94                 var tags []protocol.DiagnosticTag
95                 if onlyDeletions(e.SuggestedFixes) {
96                         tags = append(tags, protocol.Unnecessary)
97                 }
98                 // Type error analyzers only alter the tags for existing type errors.
99                 if _, ok := snapshot.View().Options().TypeErrorAnalyzers[e.Category]; ok {
100                         existingDiagnostics := typeCheckResult.Diagnostics[e.URI]
101                         for _, d := range existingDiagnostics {
102                                 if r := protocol.CompareRange(e.Range, d.Range); r != 0 {
103                                         continue
104                                 }
105                                 if e.Message != d.Message {
106                                         continue
107                                 }
108                                 d.Tags = append(d.Tags, tags...)
109                         }
110                 } else {
111                         reports[e.URI] = append(reports[e.URI], &Diagnostic{
112                                 Range:    e.Range,
113                                 Message:  e.Message,
114                                 Source:   e.Category,
115                                 Severity: protocol.SeverityWarning,
116                                 Tags:     tags,
117                                 Related:  e.Related,
118                         })
119                 }
120         }
121         return reports, nil
122 }
123
124 func pickAnalyzers(snapshot Snapshot, hadTypeErrors bool) []*analysis.Analyzer {
125         // Always run convenience analyzers.
126         categories := []map[string]Analyzer{snapshot.View().Options().ConvenienceAnalyzers}
127         // If we had type errors, only run type error analyzers.
128         if hadTypeErrors {
129                 categories = append(categories, snapshot.View().Options().TypeErrorAnalyzers)
130         } else {
131                 categories = append(categories, snapshot.View().Options().DefaultAnalyzers, snapshot.View().Options().StaticcheckAnalyzers)
132         }
133         var analyzers []*analysis.Analyzer
134         for _, m := range categories {
135                 for _, a := range m {
136                         if a.IsEnabled(snapshot.View()) {
137                                 analyzers = append(analyzers, a.Analyzer)
138                         }
139                 }
140         }
141         return analyzers
142 }
143
144 func FileDiagnostics(ctx context.Context, snapshot Snapshot, uri span.URI) (VersionedFileIdentity, []*Diagnostic, error) {
145         fh, err := snapshot.GetVersionedFile(ctx, uri)
146         if err != nil {
147                 return VersionedFileIdentity{}, nil, err
148         }
149         pkg, _, err := GetParsedFile(ctx, snapshot, fh, NarrowestPackage)
150         if err != nil {
151                 return VersionedFileIdentity{}, nil, err
152         }
153         typeCheckResults := GetTypeCheckDiagnostics(ctx, snapshot, pkg)
154         diagnostics := typeCheckResults.Diagnostics[fh.URI()]
155         if !typeCheckResults.HasParseOrListErrors {
156                 reports, err := Analyze(ctx, snapshot, pkg, typeCheckResults)
157                 if err != nil {
158                         return VersionedFileIdentity{}, nil, err
159                 }
160                 diagnostics = append(diagnostics, reports[fh.URI()]...)
161         }
162         return fh.VersionedFileIdentity(), diagnostics, nil
163 }
164
165 type TypeCheckDiagnostics struct {
166         HasTypeErrors        bool
167         HasParseOrListErrors bool
168         Diagnostics          map[span.URI][]*Diagnostic
169 }
170
171 type diagnosticSet struct {
172         listErrors, parseErrors, typeErrors []*Diagnostic
173 }
174
175 func typeCheckDiagnostics(ctx context.Context, snapshot Snapshot, pkg Package) TypeCheckDiagnostics {
176         ctx, done := event.Start(ctx, "source.diagnostics", tag.Package.Of(pkg.ID()))
177         _ = ctx // circumvent SA4006
178         defer done()
179
180         diagSets := make(map[span.URI]*diagnosticSet)
181         for _, e := range pkg.GetErrors() {
182                 diag := &Diagnostic{
183                         Message:  e.Message,
184                         Range:    e.Range,
185                         Severity: protocol.SeverityError,
186                         Related:  e.Related,
187                 }
188                 set, ok := diagSets[e.URI]
189                 if !ok {
190                         set = &diagnosticSet{}
191                         diagSets[e.URI] = set
192                 }
193                 switch e.Kind {
194                 case ParseError:
195                         set.parseErrors = append(set.parseErrors, diag)
196                         diag.Source = "syntax"
197                 case TypeError:
198                         set.typeErrors = append(set.typeErrors, diag)
199                         diag.Source = "compiler"
200                 case ListError:
201                         set.listErrors = append(set.listErrors, diag)
202                         diag.Source = "go list"
203                 }
204         }
205         typecheck := TypeCheckDiagnostics{
206                 Diagnostics: emptyDiagnostics(pkg),
207         }
208         for uri, set := range diagSets {
209                 // Don't report type errors if there are parse errors or list errors.
210                 diags := set.typeErrors
211                 switch {
212                 case len(set.parseErrors) > 0:
213                         typecheck.HasParseOrListErrors = true
214                         diags = set.parseErrors
215                 case len(set.listErrors) > 0:
216                         typecheck.HasParseOrListErrors = true
217                         if len(pkg.MissingDependencies()) > 0 {
218                                 diags = set.listErrors
219                         }
220                 case len(set.typeErrors) > 0:
221                         typecheck.HasTypeErrors = true
222                 }
223                 typecheck.Diagnostics[uri] = diags
224         }
225         return typecheck
226 }
227
228 func emptyDiagnostics(pkg Package) map[span.URI][]*Diagnostic {
229         diags := map[span.URI][]*Diagnostic{}
230         for _, pgf := range pkg.CompiledGoFiles() {
231                 if _, ok := diags[pgf.URI]; !ok {
232                         diags[pgf.URI] = nil
233                 }
234         }
235         return diags
236 }
237
238 // onlyDeletions returns true if all of the suggested fixes are deletions.
239 func onlyDeletions(fixes []SuggestedFix) bool {
240         for _, fix := range fixes {
241                 for _, edits := range fix.Edits {
242                         for _, edit := range edits {
243                                 if edit.NewText != "" {
244                                         return false
245                                 }
246                                 if protocol.ComparePosition(edit.Range.Start, edit.Range.End) == 0 {
247                                         return false
248                                 }
249                         }
250                 }
251         }
252         return len(fixes) > 0
253 }
254
255 func isConvenienceAnalyzer(category string) bool {
256         for _, a := range DefaultOptions().ConvenienceAnalyzers {
257                 if category == a.Analyzer.Name {
258                         return true
259                 }
260         }
261         return false
262 }