From d602c418d551ded2df13c6a885e06e49536cf8fc Mon Sep 17 00:00:00 2001 From: Element9 Date: Sat, 4 Feb 2012 14:41:30 +0100 Subject: C++: Rearrange parameter declaration list quickfix This quickfix switches places of the parameter declaration under cursor with the next or the previous one in the parameter declaration list. Change-Id: Ic379967ac51297a317a55d0e0faf6c5b1cb9d585 Reviewed-by: Leandro Melo --- src/plugins/cppeditor/cppquickfixes.cpp | 102 ++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) (limited to 'src/plugins/cppeditor') diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 2cfa607738..7bfd97b7e3 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -1955,6 +1955,107 @@ private: }; }; +/** + * Switches places of the parameter declaration under cursor + * with the next or the previous one in the parameter declaration list + * + * Activates on: parameter declarations + */ + +class RearrangeParamDeclList : public CppQuickFixFactory +{ +public: + enum Target + { + TargetPrevious, + TargetNext + }; + +public: + virtual QList match(const QSharedPointer &interface) + { + const QList path = interface->path(); + QList result; + + ParameterDeclarationAST *paramDecl = 0; + int index = path.size() - 1; + for (; index != -1; --index) { + paramDecl = path.at(index)->asParameterDeclaration(); + if (paramDecl) + break; + } + + if (index < 1) + return result; + + ParameterDeclarationClauseAST *paramDeclClause = path.at(index-1)->asParameterDeclarationClause(); + QTC_ASSERT(paramDeclClause && paramDeclClause->parameter_declaration_list, return result); + + ParameterDeclarationListAST *paramListNode = paramDeclClause->parameter_declaration_list; + ParameterDeclarationListAST *prevParamListNode = 0; + while (paramListNode) { + if (paramDecl == paramListNode->value) + break; + prevParamListNode = paramListNode; + paramListNode = paramListNode->next; + } + + if (!paramListNode) + return result; + + if (prevParamListNode) + result.append(CppQuickFixOperation::Ptr(new Operation(interface, paramListNode->value, + prevParamListNode->value, TargetPrevious))); + if (paramListNode->next) + result.append(CppQuickFixOperation::Ptr(new Operation(interface, paramListNode->value, + paramListNode->next->value, TargetNext))); + + return result; + } + +private: + class Operation: public CppQuickFixOperation + { + public: + Operation(const QSharedPointer &interface, + AST *currentParam, AST *targetParam, + Target target) + : CppQuickFixOperation(interface) + , m_currentParam(currentParam) + , m_targetParam(targetParam) + { + QString targetString; + if (target == TargetPrevious) + { + targetString = QApplication::translate("CppTools::QuickFix", + "Switch with Previous Parameter"); + } + else + { + targetString = QApplication::translate("CppTools::QuickFix", + "Switch with Next Parameter"); + } + + setDescription(targetString); + } + + virtual void performChanges(const CppRefactoringFilePtr ¤tFile, + const CppRefactoringChanges &) + { + int targetEndPos = currentFile->endOf(m_targetParam); + Utils::ChangeSet changes; + changes.flip(currentFile->startOf(m_currentParam), currentFile->endOf(m_currentParam), + currentFile->startOf(m_targetParam), targetEndPos); + currentFile->setChangeSet(changes); + currentFile->setOpenEditor(false, targetEndPos); + currentFile->apply(); + } + + private: + AST *m_currentParam; + AST *m_targetParam; + }; +}; } // end of anonymous namespace @@ -1982,4 +2083,5 @@ void registerQuickFixes(ExtensionSystem::IPlugin *plugIn) plugIn->addAutoReleasedObject(new ApplyDeclDefLinkChanges); plugIn->addAutoReleasedObject(new IncludeAdder); plugIn->addAutoReleasedObject(new ExtractFunction); + plugIn->addAutoReleasedObject(new RearrangeParamDeclList); } -- cgit v1.2.1