Rebecca Stambler | ccd3732 | 2019-04-05 15:56:08 -0400 | [diff] [blame] | 1 | // Copyright 2018 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 lsp |
| 6 | |
| 7 | import ( |
| 8 | "context" |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 9 | "fmt" |
Suzy Mueller | a0cf054 | 2019-08-14 15:24:21 -0400 | [diff] [blame] | 10 | "sort" |
Rebecca Stambler | ccd3732 | 2019-04-05 15:56:08 -0400 | [diff] [blame] | 11 | "strings" |
| 12 | |
Rebecca Stambler | 58eba7e | 2020-07-15 00:19:10 -0400 | [diff] [blame] | 13 | "golang.org/x/tools/internal/event" |
Suzy Mueller | 5f95ed5 | 2019-07-30 14:00:02 -0400 | [diff] [blame] | 14 | "golang.org/x/tools/internal/imports" |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 15 | "golang.org/x/tools/internal/lsp/command" |
Rebecca Stambler | 58eba7e | 2020-07-15 00:19:10 -0400 | [diff] [blame] | 16 | "golang.org/x/tools/internal/lsp/debug/tag" |
Rebecca Stambler | 1f28ee6 | 2020-10-13 00:50:48 -0400 | [diff] [blame] | 17 | "golang.org/x/tools/internal/lsp/mod" |
Rebecca Stambler | ccd3732 | 2019-04-05 15:56:08 -0400 | [diff] [blame] | 18 | "golang.org/x/tools/internal/lsp/protocol" |
| 19 | "golang.org/x/tools/internal/lsp/source" |
Rebecca Stambler | e31b568 | 2020-06-12 17:10:06 -0400 | [diff] [blame] | 20 | "golang.org/x/tools/internal/span" |
Rebecca Stambler | ccd3732 | 2019-04-05 15:56:08 -0400 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) { |
Heschi Kreinick | 412b8bd | 2020-07-02 18:34:10 -0400 | [diff] [blame] | 24 | snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind) |
| 25 | defer release() |
Heschi Kreinick | 5916a50 | 2020-02-13 13:46:49 -0500 | [diff] [blame] | 26 | if !ok { |
Rebecca Stambler | 80313e1 | 2019-11-15 12:43:45 -0500 | [diff] [blame] | 27 | return nil, err |
| 28 | } |
Heschi Kreinick | ecd3fc4 | 2020-06-08 15:21:24 -0400 | [diff] [blame] | 29 | uri := fh.URI() |
Rebecca Stambler | 57610ed | 2019-09-27 13:17:59 -0400 | [diff] [blame] | 30 | |
Rebecca Stambler | 1dcc99b | 2019-08-05 19:27:28 -0400 | [diff] [blame] | 31 | // Determine the supported actions for this file kind. |
Robert Findley | 3f6aab1 | 2022-01-13 11:04:02 -0500 | [diff] [blame] | 32 | kind := snapshot.View().FileKind(fh) |
pjw | 68b574a | 2022-01-07 19:06:25 -0500 | [diff] [blame] | 33 | supportedCodeActions, ok := snapshot.View().Options().SupportedCodeActions[kind] |
Rebecca Stambler | 1dcc99b | 2019-08-05 19:27:28 -0400 | [diff] [blame] | 34 | if !ok { |
pjw | 68b574a | 2022-01-07 19:06:25 -0500 | [diff] [blame] | 35 | return nil, fmt.Errorf("no supported code actions for %v file kind", kind) |
Rebecca Stambler | 1dcc99b | 2019-08-05 19:27:28 -0400 | [diff] [blame] | 36 | } |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 37 | |
| 38 | // The Only field of the context specifies which code actions the client wants. |
Pontus Leitzler | 8c26973 | 2020-10-03 14:12:29 +0200 | [diff] [blame] | 39 | // If Only is empty, assume that the client wants all of the non-explicit code actions. |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 40 | var wanted map[protocol.CodeActionKind]bool |
Pontus Leitzler | 8c26973 | 2020-10-03 14:12:29 +0200 | [diff] [blame] | 41 | |
| 42 | // Explicit Code Actions are opt-in and shouldn't be returned to the client unless |
| 43 | // requested using Only. |
| 44 | // TODO: Add other CodeLenses such as GoGenerate, RegenerateCgo, etc.. |
| 45 | explicit := map[protocol.CodeActionKind]bool{ |
| 46 | protocol.GoTest: true, |
| 47 | } |
| 48 | |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 49 | if len(params.Context.Only) == 0 { |
Rebecca Stambler | 1dcc99b | 2019-08-05 19:27:28 -0400 | [diff] [blame] | 50 | wanted = supportedCodeActions |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 51 | } else { |
| 52 | wanted = make(map[protocol.CodeActionKind]bool) |
| 53 | for _, only := range params.Context.Only { |
Suzy Mueller | 5e46725 | 2021-11-05 16:11:58 -0400 | [diff] [blame] | 54 | for k, v := range supportedCodeActions { |
| 55 | if only == k || strings.HasPrefix(string(k), string(only)+".") { |
| 56 | wanted[k] = wanted[k] || v |
| 57 | } |
| 58 | } |
| 59 | wanted[only] = wanted[only] || explicit[only] |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 60 | } |
| 61 | } |
Peter Weinbergr | 7cab0ef | 2021-03-02 10:16:22 -0500 | [diff] [blame] | 62 | if len(supportedCodeActions) == 0 { |
| 63 | return nil, nil // not an error if there are none supported |
| 64 | } |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 65 | if len(wanted) == 0 { |
Rebecca Stambler | a7c0594 | 2020-03-30 12:45:15 -0400 | [diff] [blame] | 66 | return nil, fmt.Errorf("no supported code action to execute for %s, wanted %v", uri, params.Context.Only) |
Rebecca Stambler | e750c41 | 2019-06-27 21:26:42 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Rebecca Stambler | ccd3732 | 2019-04-05 15:56:08 -0400 | [diff] [blame] | 69 | var codeActions []protocol.CodeAction |
pjw | 68b574a | 2022-01-07 19:06:25 -0500 | [diff] [blame] | 70 | switch kind { |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 71 | case source.Mod: |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 72 | if diagnostics := params.Context.Diagnostics; len(diagnostics) > 0 { |
Heschi Kreinick | 376db57 | 2021-03-02 18:19:32 -0500 | [diff] [blame] | 73 | diags, err := mod.DiagnosticsForMod(ctx, snapshot, fh) |
Rebecca Stambler | 5bd0538 | 2020-10-07 01:28:22 -0400 | [diff] [blame] | 74 | if source.IsNonFatalGoModError(err) { |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 75 | return nil, nil |
| 76 | } |
Rebecca Stambler | 504fe87 | 2020-04-01 21:31:43 -0400 | [diff] [blame] | 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 80 | quickFixes, err := codeActionsMatchingDiagnostics(ctx, snapshot, diagnostics, diags) |
Heschi Kreinick | 376db57 | 2021-03-02 18:19:32 -0500 | [diff] [blame] | 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | codeActions = append(codeActions, quickFixes...) |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 85 | } |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 86 | case source.Go: |
Rebecca Stambler | ab2804f | 2020-04-29 17:49:22 -0400 | [diff] [blame] | 87 | // Don't suggest fixes for generated files, since they are generally |
| 88 | // not useful and some editors may apply them automatically on save. |
| 89 | if source.IsGenerated(ctx, snapshot, uri) { |
| 90 | return nil, nil |
| 91 | } |
Rohan Challa | e46a7b9 | 2020-03-15 15:41:57 -0400 | [diff] [blame] | 92 | diagnostics := params.Context.Diagnostics |
| 93 | |
Rebecca Stambler | 9d5940d | 2020-02-26 17:07:02 -0500 | [diff] [blame] | 94 | // First, process any missing imports and pair them with the |
| 95 | // diagnostics they fix. |
| 96 | if wantQuickFixes := wanted[protocol.QuickFix] && len(diagnostics) > 0; wantQuickFixes || wanted[protocol.SourceOrganizeImports] { |
| 97 | importEdits, importEditsPerFix, err := source.AllImportsFixes(ctx, snapshot, fh) |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 98 | if err != nil { |
Rebecca Stambler | d518495 | 2020-07-16 20:00:10 -0400 | [diff] [blame] | 99 | event.Error(ctx, "imports fixes", err, tag.File.Of(fh.URI().Filename())) |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 100 | } |
Rebecca Stambler | 9d5940d | 2020-02-26 17:07:02 -0500 | [diff] [blame] | 101 | // Separate this into a set of codeActions per diagnostic, where |
| 102 | // each action is the addition, removal, or renaming of one import. |
| 103 | if wantQuickFixes { |
| 104 | for _, importFix := range importEditsPerFix { |
| 105 | fixes := importDiagnostics(importFix.Fix, diagnostics) |
| 106 | if len(fixes) == 0 { |
| 107 | continue |
| 108 | } |
| 109 | codeActions = append(codeActions, protocol.CodeAction{ |
| 110 | Title: importFixTitle(importFix.Fix), |
| 111 | Kind: protocol.QuickFix, |
| 112 | Edit: protocol.WorkspaceEdit{ |
| 113 | DocumentChanges: documentChanges(fh, importFix.Edits), |
| 114 | }, |
| 115 | Diagnostics: fixes, |
| 116 | }) |
| 117 | } |
| 118 | } |
Heschi Kreinick | bd5d160 | 2020-11-10 18:20:37 -0500 | [diff] [blame] | 119 | |
Rebecca Stambler | 9d5940d | 2020-02-26 17:07:02 -0500 | [diff] [blame] | 120 | // Send all of the import edits as one code action if the file is |
| 121 | // being organized. |
| 122 | if wanted[protocol.SourceOrganizeImports] && len(importEdits) > 0 { |
| 123 | codeActions = append(codeActions, protocol.CodeAction{ |
| 124 | Title: "Organize Imports", |
| 125 | Kind: protocol.SourceOrganizeImports, |
| 126 | Edit: protocol.WorkspaceEdit{ |
| 127 | DocumentChanges: documentChanges(fh, importEdits), |
| 128 | }, |
| 129 | }) |
| 130 | } |
| 131 | } |
Rebecca Stambler | 9d5940d | 2020-02-26 17:07:02 -0500 | [diff] [blame] | 132 | if ctx.Err() != nil { |
| 133 | return nil, ctx.Err() |
Rohan Challa | e46a7b9 | 2020-03-15 15:41:57 -0400 | [diff] [blame] | 134 | } |
Danish Dua | 8d73f17 | 2020-09-24 17:25:18 -0400 | [diff] [blame] | 135 | pkg, err := snapshot.PackageForFile(ctx, fh.URI(), source.TypecheckFull, source.WidestPackage) |
Rebecca Stambler | e31b568 | 2020-06-12 17:10:06 -0400 | [diff] [blame] | 136 | if err != nil { |
| 137 | return nil, err |
| 138 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 139 | |
| 140 | pkgDiagnostics, err := snapshot.DiagnosePackage(ctx, pkg) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | analysisDiags, err := source.Analyze(ctx, snapshot, pkg, true) |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | fileDiags := append(pkgDiagnostics[uri], analysisDiags[uri]...) |
Heschi Kreinick | 376db57 | 2021-03-02 18:19:32 -0500 | [diff] [blame] | 149 | |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 150 | // Split diagnostics into fixes, which must match incoming diagnostics, |
| 151 | // and non-fixes, which must match the requested range. Build actions |
| 152 | // for all of them. |
| 153 | var fixDiags, nonFixDiags []*source.Diagnostic |
| 154 | for _, d := range fileDiags { |
| 155 | if len(d.SuggestedFixes) == 0 { |
| 156 | continue |
| 157 | } |
Rebecca Stambler | 09a00c1 | 2021-03-18 12:12:50 -1000 | [diff] [blame] | 158 | var isFix bool |
| 159 | for _, fix := range d.SuggestedFixes { |
| 160 | if fix.ActionKind == protocol.QuickFix || fix.ActionKind == protocol.SourceFixAll { |
| 161 | isFix = true |
| 162 | break |
| 163 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 164 | } |
Rebecca Stambler | 09a00c1 | 2021-03-18 12:12:50 -1000 | [diff] [blame] | 165 | if isFix { |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 166 | fixDiags = append(fixDiags, d) |
| 167 | } else { |
| 168 | nonFixDiags = append(nonFixDiags, d) |
Rohan Challa | 6fdc577 | 2020-01-22 14:23:12 -0500 | [diff] [blame] | 169 | } |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 170 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 171 | |
| 172 | fixActions, err := codeActionsMatchingDiagnostics(ctx, snapshot, diagnostics, fixDiags) |
| 173 | if err != nil { |
| 174 | return nil, err |
Pei Xian Chee | 9b20fe4 | 2020-06-03 18:56:29 +0000 | [diff] [blame] | 175 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 176 | codeActions = append(codeActions, fixActions...) |
| 177 | |
| 178 | for _, nonfix := range nonFixDiags { |
| 179 | // For now, only show diagnostics for matching lines. Maybe we should |
| 180 | // alter this behavior in the future, depending on the user experience. |
| 181 | if !protocol.Intersect(nonfix.Range, params.Range) { |
| 182 | continue |
| 183 | } |
| 184 | actions, err := codeActionsForDiagnostic(ctx, snapshot, nonfix, nil) |
Rebecca Stambler | e31b568 | 2020-06-12 17:10:06 -0400 | [diff] [blame] | 185 | if err != nil { |
| 186 | return nil, err |
| 187 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 188 | codeActions = append(codeActions, actions...) |
Rebecca Stambler | e31b568 | 2020-06-12 17:10:06 -0400 | [diff] [blame] | 189 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 190 | |
Josh Baum | 9c9572d | 2020-06-24 09:52:23 -0400 | [diff] [blame] | 191 | if wanted[protocol.RefactorExtract] { |
Heschi Kreinick | b647668 | 2020-07-22 11:32:32 -0400 | [diff] [blame] | 192 | fixes, err := extractionFixes(ctx, snapshot, pkg, uri, params.Range) |
Josh Baum | 9c9572d | 2020-06-24 09:52:23 -0400 | [diff] [blame] | 193 | if err != nil { |
| 194 | return nil, err |
| 195 | } |
| 196 | codeActions = append(codeActions, fixes...) |
| 197 | } |
Pontus Leitzler | 8c26973 | 2020-10-03 14:12:29 +0200 | [diff] [blame] | 198 | |
| 199 | if wanted[protocol.GoTest] { |
| 200 | fixes, err := goTest(ctx, snapshot, uri, params.Range) |
| 201 | if err != nil { |
| 202 | return nil, err |
| 203 | } |
| 204 | codeActions = append(codeActions, fixes...) |
| 205 | } |
| 206 | |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 207 | default: |
| 208 | // Unsupported file kind for a code action. |
| 209 | return nil, nil |
| 210 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 211 | |
| 212 | var filtered []protocol.CodeAction |
| 213 | for _, action := range codeActions { |
| 214 | if wanted[action.Kind] { |
| 215 | filtered = append(filtered, action) |
| 216 | } |
| 217 | } |
| 218 | return filtered, nil |
Rebecca Stambler | ccd3732 | 2019-04-05 15:56:08 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 221 | func (s *Server) getSupportedCodeActions() []protocol.CodeActionKind { |
| 222 | allCodeActionKinds := make(map[protocol.CodeActionKind]struct{}) |
| 223 | for _, kinds := range s.session.Options().SupportedCodeActions { |
| 224 | for kind := range kinds { |
| 225 | allCodeActionKinds[kind] = struct{}{} |
| 226 | } |
| 227 | } |
| 228 | var result []protocol.CodeActionKind |
| 229 | for kind := range allCodeActionKinds { |
| 230 | result = append(result, kind) |
| 231 | } |
| 232 | sort.Slice(result, func(i, j int) bool { |
| 233 | return result[i] < result[j] |
| 234 | }) |
| 235 | return result |
| 236 | } |
| 237 | |
Suzy Mueller | 5f95ed5 | 2019-07-30 14:00:02 -0400 | [diff] [blame] | 238 | func importFixTitle(fix *imports.ImportFix) string { |
| 239 | var str string |
| 240 | switch fix.FixType { |
| 241 | case imports.AddImport: |
| 242 | str = fmt.Sprintf("Add import: %s %q", fix.StmtInfo.Name, fix.StmtInfo.ImportPath) |
| 243 | case imports.DeleteImport: |
| 244 | str = fmt.Sprintf("Delete import: %s %q", fix.StmtInfo.Name, fix.StmtInfo.ImportPath) |
| 245 | case imports.SetImportName: |
| 246 | str = fmt.Sprintf("Rename import: %s %q", fix.StmtInfo.Name, fix.StmtInfo.ImportPath) |
| 247 | } |
| 248 | return str |
| 249 | } |
| 250 | |
| 251 | func importDiagnostics(fix *imports.ImportFix, diagnostics []protocol.Diagnostic) (results []protocol.Diagnostic) { |
| 252 | for _, diagnostic := range diagnostics { |
| 253 | switch { |
| 254 | // "undeclared name: X" may be an unresolved import. |
| 255 | case strings.HasPrefix(diagnostic.Message, "undeclared name: "): |
| 256 | ident := strings.TrimPrefix(diagnostic.Message, "undeclared name: ") |
| 257 | if ident == fix.IdentName { |
| 258 | results = append(results, diagnostic) |
| 259 | } |
| 260 | // "could not import: X" may be an invalid import. |
| 261 | case strings.HasPrefix(diagnostic.Message, "could not import: "): |
| 262 | ident := strings.TrimPrefix(diagnostic.Message, "could not import: ") |
| 263 | if ident == fix.IdentName { |
| 264 | results = append(results, diagnostic) |
| 265 | } |
| 266 | // "X imported but not used" is an unused import. |
| 267 | // "X imported but not used as Y" is an unused import. |
| 268 | case strings.Contains(diagnostic.Message, " imported but not used"): |
| 269 | idx := strings.Index(diagnostic.Message, " imported but not used") |
| 270 | importPath := diagnostic.Message[:idx] |
| 271 | if importPath == fmt.Sprintf("%q", fix.StmtInfo.ImportPath) { |
| 272 | results = append(results, diagnostic) |
| 273 | } |
| 274 | } |
Suzy Mueller | 5f95ed5 | 2019-07-30 14:00:02 -0400 | [diff] [blame] | 275 | } |
Suzy Mueller | 5f95ed5 | 2019-07-30 14:00:02 -0400 | [diff] [blame] | 276 | return results |
| 277 | } |
| 278 | |
Heschi Kreinick | b647668 | 2020-07-22 11:32:32 -0400 | [diff] [blame] | 279 | func extractionFixes(ctx context.Context, snapshot source.Snapshot, pkg source.Package, uri span.URI, rng protocol.Range) ([]protocol.CodeAction, error) { |
Rebecca Stambler | 9267083 | 2020-07-23 23:24:36 -0400 | [diff] [blame] | 280 | if rng.Start == rng.End { |
Josh Baum | 9c9572d | 2020-06-24 09:52:23 -0400 | [diff] [blame] | 281 | return nil, nil |
| 282 | } |
Rebecca Stambler | 9267083 | 2020-07-23 23:24:36 -0400 | [diff] [blame] | 283 | fh, err := snapshot.GetFile(ctx, uri) |
| 284 | if err != nil { |
| 285 | return nil, err |
| 286 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 287 | _, pgf, err := source.GetParsedFile(ctx, snapshot, fh, source.NarrowestPackage) |
| 288 | if err != nil { |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 289 | return nil, fmt.Errorf("getting file for Identifier: %w", err) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 290 | } |
| 291 | srng, err := pgf.Mapper.RangeToSpanRange(rng) |
| 292 | if err != nil { |
| 293 | return nil, err |
| 294 | } |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 295 | puri := protocol.URIFromSpanURI(uri) |
| 296 | var commands []protocol.Command |
Alan Donovan | b929f3b | 2022-06-01 15:20:56 -0400 | [diff] [blame] | 297 | if _, ok, methodOk, _ := source.CanExtractFunction(pgf.Tok, srng, pgf.Src, pgf.File); ok { |
Suzy Mueller | 46d1522 | 2021-06-22 05:14:28 -0400 | [diff] [blame] | 298 | cmd, err := command.NewApplyFixCommand("Extract function", command.ApplyFixArgs{ |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 299 | URI: puri, |
| 300 | Fix: source.ExtractFunction, |
| 301 | Range: rng, |
| 302 | }) |
| 303 | if err != nil { |
| 304 | return nil, err |
| 305 | } |
| 306 | commands = append(commands, cmd) |
Suzy Mueller | 46d1522 | 2021-06-22 05:14:28 -0400 | [diff] [blame] | 307 | if methodOk { |
| 308 | cmd, err := command.NewApplyFixCommand("Extract method", command.ApplyFixArgs{ |
| 309 | URI: puri, |
| 310 | Fix: source.ExtractMethod, |
| 311 | Range: rng, |
| 312 | }) |
| 313 | if err != nil { |
| 314 | return nil, err |
| 315 | } |
| 316 | commands = append(commands, cmd) |
| 317 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 318 | } |
| 319 | if _, _, ok, _ := source.CanExtractVariable(srng, pgf.File); ok { |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 320 | cmd, err := command.NewApplyFixCommand("Extract variable", command.ApplyFixArgs{ |
| 321 | URI: puri, |
| 322 | Fix: source.ExtractVariable, |
| 323 | Range: rng, |
| 324 | }) |
| 325 | if err != nil { |
| 326 | return nil, err |
| 327 | } |
| 328 | commands = append(commands, cmd) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 329 | } |
Josh Baum | 6d307ed | 2020-07-10 12:13:40 -0400 | [diff] [blame] | 330 | var actions []protocol.CodeAction |
Suzy Mueller | ae0deb7 | 2021-06-24 11:53:41 -0400 | [diff] [blame] | 331 | for i := range commands { |
Josh Baum | 6d307ed | 2020-07-10 12:13:40 -0400 | [diff] [blame] | 332 | actions = append(actions, protocol.CodeAction{ |
Suzy Mueller | ae0deb7 | 2021-06-24 11:53:41 -0400 | [diff] [blame] | 333 | Title: commands[i].Title, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 334 | Kind: protocol.RefactorExtract, |
Suzy Mueller | ae0deb7 | 2021-06-24 11:53:41 -0400 | [diff] [blame] | 335 | Command: &commands[i], |
Josh Baum | 6d307ed | 2020-07-10 12:13:40 -0400 | [diff] [blame] | 336 | }) |
| 337 | } |
| 338 | return actions, nil |
Josh Baum | 9c9572d | 2020-06-24 09:52:23 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Heschi Kreinick | c9619e8 | 2020-07-26 18:01:39 -0400 | [diff] [blame] | 341 | func documentChanges(fh source.VersionedFileHandle, edits []protocol.TextEdit) []protocol.TextDocumentEdit { |
Rebecca Stambler | e33b02e | 2019-11-12 17:58:37 -0500 | [diff] [blame] | 342 | return []protocol.TextDocumentEdit{ |
| 343 | { |
pjw | c3402e3 | 2021-01-24 14:00:12 -0500 | [diff] [blame] | 344 | TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ |
Heschi Kreinick | ecd3fc4 | 2020-06-08 15:21:24 -0400 | [diff] [blame] | 345 | Version: fh.Version(), |
Rebecca Stambler | e33b02e | 2019-11-12 17:58:37 -0500 | [diff] [blame] | 346 | TextDocumentIdentifier: protocol.TextDocumentIdentifier{ |
Heschi Kreinick | ecd3fc4 | 2020-06-08 15:21:24 -0400 | [diff] [blame] | 347 | URI: protocol.URIFromSpanURI(fh.URI()), |
Rebecca Stambler | e33b02e | 2019-11-12 17:58:37 -0500 | [diff] [blame] | 348 | }, |
| 349 | }, |
| 350 | Edits: edits, |
| 351 | }, |
| 352 | } |
| 353 | } |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 354 | |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 355 | func codeActionsMatchingDiagnostics(ctx context.Context, snapshot source.Snapshot, pdiags []protocol.Diagnostic, sdiags []*source.Diagnostic) ([]protocol.CodeAction, error) { |
| 356 | var actions []protocol.CodeAction |
| 357 | for _, sd := range sdiags { |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 358 | var diag *protocol.Diagnostic |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 359 | for _, pd := range pdiags { |
| 360 | if sameDiagnostic(pd, sd) { |
| 361 | diag = &pd |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 362 | break |
| 363 | } |
| 364 | } |
| 365 | if diag == nil { |
| 366 | continue |
| 367 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 368 | diagActions, err := codeActionsForDiagnostic(ctx, snapshot, sd, diag) |
| 369 | if err != nil { |
| 370 | return nil, err |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 371 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 372 | actions = append(actions, diagActions...) |
| 373 | |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 374 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 375 | return actions, nil |
| 376 | } |
| 377 | |
| 378 | func codeActionsForDiagnostic(ctx context.Context, snapshot source.Snapshot, sd *source.Diagnostic, pd *protocol.Diagnostic) ([]protocol.CodeAction, error) { |
| 379 | var actions []protocol.CodeAction |
| 380 | for _, fix := range sd.SuggestedFixes { |
Rebecca Stambler | 09a00c1 | 2021-03-18 12:12:50 -1000 | [diff] [blame] | 381 | var changes []protocol.TextDocumentEdit |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 382 | for uri, edits := range fix.Edits { |
| 383 | fh, err := snapshot.GetVersionedFile(ctx, uri) |
| 384 | if err != nil { |
| 385 | return nil, err |
| 386 | } |
Rebecca Stambler | 09a00c1 | 2021-03-18 12:12:50 -1000 | [diff] [blame] | 387 | changes = append(changes, protocol.TextDocumentEdit{ |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 388 | TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ |
| 389 | Version: fh.Version(), |
| 390 | TextDocumentIdentifier: protocol.TextDocumentIdentifier{ |
| 391 | URI: protocol.URIFromSpanURI(uri), |
| 392 | }, |
| 393 | }, |
| 394 | Edits: edits, |
| 395 | }) |
| 396 | } |
Rebecca Stambler | 09a00c1 | 2021-03-18 12:12:50 -1000 | [diff] [blame] | 397 | action := protocol.CodeAction{ |
| 398 | Title: fix.Title, |
| 399 | Kind: fix.ActionKind, |
| 400 | Edit: protocol.WorkspaceEdit{ |
| 401 | DocumentChanges: changes, |
| 402 | }, |
| 403 | Command: fix.Command, |
| 404 | } |
| 405 | if pd != nil { |
| 406 | action.Diagnostics = []protocol.Diagnostic{*pd} |
| 407 | } |
Heschi Kreinick | 11e8f6b | 2021-03-09 12:23:42 -0500 | [diff] [blame] | 408 | actions = append(actions, action) |
| 409 | } |
| 410 | return actions, nil |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 411 | } |
Rebecca Stambler | d459050 | 2021-02-04 18:12:25 -0500 | [diff] [blame] | 412 | |
Heschi Kreinick | 51ce837 | 2021-01-28 20:03:37 -0500 | [diff] [blame] | 413 | func sameDiagnostic(pd protocol.Diagnostic, sd *source.Diagnostic) bool { |
Heschi Kreinick | e7dfe02 | 2021-01-28 21:08:30 -0500 | [diff] [blame] | 414 | return pd.Message == sd.Message && protocol.CompareRange(pd.Range, sd.Range) == 0 && pd.Source == string(sd.Source) |
Rebecca Stambler | 6123e77 | 2020-07-20 16:56:12 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Pontus Leitzler | 8c26973 | 2020-10-03 14:12:29 +0200 | [diff] [blame] | 417 | func goTest(ctx context.Context, snapshot source.Snapshot, uri span.URI, rng protocol.Range) ([]protocol.CodeAction, error) { |
| 418 | fh, err := snapshot.GetFile(ctx, uri) |
| 419 | if err != nil { |
| 420 | return nil, err |
| 421 | } |
| 422 | fns, err := source.TestsAndBenchmarks(ctx, snapshot, fh) |
| 423 | if err != nil { |
| 424 | return nil, err |
| 425 | } |
| 426 | |
| 427 | var tests, benchmarks []string |
| 428 | for _, fn := range fns.Tests { |
| 429 | if !protocol.Intersect(fn.Rng, rng) { |
| 430 | continue |
| 431 | } |
| 432 | tests = append(tests, fn.Name) |
| 433 | } |
| 434 | for _, fn := range fns.Benchmarks { |
| 435 | if !protocol.Intersect(fn.Rng, rng) { |
| 436 | continue |
| 437 | } |
| 438 | benchmarks = append(benchmarks, fn.Name) |
| 439 | } |
| 440 | |
| 441 | if len(tests) == 0 && len(benchmarks) == 0 { |
| 442 | return nil, nil |
| 443 | } |
| 444 | |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 445 | cmd, err := command.NewTestCommand("Run tests and benchmarks", protocol.URIFromSpanURI(uri), tests, benchmarks) |
Pontus Leitzler | 8c26973 | 2020-10-03 14:12:29 +0200 | [diff] [blame] | 446 | if err != nil { |
| 447 | return nil, err |
| 448 | } |
| 449 | return []protocol.CodeAction{{ |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 450 | Title: cmd.Title, |
| 451 | Kind: protocol.GoTest, |
| 452 | Command: &cmd, |
Pontus Leitzler | 8c26973 | 2020-10-03 14:12:29 +0200 | [diff] [blame] | 453 | }}, nil |
| 454 | } |