Debian Codegraph @

gopls/internal/cmd/check.go

raw symbols SCIP
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 "slices"
12
13 "golang.org/x/tools/gopls/internal/protocol"
14 "golang.org/x/tools/gopls/internal/settings"
15 )
16
17 // check implements the check verb for gopls.
18 type check struct {
19 app *Application
20 Severity string `flag:"severity" help:"minimum diagnostic severity (hint, info, warning, or error)"`
21 }
22
23 func (c *check) Name() string { return "check" }
24 func (c *check) Parent() string { return c.app.Name() }
25 func (c *check) Usage() string { return "<filename>" }
26 func (c *check) ShortHelp() string { return "show diagnostic results for the specified file" }
27 func (c *check) DetailedHelp(f *flag.FlagSet) {
28 fmt.Fprint(f.Output(), `
29 Example: show the diagnostic results of this file:
30
31 $ gopls check internal/cmd/check.go
32 `)
33 printFlagDefaults(f)
34 }
35
36 // Run performs the check on the files specified by args and prints the
37 // results to stdout.
38 func (c *check) Run(ctx context.Context, args ...string) error {
39 severityCutoff := protocol.SeverityWarning
40 switch c.Severity {
41 case "hint":
42 severityCutoff = protocol.SeverityHint
43 case "info":
44 severityCutoff = protocol.SeverityInformation
45 case "warning":
46 // default
47 case "error":
48 severityCutoff = protocol.SeverityError
49 default:
50 return fmt.Errorf("unrecognized -severity value %q", c.Severity)
51 }
52
53 if len(args) == 0 {
54 return nil
55 }
56
57 // TODO(adonovan): formally, we are required to set this
58 // option if we want RelatedInformation, but it appears to
59 // have no effect on the server, even though the default is
60 // false. Investigate.
61 origOptions := c.app.options
62 c.app.options = func(opts *settings.Options) {
63 if origOptions != nil {
64 origOptions(opts)
65 }
66 opts.RelatedInformationSupported = true
67 }
68
69 cli, _, err := c.app.connect(ctx)
70 if err != nil {
71 return err
72 }
73 defer cli.terminate(ctx)
74
75 // Open and diagnose the requested files.
76 var (
77 uris []protocol.DocumentURI
78 checking = make(map[protocol.DocumentURI]*cmdFile)
79 )
80 for _, arg := range args {
81 uri := protocol.URIFromPath(arg)
82 uris = append(uris, uri)
83 file, err := cli.openFile(ctx, uri)
84 if err != nil {
85 return err
86 }
87 checking[uri] = file
88 }
89 if err := diagnoseFiles(ctx, cli.server, uris); err != nil {
90 return err
91 }
92
93 // print prints a single element of a diagnostic.
94 print := func(uri protocol.DocumentURI, rng protocol.Range, message string) error {
95 file, err := cli.openFile(ctx, uri)
96 if err != nil {
97 return err
98 }
99 spn, err := file.rangeSpan(rng)
100 if err != nil {
101 return fmt.Errorf("could not convert position %v for %q", rng, message)
102 }
103 fmt.Printf("%v: %v\n", spn, message)
104 return nil
105 }
106
107 for _, file := range checking {
108 file.diagnosticsMu.Lock()
109 diags := slices.Clone(file.diagnostics)
110 file.diagnosticsMu.Unlock()
111
112 for _, diag := range diags {
113 if diag.Severity > severityCutoff { // lower severity value => greater severity, counterintuitively
114 continue
115 }
116 if err := print(file.uri, diag.Range, diag.Message); err != nil {
117 return err
118 }
119 for _, rel := range diag.RelatedInformation {
120 if err := print(rel.Location.URI, rel.Location.Range, "- "+rel.Message); err != nil {
121 return err
122 }
123 }
124
125 }
126 }
127 return nil
128 }