CppEditor: Introduce central function for registering quickfixes

... and use it to register the InsertVirtualMethods quickfix.
The resulting registration pattern encourages the presence of tests and
easily allows minimal visibility of the classes involved.

Change-Id: I85fba0b983b51d84b6fae1f6fe51b63eed0ee336
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2024-05-14 11:06:46 +02:00
parent ce16d58214
commit 6a3f66af85
6 changed files with 59 additions and 22 deletions

View File

@@ -43,7 +43,6 @@
#include "functionutils.h"
#include "includeutils.h"
#include "projectinfo_test.h"
#include "quickfixes/cppinsertvirtualmethods.h"
#include "quickfixes/cppquickfix_test.h"
#include "symbolsearcher_test.h"
#include "typehierarchybuilder_test.h"
@@ -514,7 +513,6 @@ void CppEditorPlugin::registerTests()
addTest<Tests::FileAndTokenActionsTest>();
addTest<Tests::FollowSymbolTest>();
addTest<Tests::IncludeHierarchyTest>();
addTest<Tests::InsertVirtualMethodsTest>();
addTest<Tests::QuickfixTest>();
addTest<Tests::GlobalRenamingTest>();
addTest<Tests::SelectionsTest>();

View File

@@ -116,6 +116,11 @@ public:
QSortFilterProxyModel *classFunctionFilterModel;
};
void registerInsertVirtualMethodsQuickfix()
{
CppQuickFixFactory::registerFactory<InsertVirtualMethods>();
}
} // namespace Internal
} // namespace CppEditor
@@ -1269,6 +1274,17 @@ public:
void saveSettings() override { }
};
class InsertVirtualMethodsTest : public QObject
{
Q_OBJECT
private slots:
void test_data();
void test();
void testImplementationFile();
void testBaseClassInNamespace();
};
void InsertVirtualMethodsTest::test_data()
{
QTest::addColumn<InsertVirtualMethodsDialog::ImplementationMode>("implementationMode");
@@ -1969,6 +1985,11 @@ InsertVirtualMethods *InsertVirtualMethods::createTestFactory()
InsertVirtualMethodsDialog::ModeOutsideClass, true, false));
}
QObject *InsertVirtualMethods::createTest()
{
return new Tests::InsertVirtualMethodsTest;
}
#endif // WITH_TESTS
} // namespace Internal

View File

@@ -19,6 +19,7 @@ public:
void doMatch(const CppQuickFixInterface &interface,
TextEditor::QuickFixOperations &result) override;
#ifdef WITH_TESTS
static QObject *createTest();
static InsertVirtualMethods *createTestFactory();
#endif
@@ -26,20 +27,7 @@ private:
InsertVirtualMethodsDialog *m_dialog;
};
#ifdef WITH_TESTS
namespace Tests {
class InsertVirtualMethodsTest : public QObject
{
Q_OBJECT
private slots:
void test_data();
void test();
void testImplementationFile();
void testBaseClassInNamespace();
};
} // namespace Tests
#endif // WITH_TESTS
void registerInsertVirtualMethodsQuickfix();
} // namespace Internal
} // namespace CppEditor

View File

@@ -3,13 +3,33 @@
#include "cppquickfix.h"
#include "../cpprefactoringchanges.h"
#include "cppquickfixassistant.h"
#include <extensionsystem/pluginmanager.h>
#include <extensionsystem/pluginspec.h>
using namespace CPlusPlus;
using namespace TextEditor;
namespace CppEditor::Internal {
namespace CppEditor {
static ExtensionSystem::IPlugin *getCppEditor()
{
using namespace ExtensionSystem;
for (PluginSpec * const spec : PluginManager::plugins()) {
if (spec->name() == "CppEditor")
return spec->plugin();
}
QTC_ASSERT(false, return nullptr);
}
ExtensionSystem::IPlugin *CppQuickFixFactory::cppEditor()
{
static ExtensionSystem::IPlugin *plugin = getCppEditor();
return plugin;
}
namespace Internal {
const QStringList magicQObjectFunctions()
{
@@ -23,4 +43,5 @@ CppQuickFixOperation::CppQuickFixOperation(const CppQuickFixInterface &interface
CppQuickFixOperation::~CppQuickFixOperation() = default;
} // namespace CppEditor::Internal
} // namespace Internal
} // namespace CppEditor

View File

@@ -6,6 +6,7 @@
#include "../cppeditor_global.h"
#include "cppquickfixassistant.h"
#include <extensionsystem/iplugin.h>
#include <texteditor/quickfix.h>
#include <QVersionNumber>
@@ -59,6 +60,14 @@ public:
std::optional<QVersionNumber> clangdReplacement() const { return m_clangdReplacement; }
void setClangdReplacement(const QVersionNumber &version) { m_clangdReplacement = version; }
template<class Factory> static void registerFactory()
{
new Factory;
#ifdef WITH_TESTS
cppEditor()->addTestCreator(Factory::createTest);
#endif
}
private:
/*!
Implement this function to doMatch and create the appropriate
@@ -67,6 +76,8 @@ private:
virtual void doMatch(const Internal::CppQuickFixInterface &interface,
QuickFixOperations &result) = 0;
static ExtensionSystem::IPlugin *cppEditor();
std::optional<QVersionNumber> m_clangdReplacement;
};

View File

@@ -33,8 +33,6 @@
#include <cplusplus/TypeOfExpression.h>
#include <cplusplus/TypePrettyPrinter.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h>
@@ -10647,7 +10645,7 @@ void createCppQuickFixes()
new AssignToLocalVariable;
new InsertVirtualMethods;
registerInsertVirtualMethodsQuickfix();
new OptimizeForLoop;