| 1 | // Copyright 2020 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 settings |
| 6 | |
| 7 | import "golang.org/x/tools/gopls/internal/protocol" |
| 8 | |
| 9 | // This file defines constants for non-standard CodeActions. |
| 10 | |
| 11 | // CodeAction kinds specific to gopls |
| 12 | // |
| 13 | // See ../protocol/tsprotocol.go for LSP standard kinds, including |
| 14 | // |
| 15 | // quickfix |
| 16 | // refactor |
| 17 | // refactor.extract |
| 18 | // refactor.inline |
| 19 | // refactor.move |
| 20 | // refactor.rewrite |
| 21 | // source |
| 22 | // source.organizeImports |
| 23 | // source.fixAll |
| 24 | // notebook |
| 25 | // |
| 26 | // Kinds are hierarchical: "refactor" subsumes "refactor.inline", |
| 27 | // which subsumes "refactor.inline.call". This rule implies that the |
| 28 | // empty string, confusingly named protocol.Empty, subsumes all kinds. |
| 29 | // The "Only" field in a CodeAction request may specify a category |
| 30 | // such as "refactor"; any matching code action will be returned. |
| 31 | // |
| 32 | // All CodeActions returned by gopls use a specific leaf kind such as |
| 33 | // "refactor.inline.call", except for quick fixes, which all use |
| 34 | // "quickfix". TODO(adonovan): perhaps quick fixes should also be |
| 35 | // hierarchical (e.g. quickfix.govulncheck.{reset,upgrade})? |
| 36 | // |
| 37 | // # VS Code |
| 38 | // |
| 39 | // The effects of CodeActionKind on the behavior of VS Code are |
| 40 | // baffling and undocumented. Here's what we have observed. |
| 41 | // |
| 42 | // Clicking on the "Refactor..." menu item shows a submenu of actions |
| 43 | // with kind="refactor.*", and clicking on "Source action..." shows |
| 44 | // actions with kind="source.*". A lightbulb appears in both cases. |
| 45 | // |
| 46 | // A third menu, "Quick fix...", not found on the usual context |
| 47 | // menu but accessible through the command palette or "⌘.", |
| 48 | // does not set the Only field in its request, so the set of |
| 49 | // kinds is determined by how the server interprets the default. |
| 50 | // The LSP 3.18 guidance is that this should be treated |
| 51 | // equivalent to Only=["quickfix"], and that is what gopls |
| 52 | // now does. (If the server responds with more kinds, they will |
| 53 | // be displayed in menu subsections.) |
| 54 | // |
| 55 | // All of these CodeAction requests have triggerkind=Invoked. |
| 56 | // |
| 57 | // Cursor motion also performs a CodeAction request, but with |
| 58 | // triggerkind=Automatic. Even if this returns a mix of action kinds, |
| 59 | // only the "refactor" and "quickfix" actions seem to matter. |
| 60 | // A lightbulb appears if that subset of actions is non-empty, and the |
| 61 | // menu displays them. (This was noisy--see #65167--so gopls now only |
| 62 | // reports diagnostic-associated code actions if kind is Invoked or |
| 63 | // missing.) |
| 64 | // |
| 65 | // None of these CodeAction requests specifies a "kind" restriction; |
| 66 | // the filtering is done on the response, by the client. |
| 67 | // |
| 68 | // In all these menus, VS Code organizes the actions' menu items |
| 69 | // into groups based on their kind, with hardwired captions such as |
| 70 | // "Refactor...", "Extract", "Inline", "More actions", and "Quick fix". |
| 71 | // |
| 72 | // The special category "source.fixAll" is intended for actions that |
| 73 | // are unambiguously safe to apply so that clients may automatically |
| 74 | // apply all actions matching this category on save. (That said, this |
| 75 | // is not VS Code's default behavior; see editor.codeActionsOnSave.) |
| 76 | const ( |
| 77 | // source |
| 78 | GoAssembly protocol.CodeActionKind = "source.assembly" |
| 79 | GoDoc protocol.CodeActionKind = "source.doc" |
| 80 | GoFreeSymbols protocol.CodeActionKind = "source.freesymbols" |
| 81 | GoSplitPackage protocol.CodeActionKind = "source.splitPackage" |
| 82 | GoTest protocol.CodeActionKind = "source.test" |
| 83 | GoToggleCompilerOptDetails protocol.CodeActionKind = "source.toggleCompilerOptDetails" |
| 84 | AddTest protocol.CodeActionKind = "source.addTest" |
| 85 | OrganizeImports protocol.CodeActionKind = "source.organizeImports" |
| 86 | |
| 87 | // gopls |
| 88 | GoplsDocFeatures protocol.CodeActionKind = "gopls.doc.features" |
| 89 | |
| 90 | // refactor.rewrite |
| 91 | RefactorRewriteChangeQuote protocol.CodeActionKind = "refactor.rewrite.changeQuote" |
| 92 | RefactorRewriteFillStruct protocol.CodeActionKind = "refactor.rewrite.fillStruct" |
| 93 | RefactorRewriteFillSwitch protocol.CodeActionKind = "refactor.rewrite.fillSwitch" |
| 94 | RefactorRewriteInvertIf protocol.CodeActionKind = "refactor.rewrite.invertIf" |
| 95 | RefactorRewriteJoinLines protocol.CodeActionKind = "refactor.rewrite.joinLines" |
| 96 | RefactorRewriteRemoveUnusedParam protocol.CodeActionKind = "refactor.rewrite.removeUnusedParam" |
| 97 | RefactorRewriteMoveParamLeft protocol.CodeActionKind = "refactor.rewrite.moveParamLeft" |
| 98 | RefactorRewriteMoveParamRight protocol.CodeActionKind = "refactor.rewrite.moveParamRight" |
| 99 | RefactorRewriteSplitLines protocol.CodeActionKind = "refactor.rewrite.splitLines" |
| 100 | RefactorRewriteEliminateDotImport protocol.CodeActionKind = "refactor.rewrite.eliminateDotImport" |
| 101 | RefactorRewriteAddTags protocol.CodeActionKind = "refactor.rewrite.addTags" |
| 102 | RefactorRewriteImplementInterface protocol.CodeActionKind = "refactor.rewrite.implementInterface" |
| 103 | RefactorRewriteRemoveTags protocol.CodeActionKind = "refactor.rewrite.removeTags" |
| 104 | |
| 105 | // refactor.inline |
| 106 | RefactorInlineCall protocol.CodeActionKind = "refactor.inline.call" |
| 107 | RefactorInlineVariable protocol.CodeActionKind = "refactor.inline.variable" |
| 108 | |
| 109 | // refactor.extract |
| 110 | RefactorExtractConstant protocol.CodeActionKind = "refactor.extract.constant" |
| 111 | RefactorExtractConstantAll protocol.CodeActionKind = "refactor.extract.constant-all" |
| 112 | RefactorExtractFunction protocol.CodeActionKind = "refactor.extract.function" |
| 113 | RefactorExtractMethod protocol.CodeActionKind = "refactor.extract.method" |
| 114 | RefactorExtractVariable protocol.CodeActionKind = "refactor.extract.variable" |
| 115 | RefactorExtractVariableAll protocol.CodeActionKind = "refactor.extract.variable-all" |
| 116 | RefactorExtractToNewFile protocol.CodeActionKind = "refactor.extract.toNewFile" |
| 117 | |
| 118 | // refactor.move |
| 119 | RefactorMoveType protocol.CodeActionKind = "refactor.move.moveType" |
| 120 | |
| 121 | // Note: add new kinds to: |
| 122 | // - the SupportedCodeActions map in default.go |
| 123 | // - the codeActionProducers table in ../golang/codeaction.go |
| 124 | // - the docs in ../../doc/features/transformation.md |
| 125 | ) |