| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 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 |
| 118 |
} |
| 119 |
if filespan.HasPosition() && !protocol.Intersect(loc.Range, lens.Range) { |
| 120 |
continue |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
if r.Exec { |
| 125 |
_, err := executeCommand(ctx, cli.server, lens.Command) |
| 126 |
return err |
| 127 |
} |
| 128 |
|
| 129 |
|
| 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 |
} |