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 "time" 12 13 "golang.org/x/tools/internal/span" 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 // now we ready to kick things off 44 conn, err := c.app.connect(ctx) 45 if err != nil { 46 return err 47 } 48 defer conn.terminate(ctx) 49 for _, arg := range args { 50 uri := span.FileURI(arg) 51 file := conn.AddFile(ctx, uri) 52 if file.err != nil { 53 return file.err 54 } 55 checking[uri] = file 56 } 57 // now wait for results 58 //TODO: maybe conn.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{Command: "gopls-wait-idle"}) 59 for _, file := range checking { 60 select { 61 case <-file.hasDiagnostics: 62 case <-time.Tick(30 * time.Second): 63 return fmt.Errorf("timed out waiting for results from %v", file.uri) 64 } 65 file.diagnosticsMu.Lock() 66 defer file.diagnosticsMu.Unlock() 67 for _, d := range file.diagnostics { 68 spn, err := file.mapper.RangeSpan(d.Range) 69 if err != nil { 70 return fmt.Errorf("Could not convert position %v for %q", d.Range, d.Message) 71 } 72 fmt.Printf("%v: %v\n", spn, d.Message) 73 } 74 } 75 return nil 76} 77