2019-02-08 14:16:57 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-08 14:16:57 -07:00
|
|
|
)
|
|
|
|
|
2019-04-04 07:26:18 -06:00
|
|
|
// check implements the check verb for gopls.
|
2019-02-08 14:16:57 -07:00
|
|
|
type check struct {
|
|
|
|
app *Application
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *check) Name() string { return "check" }
|
|
|
|
func (c *check) Usage() string { return "<filename>" }
|
|
|
|
func (c *check) ShortHelp() string { return "show diagnostic results for the specified file" }
|
|
|
|
func (c *check) DetailedHelp(f *flag.FlagSet) {
|
|
|
|
fmt.Fprint(f.Output(), `
|
|
|
|
Example: show the diagnostic results of this file:
|
|
|
|
|
|
|
|
$ gopls check internal/lsp/cmd/check.go
|
|
|
|
|
|
|
|
gopls check flags are:
|
|
|
|
`)
|
|
|
|
f.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run performs the check on the files specified by args and prints the
|
|
|
|
// results to stdout.
|
|
|
|
func (c *check) Run(ctx context.Context, args ...string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
// no files, so no results
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-07 11:54:07 -06:00
|
|
|
checking := map[span.URI]*cmdFile{}
|
2020-01-10 15:54:47 -07:00
|
|
|
var uris []span.URI
|
2019-02-08 14:16:57 -07:00
|
|
|
// now we ready to kick things off
|
2019-05-07 11:54:07 -06:00
|
|
|
conn, err := c.app.connect(ctx)
|
2019-02-08 14:16:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-02 08:55:04 -06:00
|
|
|
defer conn.terminate(ctx)
|
2019-02-08 14:16:57 -07:00
|
|
|
for _, arg := range args {
|
|
|
|
uri := span.FileURI(arg)
|
2020-01-10 15:54:47 -07:00
|
|
|
uris = append(uris, uri)
|
2019-05-07 11:54:07 -06:00
|
|
|
file := conn.AddFile(ctx, uri)
|
|
|
|
if file.err != nil {
|
|
|
|
return file.err
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
2019-05-07 11:54:07 -06:00
|
|
|
checking[uri] = file
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
2020-01-10 15:54:47 -07:00
|
|
|
if err := conn.diagnoseFiles(ctx, uris); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-10 10:29:37 -07:00
|
|
|
conn.Client.filesMu.Lock()
|
|
|
|
defer conn.Client.filesMu.Unlock()
|
|
|
|
|
2019-05-07 11:54:07 -06:00
|
|
|
for _, file := range checking {
|
2020-01-10 15:54:47 -07:00
|
|
|
for _, d := range file.diagnostics {
|
2019-05-07 11:54:07 -06:00
|
|
|
spn, err := file.mapper.RangeSpan(d.Range)
|
2019-02-08 14:16:57 -07:00
|
|
|
if err != nil {
|
2019-08-06 13:13:11 -06:00
|
|
|
return errors.Errorf("Could not convert position %v for %q", d.Range, d.Message)
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
|
|
|
fmt.Printf("%v: %v\n", spn, d.Message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|