diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h --- a/clang-tools-extra/clangd/ClangdLSPServer.h +++ b/clang-tools-extra/clangd/ClangdLSPServer.h @@ -263,6 +263,9 @@ CompletionItemKindBitset SupportedCompletionItemKinds; /// Whether the client supports CodeAction response objects. bool SupportsCodeAction = false; + /// Whether the client supports isPreferred property on CodeAction response + /// objects. + bool SupportsCodeActionIsPreferred = false; /// From capabilities of textDocument/documentSymbol. bool SupportsHierarchicalDocumentSymbol = false; /// Whether the client supports showing file status. diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -532,6 +532,7 @@ if (Params.capabilities.CompletionItemKinds) SupportedCompletionItemKinds |= *Params.capabilities.CompletionItemKinds; SupportsCodeAction = Params.capabilities.CodeActionStructure; + SupportsCodeActionIsPreferred = Params.capabilities.CodeActionIsPreferred; SupportsHierarchicalDocumentSymbol = Params.capabilities.HierarchicalDocumentSymbol; SupportFileStatus = Params.initializationOptions.FileStatus; @@ -1029,15 +1030,17 @@ // 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; + if (SupportsCodeActionIsPreferred) { + 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; } - Action.isPreferred = true; - OnlyFix = &Action; } } diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h --- a/clang-tools-extra/clangd/Protocol.h +++ b/clang-tools-extra/clangd/Protocol.h @@ -445,6 +445,10 @@ /// textDocument.codeAction.codeActionLiteralSupport. bool CodeActionStructure = false; + /// Client supports isPreferred property on CodeActions. + /// textDocument.codeAction.isPreferredSupport. + bool CodeActionIsPreferred = false; + /// Client advertises support for the semanticTokens feature. /// We support the textDocument/semanticTokens request in any case. /// textDocument.semanticTokens diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp --- a/clang-tools-extra/clangd/Protocol.cpp +++ b/clang-tools-extra/clangd/Protocol.cpp @@ -346,6 +346,8 @@ if (auto *CodeAction = TextDocument->getObject("codeAction")) { if (CodeAction->getObject("codeActionLiteralSupport")) R.CodeActionStructure = true; + if (auto IsPreferred = CodeAction->getBoolean("isPreferredSupport")) + R.CodeActionIsPreferred = *IsPreferred; } if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) { if (auto HierarchicalSupport =