File: check.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (110 lines) | stat: -rw-r--r-- 2,810 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 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/gopls/internal/protocol"
	"golang.org/x/tools/gopls/internal/settings"
	"golang.org/x/tools/gopls/internal/util/slices"
)

// check implements the check verb for gopls.
type check struct {
	app *Application
}

func (c *check) Name() string      { return "check" }
func (c *check) Parent() string    { return c.app.Name() }
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/cmd/check.go
`)
	printFlagDefaults(f)
}

// 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 {
		return nil
	}

	// TODO(adonovan): formally, we are required to set this
	// option if we want RelatedInformation, but it appears to
	// have no effect on the server, even though the default is
	// false. Investigate.
	origOptions := c.app.options
	c.app.options = func(opts *settings.Options) {
		if origOptions != nil {
			origOptions(opts)
		}
		opts.RelatedInformationSupported = true
	}

	conn, err := c.app.connect(ctx)
	if err != nil {
		return err
	}
	defer conn.terminate(ctx)

	// Open and diagnose the requested files.
	var (
		uris     []protocol.DocumentURI
		checking = make(map[protocol.DocumentURI]*cmdFile)
	)
	for _, arg := range args {
		uri := protocol.URIFromPath(arg)
		uris = append(uris, uri)
		file, err := conn.openFile(ctx, uri)
		if err != nil {
			return err
		}
		checking[uri] = file
	}
	if err := conn.diagnoseFiles(ctx, uris); err != nil {
		return err
	}

	// print prints a single element of a diagnostic.
	print := func(uri protocol.DocumentURI, rng protocol.Range, message string) error {
		file, err := conn.openFile(ctx, uri)
		if err != nil {
			return err
		}
		spn, err := file.rangeSpan(rng)
		if err != nil {
			return fmt.Errorf("could not convert position %v for %q", rng, message)
		}
		fmt.Printf("%v: %v\n", spn, message)
		return nil
	}

	for _, file := range checking {
		file.diagnosticsMu.Lock()
		diags := slices.Clone(file.diagnostics)
		file.diagnosticsMu.Unlock()

		for _, diag := range diags {
			if err := print(file.uri, diag.Range, diag.Message); err != nil {
				return err
			}
			for _, rel := range diag.RelatedInformation {
				if err := print(rel.Location.URI, rel.Location.Range, "- "+rel.Message); err != nil {
					return err
				}
			}

		}
	}
	return nil
}