Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / internal / lsp / mod / diagnostics.go
1 // Copyright 2019 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 mod provides core features related to go.mod file
6 // handling for use by Go editors and tools.
7 package mod
8
9 import (
10         "context"
11
12         "golang.org/x/tools/internal/event"
13         "golang.org/x/tools/internal/lsp/debug/tag"
14         "golang.org/x/tools/internal/lsp/protocol"
15         "golang.org/x/tools/internal/lsp/source"
16 )
17
18 func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.VersionedFileIdentity][]*source.Diagnostic, error) {
19         ctx, done := event.Start(ctx, "mod.Diagnostics", tag.Snapshot.Of(snapshot.ID()))
20         defer done()
21
22         reports := map[source.VersionedFileIdentity][]*source.Diagnostic{}
23         for _, uri := range snapshot.ModFiles() {
24                 fh, err := snapshot.GetVersionedFile(ctx, uri)
25                 if err != nil {
26                         return nil, err
27                 }
28                 reports[fh.VersionedFileIdentity()] = []*source.Diagnostic{}
29                 errors, err := ErrorsForMod(ctx, snapshot, fh)
30                 if err != nil {
31                         return nil, err
32                 }
33                 for _, e := range errors {
34                         d := &source.Diagnostic{
35                                 Message: e.Message,
36                                 Range:   e.Range,
37                                 Source:  e.Category,
38                         }
39                         if e.Category == "syntax" {
40                                 d.Severity = protocol.SeverityError
41                         } else {
42                                 d.Severity = protocol.SeverityWarning
43                         }
44                         fh, err := snapshot.GetVersionedFile(ctx, e.URI)
45                         if err != nil {
46                                 return nil, err
47                         }
48                         reports[fh.VersionedFileIdentity()] = append(reports[fh.VersionedFileIdentity()], d)
49                 }
50         }
51         return reports, nil
52 }
53
54 func ErrorsForMod(ctx context.Context, snapshot source.Snapshot, fh source.FileHandle) ([]source.Error, error) {
55         tidied, err := snapshot.ModTidy(ctx, fh)
56
57         if source.IsNonFatalGoModError(err) {
58                 return nil, nil
59         }
60         if err != nil {
61                 return nil, err
62         }
63         return tidied.Errors, nil
64 }