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 5package cmd 6 7import ( 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. 17type check struct { 18 app *Application 19} 20 21func (c *check) Name() string { return "check" } 22func (c *check) Usage() string { return "<filename>" } 23func (c *check) ShortHelp() string { return "show diagnostic results for the specified file" } 24func (c *check) DetailedHelp(f *flag.FlagSet) { 25 fmt.Fprint(f.Output(), ` 26Example: 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. 37func (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} 76