|
| 1 | +// Copyright 2024 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 | + "regexp" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "golang.org/x/tools/gopls/internal/protocol" |
| 15 | + "golang.org/x/tools/gopls/internal/util/slices" |
| 16 | + "golang.org/x/tools/internal/tool" |
| 17 | +) |
| 18 | + |
| 19 | +// codeaction implements the codeaction verb for gopls. |
| 20 | +type codeaction struct { |
| 21 | + EditFlags |
| 22 | + Kind string `flag:"kind" help:"comma-separated list of code action kinds to filter"` |
| 23 | + Title string `flag:"title" help:"regular expression to match title"` |
| 24 | + Exec bool `flag:"exec" help:"execute the first matching code action"` |
| 25 | + |
| 26 | + app *Application |
| 27 | +} |
| 28 | + |
| 29 | +func (cmd *codeaction) Name() string { return "codeaction" } |
| 30 | +func (cmd *codeaction) Parent() string { return cmd.app.Name() } |
| 31 | +func (cmd *codeaction) Usage() string { return "[codeaction-flags] filename[:line[:col]]" } |
| 32 | +func (cmd *codeaction) ShortHelp() string { return "list or execute code actions" } |
| 33 | +func (cmd *codeaction) DetailedHelp(f *flag.FlagSet) { |
| 34 | + fmt.Fprintf(f.Output(), ` |
| 35 | +
|
| 36 | +The codeaction command lists or executes code actions for the |
| 37 | +specified file or range of a file. Each code action contains |
| 38 | +either an edit to be directly applied to the file, or a command |
| 39 | +to be executed by the server, which may have an effect such as: |
| 40 | +- requesting that the client apply an edit; |
| 41 | +- changing the state of the server; or |
| 42 | +- requesting that the client open a document. |
| 43 | +
|
| 44 | +The -kind and and -title flags filter the list of actions. |
| 45 | +
|
| 46 | +The -kind flag specifies a comma-separated list of LSP CodeAction kinds. |
| 47 | +Only actions of these kinds will be requested from the server. |
| 48 | +Valid kinds include: |
| 49 | +
|
| 50 | + quickfix |
| 51 | + refactor |
| 52 | + refactor.extract |
| 53 | + refactor.inline |
| 54 | + refactor.rewrite |
| 55 | + source.organizeImports |
| 56 | + source.fixAll |
| 57 | + source.assembly |
| 58 | + source.doc |
| 59 | + source.freesymbols |
| 60 | + goTest |
| 61 | +
|
| 62 | +Kinds are hierarchical, so "refactor" includes "refactor.inline". |
| 63 | +(Note: actions of kind "goTest" are not returned unless explicitly |
| 64 | +requested.) |
| 65 | +
|
| 66 | +The -title flag specifies a regular expression that must match the |
| 67 | +action's title. (Ideally kinds would be specific enough that this |
| 68 | +isn't necessary; we really need to subdivide refactor.rewrite; see |
| 69 | +gopls/internal/settings/codeactionkind.go.) |
| 70 | +
|
| 71 | +The -exec flag causes the first matching code action to be executed. |
| 72 | +Without the flag, the matching actions are merely listed. |
| 73 | +
|
| 74 | +It is not currently possible to execute more than one action, |
| 75 | +as that requires a way to detect and resolve conflicts. |
| 76 | +TODO(adonovan): support it when golang/go#67049 is resolved. |
| 77 | +
|
| 78 | +If executing an action causes the server to send a patch to the |
| 79 | +client, the usual -write, -preserve, -diff, and -list flags govern how |
| 80 | +the client deals with the patch. |
| 81 | +
|
| 82 | +Example: execute the first "quick fix" in the specified file and show the diff: |
| 83 | +
|
| 84 | + $ gopls codeaction -kind=quickfix -exec -diff ./gopls/main.go |
| 85 | +
|
| 86 | +codeaction-flags: |
| 87 | +`) |
| 88 | + printFlagDefaults(f) |
| 89 | +} |
| 90 | + |
| 91 | +func (cmd *codeaction) Run(ctx context.Context, args ...string) error { |
| 92 | + if len(args) < 1 { |
| 93 | + return tool.CommandLineErrorf("codeaction expects at least 1 argument") |
| 94 | + } |
| 95 | + cmd.app.editFlags = &cmd.EditFlags |
| 96 | + conn, err := cmd.app.connect(ctx) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + defer conn.terminate(ctx) |
| 101 | + |
| 102 | + from := parseSpan(args[0]) |
| 103 | + uri := from.URI() |
| 104 | + file, err := conn.openFile(ctx, uri) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + rng, err := file.spanRange(from) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + titleRE, err := regexp.Compile(cmd.Title) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + // Get diagnostics, as they may encode various lazy code actions. |
| 119 | + if err := conn.diagnoseFiles(ctx, []protocol.DocumentURI{uri}); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + diagnostics := []protocol.Diagnostic{} // LSP wants non-nil slice |
| 123 | + conn.client.filesMu.Lock() |
| 124 | + diagnostics = append(diagnostics, file.diagnostics...) |
| 125 | + conn.client.filesMu.Unlock() |
| 126 | + |
| 127 | + // Request code actions of the desired kinds. |
| 128 | + var kinds []protocol.CodeActionKind |
| 129 | + if cmd.Kind != "" { |
| 130 | + for _, kind := range strings.Split(cmd.Kind, ",") { |
| 131 | + kinds = append(kinds, protocol.CodeActionKind(kind)) |
| 132 | + } |
| 133 | + } |
| 134 | + actions, err := conn.CodeAction(ctx, &protocol.CodeActionParams{ |
| 135 | + TextDocument: protocol.TextDocumentIdentifier{URI: uri}, |
| 136 | + Range: rng, |
| 137 | + Context: protocol.CodeActionContext{ |
| 138 | + Only: kinds, |
| 139 | + Diagnostics: diagnostics, |
| 140 | + }, |
| 141 | + }) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("%v: %v", from, err) |
| 144 | + } |
| 145 | + |
| 146 | + // Gather edits from matching code actions. |
| 147 | + var edits []protocol.TextEdit |
| 148 | + for _, act := range actions { |
| 149 | + if act.Disabled != nil { |
| 150 | + continue |
| 151 | + } |
| 152 | + if !titleRE.MatchString(act.Title) { |
| 153 | + continue |
| 154 | + } |
| 155 | + |
| 156 | + // If the provided span has a position (not just offsets), |
| 157 | + // and the action has diagnostics, the action must have a |
| 158 | + // diagnostic with the same range as it. |
| 159 | + if from.HasPosition() && len(act.Diagnostics) > 0 && |
| 160 | + !slices.ContainsFunc(act.Diagnostics, func(diag protocol.Diagnostic) bool { |
| 161 | + return diag.Range.Start == rng.Start |
| 162 | + }) { |
| 163 | + continue |
| 164 | + } |
| 165 | + |
| 166 | + if cmd.Exec { |
| 167 | + // -exec: run the first matching code action. |
| 168 | + if act.Command != nil { |
| 169 | + // This may cause the server to make |
| 170 | + // an ApplyEdit downcall to the client. |
| 171 | + if _, err := conn.executeCommand(ctx, act.Command); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + // The specification says that commands should |
| 175 | + // be executed _after_ edits are applied, not |
| 176 | + // instead of them, but we don't want to |
| 177 | + // duplicate edits. |
| 178 | + } else { |
| 179 | + // Partially apply CodeAction.Edit, a WorkspaceEdit. |
| 180 | + // (See also conn.Client.applyWorkspaceEdit(a.Edit)). |
| 181 | + for _, c := range act.Edit.DocumentChanges { |
| 182 | + tde := c.TextDocumentEdit |
| 183 | + if tde != nil && tde.TextDocument.URI == uri { |
| 184 | + // TODO(adonovan): this logic will butcher an edit that spans files. |
| 185 | + // It will also ignore create/delete/rename operations. |
| 186 | + // Fix or document. Need a three-way merge. |
| 187 | + edits = append(edits, protocol.AsTextEdits(tde.Edits)...) |
| 188 | + } |
| 189 | + } |
| 190 | + return applyTextEdits(file.mapper, edits, cmd.app.editFlags) |
| 191 | + } |
| 192 | + return nil |
| 193 | + } else { |
| 194 | + // No -exec: list matching code actions. |
| 195 | + action := "edit" |
| 196 | + if act.Command != nil { |
| 197 | + action = "command" |
| 198 | + } |
| 199 | + fmt.Printf("%s\t%q [%s]\n", |
| 200 | + action, |
| 201 | + act.Title, |
| 202 | + act.Kind) |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + if cmd.Exec { |
| 207 | + return fmt.Errorf("no matching code action at %s", from) |
| 208 | + } |
| 209 | + return nil |
| 210 | +} |
0 commit comments