Debian Codegraph @

gopls/internal/cmd/codelens.go

raw symbols SCIP
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
5 package cmd
6
7 import (
8 "context"
9 "flag"
10 "fmt"
11
12 "golang.org/x/tools/gopls/internal/protocol"
13 "golang.org/x/tools/gopls/internal/settings"
14 "golang.org/x/tools/gopls/internal/tool"
15 )
16
17 // codelens implements the codelens verb for gopls.
18 type codelens struct {
19 EditFlags
20 app *Application
21
22 Exec bool `flag:"exec" help:"execute the first matching code lens"`
23 }
24
25 func (r *codelens) Name() string { return "codelens" }
26 func (r *codelens) Parent() string { return r.app.Name() }
27 func (r *codelens) Usage() string { return "[codelens-flags] file[:line[:col]] [title]" }
28 func (r *codelens) ShortHelp() string { return "List or execute code lenses for a file" }
29 func (r *codelens) DetailedHelp(f *flag.FlagSet) {
30 fmt.Fprint(f.Output(), `
31 The codelens command lists or executes code lenses for the specified
32 file, or line within a file. A code lens is a command associated with
33 a position in the code.
34
35 With an optional title argument, only code lenses matching that
36 title are considered.
37
38 By default, the codelens command lists the available lenses for the
39 specified file or line within a file, including the title and
40 title of the command. With the -exec flag, the first matching command
41 is executed, and its output is printed to stdout.
42
43 Example:
44
45 $ gopls codelens a_test.go # list code lenses in a file
46 $ gopls codelens a_test.go:10 # list code lenses on line 10
47 $ gopls codelens a_test.go "run test" # list gopls.run_tests commands
48 $ gopls codelens -exec a_test.go:10 "run test" # run a specific test
49
50 codelens-flags:
51 `)
52 printFlagDefaults(f)
53 }
54
55 func (r *codelens) Run(ctx context.Context, args ...string) error {
56 var filename, title string
57 switch len(args) {
58 case 0:
59 return tool.CommandLineErrorf("codelens requires a file name")
60 case 2:
61 title = args[1]
62 fallthrough
63 case 1:
64 filename = args[0]
65 default:
66 return tool.CommandLineErrorf("codelens expects at most two arguments")
67 }
68
69 r.app.editFlags = &r.EditFlags // in case a codelens perform an edit
70
71 // Override the default setting for codelenses["test"], which is
72 // off by default because VS Code has a superior client-side
73 // implementation. But this client is not VS Code.
74 // See golang.LensFuncs().
75 origOptions := r.app.options
76 r.app.options = func(opts *settings.Options) {
77 if origOptions != nil {
78 origOptions(opts)
79 }
80 if opts.Codelenses == nil {
81 opts.Codelenses = make(map[settings.CodeLensSource]bool)
82 }
83 opts.Codelenses[settings.CodeLensTest] = true
84 }
85
86 cli, _, err := r.app.connect(ctx)
87 if err != nil {
88 return err
89 }
90 defer cli.terminate(ctx)
91
92 filespan := parseSpan(filename)
93 file, err := cli.openFile(ctx, filespan.URI())
94 if err != nil {
95 return err
96 }
97 loc, err := file.spanLocation(filespan)
98 if err != nil {
99 return err
100 }
101
102 p := protocol.CodeLensParams{
103 TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
104 }
105 lenses, err := cli.server.CodeLens(ctx, &p)
106 if err != nil {
107 return err
108 }
109
110 for _, lens := range lenses {
111 sp, err := file.rangeSpan(lens.Range)
112 if err != nil {
113 return nil
114 }
115
116 if title != "" && lens.Command.Title != title {
117 continue // title was specified but does not match
118 }
119 if filespan.HasPosition() && !protocol.Intersect(loc.Range, lens.Range) {
120 continue // position was specified but does not match
121 }
122
123 // -exec: run the first matching code lens.
124 if r.Exec {
125 _, err := executeCommand(ctx, cli.server, lens.Command)
126 return err
127 }
128
129 // No -exec: list matching code lenses.
130 fmt.Printf("%v: %q [%s]\n", sp, lens.Command.Title, lens.Command.Command)
131 }
132
133 if r.Exec {
134 return fmt.Errorf("no code lens at %s with title %q", filespan, title)
135 }
136 return nil
137 }