From e99f9156237cb71e6a4347924c21a568b30dfae2 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 5 Sep 2011 10:33:17 +0200 Subject: [PATCH] C++: Trigger function signature change via quick fix. This way we don't need to override Return and Escape. Change-Id: I1548118c06ee7338ba3dd9d84b82b9314ab16782 Reviewed-on: http://codereview.qt.nokia.com/4183 Reviewed-by: Leandro T. C. Melo --- src/plugins/cppeditor/cppeditor.cpp | 29 +++--------- src/plugins/cppeditor/cppeditor.h | 4 +- src/plugins/cppeditor/cppeditorconstants.h | 2 - .../cppeditor/cppfunctiondecldeflink.cpp | 46 +++++++++++++++++++ .../cppeditor/cppfunctiondecldeflink.h | 9 ++++ src/plugins/cppeditor/cppquickfixes.cpp | 2 + 6 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index 4a3841499c5..1940e1197a8 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1473,10 +1473,9 @@ bool CPPEditorWidget::event(QEvent *e) { switch (e->type()) { case QEvent::ShortcutOverride: - // handle escape manually if a rename or func decl/def link is active + // handle escape manually if a rename is active if (static_cast(e)->key() == Qt::Key_Escape - && (m_currentRenameSelection != NoCurrentRenameSelection - || m_declDefLink)) { + && m_currentRenameSelection != NoCurrentRenameSelection) { e->accept(); return true; } @@ -1551,23 +1550,6 @@ void CPPEditorWidget::contextMenuEvent(QContextMenuEvent *e) void CPPEditorWidget::keyPressEvent(QKeyEvent *e) { if (m_currentRenameSelection == NoCurrentRenameSelection) { - // key handling for linked function declarations/definitions - if (m_declDefLink && m_declDefLink->isMarkerVisible()) { - switch (e->key()) { - case Qt::Key_Enter: - case Qt::Key_Return: - applyDeclDefLinkChanges(/*jump tp change*/ e->modifiers() & Qt::ShiftModifier); - e->accept(); - return; - case Qt::Key_Escape: - abortDeclDefLink(); - e->accept(); - return; - default: - break; - } - } - TextEditor::BaseTextEditorWidget::keyPressEvent(e); return; } @@ -2151,6 +2133,11 @@ TextEditor::IAssistInterface *CPPEditorWidget::createAssistInterface( return 0; } +QSharedPointer CPPEditorWidget::declDefLink() const +{ + return m_declDefLink; +} + void CPPEditorWidget::onRefactorMarkerClicked(const TextEditor::RefactorMarker &marker) { if (marker.data.canConvert()) @@ -2207,7 +2194,6 @@ void CPPEditorWidget::onFunctionDeclDefLinkFound(QSharedPointerhideMarker(this); m_declDefLink.clear(); } diff --git a/src/plugins/cppeditor/cppeditor.h b/src/plugins/cppeditor/cppeditor.h index 1c46fddfbff..e2a986c6a95 100644 --- a/src/plugins/cppeditor/cppeditor.h +++ b/src/plugins/cppeditor/cppeditor.h @@ -195,6 +195,9 @@ public: virtual TextEditor::IAssistInterface *createAssistInterface(TextEditor::AssistKind kind, TextEditor::AssistReason reason) const; + QSharedPointer declDefLink() const; + void applyDeclDefLinkChanges(bool jumpToMatch); + Q_SIGNALS: void outlineModelIndexChanged(const QModelIndex &index); @@ -268,7 +271,6 @@ private: void finishRename(); void abortRename(); - void applyDeclDefLinkChanges(bool jumpToMatch); Q_SLOT void abortDeclDefLink(); Link attemptFuncDeclDef(const QTextCursor &cursor, diff --git a/src/plugins/cppeditor/cppeditorconstants.h b/src/plugins/cppeditor/cppeditorconstants.h index 32af58e36cb..21eddf2a593 100644 --- a/src/plugins/cppeditor/cppeditorconstants.h +++ b/src/plugins/cppeditor/cppeditorconstants.h @@ -65,8 +65,6 @@ const char * const WIZARD_TR_CATEGORY = QT_TRANSLATE_NOOP("CppEditor", "C++"); const char * const CPP_SNIPPETS_GROUP_ID = "C++"; -const char * const FUNCTION_DECL_DEF_LINK_PROPERTY_NAME = "inCppFunctionDeclDefLink"; - } // namespace Constants } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp index a4cca01819a..74946fa55ba 100644 --- a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp +++ b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp @@ -33,6 +33,7 @@ #include "cppfunctiondecldeflink.h" #include "cppeditor.h" +#include "cppquickfixassistant.h" #include #include @@ -671,3 +672,48 @@ Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targ return changes; } + +class ApplyDeclDefLinkOperation : public CppQuickFixOperation +{ +public: + explicit ApplyDeclDefLinkOperation( + const QSharedPointer &interface, + const QSharedPointer &link, + int priority = -1) + : CppQuickFixOperation(interface, priority) + , m_link(link) + {} + + virtual void perform() + { + CPPEditorWidget *editor = assistInterface()->editor(); + QSharedPointer link = editor->declDefLink(); + if (link != m_link) + return; + + return editor->applyDeclDefLinkChanges(true); + } + +protected: + virtual void performChanges(const CppTools::CppRefactoringFilePtr &, const CppTools::CppRefactoringChanges &) + { /* never called since perform is overridden */ } + +private: + QSharedPointer m_link; +}; + +QList ApplyDeclDefLinkChanges::match(const QSharedPointer &interface) +{ + QList results; + + QSharedPointer link = interface->editor()->declDefLink(); + if (!link || !link->isMarkerVisible()) + return results; + + QSharedPointer op(new ApplyDeclDefLinkOperation(interface, link)); + op->setDescription(FunctionDeclDefLink::tr("Apply function signature changes")); + op->setPriority(0); + results += op; + + return results; +} diff --git a/src/plugins/cppeditor/cppfunctiondecldeflink.h b/src/plugins/cppeditor/cppfunctiondecldeflink.h index 13fc47d327b..03bacc34a94 100644 --- a/src/plugins/cppeditor/cppfunctiondecldeflink.h +++ b/src/plugins/cppeditor/cppfunctiondecldeflink.h @@ -33,6 +33,8 @@ #ifndef CPPFUNCTIONDECLDEFLINK_H #define CPPFUNCTIONDECLDEFLINK_H +#include "cppquickfix.h" + #include #include @@ -121,6 +123,13 @@ private: friend class FunctionDeclDefLinkFinder; }; +class ApplyDeclDefLinkChanges: public CppQuickFixFactory +{ +public: + virtual QList + match(const QSharedPointer &interface); +}; + } // namespace Internal } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index c169e324891..d6742c0017e 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -37,6 +37,7 @@ #include "cppinsertqtpropertymembers.h" #include "cppquickfixassistant.h" #include "cppcompleteswitch.h" +#include "cppfunctiondecldeflink.h" #include #include @@ -1693,4 +1694,5 @@ void registerQuickFixes(ExtensionSystem::IPlugin *plugIn) plugIn->addAutoReleasedObject(new InsertQtPropertyMembers); plugIn->addAutoReleasedObject(new DeclFromDef); plugIn->addAutoReleasedObject(new DefFromDecl); + plugIn->addAutoReleasedObject(new ApplyDeclDefLinkChanges); }