From 72e0ded3c4882868a0030300f32c18f8671b4d39 Mon Sep 17 00:00:00 2001 From: "Andrey M. Tokarev" Date: Wed, 10 Apr 2013 19:37:25 +0400 Subject: [PATCH] CppEditor: add more sections (protected, ...) for declaration (refactoring) You can write definition of function, type Alt+Enter, as usual, and select not only public but also other possible sections like private, public slots and so on. Change-Id: I2faefc3833c6f05c9e2e5a2a41328bcdbe17ba14 Reviewed-by: Nikolai Kosjar --- src/plugins/cppeditor/cppplugin.h | 2 + src/plugins/cppeditor/cppquickfix_test.cpp | 65 +++++++++++++++++-- src/plugins/cppeditor/cppquickfixes.cpp | 36 +++++++++- src/plugins/texteditor/basetexteditor.cpp | 2 +- .../codeassist/basicproposalitemlistmodel.cpp | 14 ++++ .../codeassist/basicproposalitemlistmodel.h | 3 + .../texteditor/codeassist/codeassistant.cpp | 7 ++ 7 files changed, 119 insertions(+), 10 deletions(-) diff --git a/src/plugins/cppeditor/cppplugin.h b/src/plugins/cppeditor/cppplugin.h index 27780909a2c..dd57aa5effb 100644 --- a/src/plugins/cppeditor/cppplugin.h +++ b/src/plugins/cppeditor/cppplugin.h @@ -126,6 +126,8 @@ private slots: void test_quickfix_InsertDefFromDecl_headerSource_namespace2(); void test_quickfix_InsertDefFromDecl_freeFunction(); + void test_quickfix_InsertDeclFromDef(); + void test_quickfix_AddIncludeForUndefinedIdentifier_normal(); void test_quickfix_AddIncludeForUndefinedIdentifier_noinclude(); void test_quickfix_AddIncludeForUndefinedIdentifier_noincludeComment01(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 46f66825fec..2107f29769c 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -129,10 +129,11 @@ struct TestCase TestCase(const QList theTestFiles); ~TestCase(); - QuickFixOperation::Ptr getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget); + QuickFixOperation::Ptr getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget, + int resultIndex = 0); TestDocumentPtr testFileWithCursorMarker() const; - void run(CppQuickFixFactory *factory); + void run(CppQuickFixFactory *factory, int resultIndex = 0); private: TestCase(const TestCase &); @@ -141,13 +142,14 @@ private: void init(); }; -/// Apply the factory on the source and get back the first result or a null pointer. -QuickFixOperation::Ptr TestCase::getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget) +/// Apply the factory on the source and get back the resultIndex'th result or a null pointer. +QuickFixOperation::Ptr TestCase::getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget, + int resultIndex) { CppQuickFixInterface qfi(new CppQuickFixAssistInterface(editorWidget, ExplicitlyInvoked)); TextEditor::QuickFixOperations results; factory->match(qfi, results); - return results.isEmpty() ? QuickFixOperation::Ptr() : results.first(); + return results.isEmpty() ? QuickFixOperation::Ptr() : results.at(resultIndex); } /// The '@' in the originalSource is the position from where the quick-fix discovery is triggered. @@ -271,7 +273,7 @@ QByteArray &removeTrailingWhitespace(QByteArray &input) return input; } -void TestCase::run(CppQuickFixFactory *factory) +void TestCase::run(CppQuickFixFactory *factory, int resultIndex) { // Run the fix in the file having the cursor marker TestDocumentPtr testFile; @@ -281,7 +283,7 @@ void TestCase::run(CppQuickFixFactory *factory) } QVERIFY2(testFile, "No test file with cursor marker found"); - if (QuickFixOperation::Ptr fix = getFix(factory, testFile->editorWidget)) + if (QuickFixOperation::Ptr fix = getFix(factory, testFile->editorWidget, resultIndex)) fix->perform(); else qDebug() << "Quickfix was not triggered"; @@ -881,6 +883,55 @@ void CppPlugin::test_quickfix_InsertDefFromDecl_freeFunction() data.run(&factory); } +// Function for one of InsertDeclDef section cases +void insertToSectionDeclFromDef(const QByteArray §ion, int sectionIndex) +{ + QList testFiles; + + QByteArray original; + QByteArray expected; + + // Header File + original = + "class Foo\n" + "{\n" + "};\n"; + expected = + "class Foo\n" + "{\n" + + section + ":\n" + + " Foo();\n" + "@};\n\n"; + testFiles << TestDocument::create(original, expected, QLatin1String("file.h")); + + // Source File + original = + "#include \"file.h\"\n" + "\n" + "Foo::Foo@()\n" + "{\n" + "}\n" + "\n" + ; + expected = original + "\n"; + testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp")); + + InsertDeclFromDef factory; + TestCase data(testFiles); + data.run(&factory, sectionIndex); +} + +/// Check from source file: Insert in header file. +void CppPlugin::test_quickfix_InsertDeclFromDef() +{ + insertToSectionDeclFromDef("public", 0); + insertToSectionDeclFromDef("public slots", 1); + insertToSectionDeclFromDef("protected", 2); + insertToSectionDeclFromDef("protected slots", 3); + insertToSectionDeclFromDef("private", 4); + insertToSectionDeclFromDef("private slots", 5); +} + /// Check normal add include if there is already a include void CppPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_normal() { diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 7f8e77bff34..fbabeaf7aca 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -2339,6 +2339,30 @@ private: QString m_decl; }; +class DeclOperationFactory +{ +public: + DeclOperationFactory(const CppQuickFixInterface &interface, const QString &fileName, + const Class *matchingClass, const QString &decl) + : m_interface(interface) + , m_fileName(fileName) + , m_matchingClass(matchingClass) + , m_decl(decl) + {} + TextEditor::QuickFixOperation::Ptr + operator()(InsertionPointLocator::AccessSpec xsSpec) + { + return TextEditor::QuickFixOperation::Ptr( + new InsertDeclOperation(m_interface, m_fileName, m_matchingClass, xsSpec, m_decl)); + } + +private: + const CppQuickFixInterface &m_interface; + const QString &m_fileName; + const Class *m_matchingClass; + const QString &m_decl; +}; + } // anonymous namespace void InsertDeclFromDef::match(const CppQuickFixInterface &interface, QuickFixOperations &result) @@ -2387,8 +2411,16 @@ void InsertDeclFromDef::match(const CppQuickFixInterface &interface, QuickFixOpe QString fileName = QString::fromUtf8(matchingClass->fileName(), matchingClass->fileNameLength()); const QString decl = InsertDeclOperation::generateDeclaration(fun); - result.append(QuickFixOperation::Ptr(new InsertDeclOperation(interface, fileName, matchingClass, - InsertionPointLocator::Public, decl))); + + // Add several possible insertion locations for declaration + DeclOperationFactory operation(interface, fileName, matchingClass, decl); + + result.append(operation(InsertionPointLocator::Public)); + result.append(operation(InsertionPointLocator::PublicSlot)); + result.append(operation(InsertionPointLocator::Protected)); + result.append(operation(InsertionPointLocator::ProtectedSlot)); + result.append(operation(InsertionPointLocator::Private)); + result.append(operation(InsertionPointLocator::PrivateSlot)); } } diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 3252f024942..116b2d06581 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -6210,7 +6210,7 @@ int BaseTextEditorWidget::lineNumberDigits() const bool BaseTextEditorWidget::selectionVisible(int blockNumber) const { - Q_UNUSED(blockNumber) + Q_UNUSED(blockNumber); return true; } diff --git a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp index b6c479b9c89..7e31f3e6adb 100644 --- a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp +++ b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.cpp @@ -118,11 +118,13 @@ struct ContentLessThan } // Anonymous BasicProposalItemListModel::BasicProposalItemListModel() + : m_isSortingAllowed(false) {} BasicProposalItemListModel::BasicProposalItemListModel(const QList &items) : m_currentItems(items) , m_originalItems(items) + , m_isSortingAllowed(true) { mapPersistentIds(); } @@ -139,6 +141,16 @@ void BasicProposalItemListModel::loadContent(const QList &i mapPersistentIds(); } +void BasicProposalItemListModel::setSortingAllowed(bool isAllowed) +{ + m_isSortingAllowed = isAllowed; +} + +bool BasicProposalItemListModel::isSortingAllowed() const +{ + return m_isSortingAllowed; +} + void BasicProposalItemListModel::mapPersistentIds() { for (int i = 0; i < m_originalItems.size(); ++i) @@ -258,6 +270,8 @@ bool BasicProposalItemListModel::isSortable(const QString &prefix) const { Q_UNUSED(prefix); + if (!isSortingAllowed()) + return false; if (m_currentItems.size() < kMaxSort) return true; return false; diff --git a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.h b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.h index 533f2859c05..e9769a192f2 100644 --- a/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.h +++ b/src/plugins/texteditor/codeassist/basicproposalitemlistmodel.h @@ -65,6 +65,8 @@ public: virtual IAssistProposalItem *proposalItem(int index) const; void loadContent(const QList &items); + void setSortingAllowed(bool isAllowed); + bool isSortingAllowed() const; protected: typedef QList::iterator ItemIterator; @@ -76,6 +78,7 @@ private: QHash m_idByText; QList m_originalItems; + bool m_isSortingAllowed; }; } // TextEditor diff --git a/src/plugins/texteditor/codeassist/codeassistant.cpp b/src/plugins/texteditor/codeassist/codeassistant.cpp index ca2003a600a..54847c05ef2 100644 --- a/src/plugins/texteditor/codeassist/codeassistant.cpp +++ b/src/plugins/texteditor/codeassist/codeassistant.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -265,6 +266,12 @@ void CodeAssistantPrivate::requestProposal(AssistReason reason, } IAssistProposal *newProposal = processor->perform(assistInterface); + if (kind == QuickFix) { + TextEditor::BasicProposalItemListModel *proposalModel = + static_cast(newProposal->model()); + proposalModel->setSortingAllowed(false); + } + displayProposal(newProposal, reason); delete processor; }