for (const auto &T : *Tweaks)
Actions.push_back(toCodeAction(T, File, Selection));
+ // If there's exactly one quick-fix, call it "preferred".
+ // We never consider refactorings etc as preferred.
+ CodeAction *OnlyFix = nullptr;
+ for (auto &Action : Actions) {
+ if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) {
+ if (OnlyFix) {
+ OnlyFix->isPreferred = false;
+ break;
+ }
+ Action.isPreferred = true;
+ OnlyFix = &Action;
+ }
+ }
+
if (SupportsCodeAction)
return Reply(llvm::json::Array(Actions));
std::vector<Command> Commands;
Main.codeActions.emplace();
for (const auto &Fix : D.Fixes)
Main.codeActions->push_back(toCodeAction(Fix, File));
+ if (Main.codeActions->size() == 1)
+ Main.codeActions->front().isPreferred = true;
}
if (Opts.SendDiagnosticCategory && !D.Category.empty())
Main.category = D.Category;
CodeAction["kind"] = *CA.kind;
if (CA.diagnostics)
CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
+ if (CA.isPreferred)
+ CodeAction["isPreferred"] = true;
if (CA.edit)
CodeAction["edit"] = *CA.edit;
if (CA.command)
/// The diagnostics that this code action resolves.
llvm::Optional<std::vector<Diagnostic>> diagnostics;
+ /// Marks this as a preferred action. Preferred actions are used by the
+ /// `auto fix` command and can be targeted by keybindings.
+ /// A quick fix should be marked preferred if it properly addresses the
+ /// underlying error. A refactoring should be marked preferred if it is the
+ /// most reasonable choice of actions to take.
+ bool isPreferred = false;
+
/// The workspace edit this code action performs.
llvm::Optional<WorkspaceEdit> edit;
# CHECK-NEXT: ]
# CHECK-NEXT: }
# CHECK-NEXT: },
+# CHECK-NEXT: "isPreferred": true,
# CHECK-NEXT: "kind": "quickfix",
# CHECK-NEXT: "title": "change 'union' to 'struct'"
# CHECK-NEXT: }