ClangCodeModel: Move quickfix functionality to dedicated set of files

Change-Id: I7cc55afa7aa50ba584593457b6c1393794866c56
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-07-07 15:21:31 +02:00
parent 14216fdf73
commit 1fb4737d0d
6 changed files with 82 additions and 72 deletions

View File

@@ -19,7 +19,7 @@ add_qtc_plugin(ClangCodeModel
clangdclient.cpp clangdclient.h clangdclient.cpp clangdclient.h
clangdfollowsymbol.cpp clangdfollowsymbol.h clangdfollowsymbol.cpp clangdfollowsymbol.h
clangdiagnostictooltipwidget.cpp clangdiagnostictooltipwidget.h clangdiagnostictooltipwidget.cpp clangdiagnostictooltipwidget.h
clangdquickfixfactory.cpp clangdquickfixfactory.h clangdquickfixes.cpp clangdquickfixes.h
clangdqpropertyhighlighter.cpp clangdqpropertyhighlighter.h clangdqpropertyhighlighter.cpp clangdqpropertyhighlighter.h
clangdsemantichighlighting.cpp clangdsemantichighlighting.h clangdsemantichighlighting.cpp clangdsemantichighlighting.h
clangdswitchdecldef.cpp clangdswitchdecldef.h clangdswitchdecldef.cpp clangdswitchdecldef.h

View File

@@ -42,8 +42,8 @@ QtcPlugin {
"clangdlocatorfilters.h", "clangdlocatorfilters.h",
"clangdqpropertyhighlighter.cpp", "clangdqpropertyhighlighter.cpp",
"clangdqpropertyhighlighter.h", "clangdqpropertyhighlighter.h",
"clangdquickfixfactory.cpp", "clangdquickfixes.cpp",
"clangdquickfixfactory.h", "clangdquickfixes.h",
"clangdsemantichighlighting.cpp", "clangdsemantichighlighting.cpp",
"clangdsemantichighlighting.h", "clangdsemantichighlighting.h",
"clangdswitchdecldef.cpp", "clangdswitchdecldef.cpp",

View File

@@ -30,6 +30,7 @@
#include "clangdast.h" #include "clangdast.h"
#include "clangdfollowsymbol.h" #include "clangdfollowsymbol.h"
#include "clangdlocatorfilters.h" #include "clangdlocatorfilters.h"
#include "clangdquickfixes.h"
#include "clangdswitchdecldef.h" #include "clangdswitchdecldef.h"
#include "clangpreprocessorassistproposalitem.h" #include "clangpreprocessorassistproposalitem.h"
#include "clangtextmark.h" #include "clangtextmark.h"
@@ -65,7 +66,6 @@
#include <languageclient/languageclienthoverhandler.h> #include <languageclient/languageclienthoverhandler.h>
#include <languageclient/languageclientinterface.h> #include <languageclient/languageclientinterface.h>
#include <languageclient/languageclientmanager.h> #include <languageclient/languageclientmanager.h>
#include <languageclient/languageclientquickfix.h>
#include <languageclient/languageclientsymbolsupport.h> #include <languageclient/languageclientsymbolsupport.h>
#include <languageclient/languageclientutils.h> #include <languageclient/languageclientutils.h>
#include <languageserverprotocol/clientcapabilities.h> #include <languageserverprotocol/clientcapabilities.h>
@@ -775,71 +775,6 @@ private:
ClangdClient * const m_client; ClangdClient * const m_client;
}; };
class ClangdQuickFixProcessor : public LanguageClientQuickFixAssistProcessor
{
public:
ClangdQuickFixProcessor(LanguageClient::Client *client)
: LanguageClientQuickFixAssistProcessor(client)
{
}
private:
IAssistProposal *perform(const AssistInterface *interface) override
{
m_interface = interface;
// Step 1: Collect clangd code actions asynchronously
LanguageClientQuickFixAssistProcessor::perform(interface);
// Step 2: Collect built-in quickfixes synchronously
m_builtinOps = CppEditor::quickFixOperations(interface);
return nullptr;
}
TextEditor::GenericProposal *handleCodeActionResult(const CodeActionResult &result) override
{
auto toOperation =
[=](const Utils::variant<Command, CodeAction> &item) -> QuickFixOperation * {
if (auto action = Utils::get_if<CodeAction>(&item)) {
const Utils::optional<QList<Diagnostic>> diagnostics = action->diagnostics();
if (!diagnostics.has_value() || diagnostics->isEmpty())
return new CodeActionQuickFixOperation(*action, client());
}
if (auto command = Utils::get_if<Command>(&item))
return new CommandQuickFixOperation(*command, client());
return nullptr;
};
if (auto list = Utils::get_if<QList<Utils::variant<Command, CodeAction>>>(&result)) {
QuickFixOperations ops;
for (const Utils::variant<Command, CodeAction> &item : *list) {
if (QuickFixOperation *op = toOperation(item)) {
op->setDescription("clangd: " + op->description());
ops << op;
}
}
return GenericProposal::createProposal(m_interface, ops + m_builtinOps);
}
return nullptr;
}
QuickFixOperations m_builtinOps;
const AssistInterface *m_interface = nullptr;
};
class ClangdQuickFixProvider : public LanguageClientQuickFixProvider
{
public:
ClangdQuickFixProvider(ClangdClient *client) : LanguageClientQuickFixProvider(client) {};
private:
IAssistProcessor *createProcessor(const TextEditor::AssistInterface *) const override
{
return new ClangdQuickFixProcessor(client());
}
};
static void addToCompilationDb(QJsonObject &cdb, static void addToCompilationDb(QJsonObject &cdb,
const CppEditor::ProjectPart &projectPart, const CppEditor::ProjectPart &projectPart,
CppEditor::UsePrecompiledHeaders usePch, CppEditor::UsePrecompiledHeaders usePch,

View File

@@ -23,14 +23,16 @@
** **
****************************************************************************/ ****************************************************************************/
#include "clangdquickfixfactory.h" #include "clangdquickfixes.h"
#include "clangdclient.h" #include "clangdclient.h"
#include "clangmodelmanagersupport.h" #include "clangmodelmanagersupport.h"
#include <languageclient/languageclientquickfix.h> #include <texteditor/codeassist/genericproposal.h>
using namespace LanguageClient;
using namespace LanguageServerProtocol; using namespace LanguageServerProtocol;
using namespace TextEditor;
namespace ClangCodeModel { namespace ClangCodeModel {
namespace Internal { namespace Internal {
@@ -58,5 +60,66 @@ void ClangdQuickFixFactory::match(const CppEditor::Internal::CppQuickFixInterfac
} }
} }
class ClangdQuickFixProcessor : public LanguageClientQuickFixAssistProcessor
{
public:
ClangdQuickFixProcessor(LanguageClient::Client *client)
: LanguageClientQuickFixAssistProcessor(client)
{
}
private:
IAssistProposal *perform(const AssistInterface *interface) override
{
m_interface = interface;
// Step 1: Collect clangd code actions asynchronously
LanguageClientQuickFixAssistProcessor::perform(interface);
// Step 2: Collect built-in quickfixes synchronously
m_builtinOps = CppEditor::quickFixOperations(interface);
return nullptr;
}
TextEditor::GenericProposal *handleCodeActionResult(const CodeActionResult &result) override
{
auto toOperation =
[=](const Utils::variant<Command, CodeAction> &item) -> QuickFixOperation * {
if (auto action = Utils::get_if<CodeAction>(&item)) {
const Utils::optional<QList<Diagnostic>> diagnostics = action->diagnostics();
if (!diagnostics.has_value() || diagnostics->isEmpty())
return new CodeActionQuickFixOperation(*action, client());
}
if (auto command = Utils::get_if<Command>(&item))
return new CommandQuickFixOperation(*command, client());
return nullptr;
};
if (auto list = Utils::get_if<QList<Utils::variant<Command, CodeAction>>>(&result)) {
QuickFixOperations ops;
for (const Utils::variant<Command, CodeAction> &item : *list) {
if (QuickFixOperation *op = toOperation(item)) {
op->setDescription("clangd: " + op->description());
ops << op;
}
}
return GenericProposal::createProposal(m_interface, ops + m_builtinOps);
}
return nullptr;
}
QuickFixOperations m_builtinOps;
const AssistInterface *m_interface = nullptr;
};
ClangdQuickFixProvider::ClangdQuickFixProvider(ClangdClient *client)
: LanguageClientQuickFixProvider(client) {}
IAssistProcessor *ClangdQuickFixProvider::createProcessor(const AssistInterface *) const
{
return new ClangdQuickFixProcessor(client());
}
} // namespace Internal } // namespace Internal
} // namespace ClangCodeModel } // namespace ClangCodeModel

View File

@@ -26,9 +26,11 @@
#pragma once #pragma once
#include <cppeditor/cppquickfix.h> #include <cppeditor/cppquickfix.h>
#include <languageclient/languageclientquickfix.h>
namespace ClangCodeModel { namespace ClangCodeModel {
namespace Internal { namespace Internal {
class ClangdClient;
class ClangdQuickFixFactory : public CppEditor::CppQuickFixFactory class ClangdQuickFixFactory : public CppEditor::CppQuickFixFactory
{ {
@@ -39,5 +41,15 @@ public:
QuickFixOperations &result) override; QuickFixOperations &result) override;
}; };
class ClangdQuickFixProvider : public LanguageClient::LanguageClientQuickFixProvider
{
public:
ClangdQuickFixProvider(ClangdClient *client);
private:
TextEditor::IAssistProcessor *createProcessor(
const TextEditor::AssistInterface *) const override;
};
} // namespace Internal } // namespace Internal
} // namespace ClangCodeModel } // namespace ClangCodeModel

View File

@@ -27,7 +27,7 @@
#include "clangconstants.h" #include "clangconstants.h"
#include "clangdclient.h" #include "clangdclient.h"
#include "clangdquickfixfactory.h" #include "clangdquickfixes.h"
#include "clangeditordocumentprocessor.h" #include "clangeditordocumentprocessor.h"
#include "clangdlocatorfilters.h" #include "clangdlocatorfilters.h"
#include "clangutils.h" #include "clangutils.h"