Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / internal / lsp / cmd / check.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 cmd
6
7 import (
8         "context"
9         "flag"
10         "fmt"
11
12         "golang.org/x/tools/internal/span"
13         errors "golang.org/x/xerrors"
14 )
15
16 // check implements the check verb for gopls.
17 type check struct {
18         app *Application
19 }
20
21 func (c *check) Name() string      { return "check" }
22 func (c *check) Usage() string     { return "<filename>" }
23 func (c *check) ShortHelp() string { return "show diagnostic results for the specified file" }
24 func (c *check) DetailedHelp(f *flag.FlagSet) {
25         fmt.Fprint(f.Output(), `
26 Example: show the diagnostic results of this file:
27
28   $ gopls check internal/lsp/cmd/check.go
29
30         gopls check flags are:
31 `)
32         f.PrintDefaults()
33 }
34
35 // Run performs the check on the files specified by args and prints the
36 // results to stdout.
37 func (c *check) Run(ctx context.Context, args ...string) error {
38         if len(args) == 0 {
39                 // no files, so no results
40                 return nil
41         }
42         checking := map[span.URI]*cmdFile{}
43         var uris []span.URI
44         // now we ready to kick things off
45         conn, err := c.app.connect(ctx)
46         if err != nil {
47                 return err
48         }
49         defer conn.terminate(ctx)
50         for _, arg := range args {
51                 uri := span.URIFromPath(arg)
52                 uris = append(uris, uri)
53                 file := conn.AddFile(ctx, uri)
54                 if file.err != nil {
55                         return file.err
56                 }
57                 checking[uri] = file
58         }
59         if err := conn.diagnoseFiles(ctx, uris); err != nil {
60                 return err
61         }
62         conn.Client.filesMu.Lock()
63         defer conn.Client.filesMu.Unlock()
64
65         for _, file := range checking {
66                 for _, d := range file.diagnostics {
67                         spn, err := file.mapper.RangeSpan(d.Range)
68                         if err != nil {
69                                 return errors.Errorf("Could not convert position %v for %q", d.Range, d.Message)
70                         }
71                         fmt.Printf("%v: %v\n", spn, d.Message)
72                 }
73         }
74         return nil
75 }