From 135e76f965618747b68ab1caae5768feda5fcee0 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 20 Oct 2020 12:49:12 +0200 Subject: CppEditor: Offer InsertDefsOperation quickfix for all classes The check whether there are unimplemented member functions takes quite long, so Creator would freeze for several seconds when right-clicking on the name of even a medium-sized class. Therefore, we offer the operation for all classes with member functions and move the expensive check into the perform() method. Change-Id: Ie19958ba8c53493be859f9982d7d5697e6e9d88b Reviewed-by: Christian Stenger --- src/plugins/cppeditor/cppquickfix_test.cpp | 17 +++++++++++++++++ src/plugins/cppeditor/cppquickfixes.cpp | 22 +++++++++++++--------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 1dbfeafedf..e5257df4c0 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -3653,6 +3653,23 @@ void CppEditorPlugin::test_quickfix_InsertDefsFromDecls_data() } // namespace N)"; QTest::addRow("no candidates") + << QByteArrayList{origHeader, origHeader} + << QByteArrayList{origSource, origSource} + << int(InsertDefsFromDecls::Mode::Alternating); + + origHeader = R"( + namespace N { + class @C + { + public: + friend void ignoredFriend(); + void ignoredImplemented() {}; + + signals: + void ignoredSignal(); + }; + } // namespace N)"; + QTest::addRow("no member functions") << QByteArrayList{origHeader, ""} << QByteArrayList{origSource, ""} << int(InsertDefsFromDecls::Mode::Alternating); diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 50d0da5aff..2265d2ea5c 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -3479,12 +3479,11 @@ public: m_classAST = path.at(path.size() - 2)->asClassSpecifier(); if (!m_classAST) return; - const Class * const theClass = m_classAST->symbol; if (!theClass) return; - // Collect all member functions without an implementation. + // Collect all member functions. for (auto it = theClass->memberBegin(); it != theClass->memberEnd(); ++it) { Symbol * const s = *it; if (!s->identifier() || !s->type() || !s->isDeclaration() || s->asFunction()) @@ -3492,8 +3491,6 @@ public: Function * const func = s->type()->asFunctionType(); if (!func || func->isSignal() || func->isFriend()) continue; - if (SymbolFinder().findMatchingDefinition(s, interface.snapshot())) - continue; m_declarations << s; } } @@ -3504,7 +3501,14 @@ public: private: void perform() override { - QTC_ASSERT(!m_declarations.isEmpty(), return); + QList unimplemented; + SymbolFinder symbolFinder; + for (Symbol * const s : qAsConst(m_declarations)) { + if (!symbolFinder.findMatchingDefinition(s, snapshot())) + unimplemented << s; + } + if (unimplemented.isEmpty()) + return; CppRefactoringChanges refactoring(snapshot()); const bool isHeaderFile = ProjectFile::isHeader(ProjectFile::classify(filePath().toString())); @@ -3512,7 +3516,7 @@ private: if (isHeaderFile) { InsertionPointLocator locator(refactoring); for (const InsertionLocation &location - : locator.methodDefinition(m_declarations.first(), false, {})) { + : locator.methodDefinition(unimplemented.first(), false, {})) { if (!location.isValid()) continue; const QString fileName = location.fileName(); @@ -3530,7 +3534,7 @@ private: MemberFunctionImplSettings settings; switch (m_mode) { case InsertDefsFromDecls::Mode::User: { - AddImplementationsDialog dlg(m_declarations, Utils::FilePath::fromString(cppFile)); + AddImplementationsDialog dlg(unimplemented, Utils::FilePath::fromString(cppFile)); if (dlg.exec() == QDialog::Accepted) settings = dlg.settings(); break; @@ -3540,7 +3544,7 @@ private: const auto incDefPos = [&defPos] { defPos = (defPos + 1) % (DefPosImplementationFile + 2); }; - for (Symbol * const func : qAsConst(m_declarations)) { + for (Symbol * const func : qAsConst(unimplemented)) { incDefPos(); if (defPos > DefPosImplementationFile) continue; @@ -3610,8 +3614,8 @@ private: } ClassSpecifierAST *m_classAST = nullptr; - QList m_declarations; InsertDefsFromDecls::Mode m_mode; + QList m_declarations; }; -- cgit v1.2.3