| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 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 |
|
| 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 { |
| 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 |
} |