diff options
author | Element9 <bojan85@gmail.com> | 2012-02-04 14:41:30 +0100 |
---|---|---|
committer | Leandro Melo <leandro.melo@nokia.com> | 2012-02-10 12:45:24 +0100 |
commit | d602c418d551ded2df13c6a885e06e49536cf8fc (patch) | |
tree | bba76d93009c34e151d743888e58fac4c57eaa2d /src/plugins/cppeditor | |
parent | fe9eeece403e15d88759aa3766168f7c7d76d5d7 (diff) | |
download | qt-creator-d602c418d551ded2df13c6a885e06e49536cf8fc.tar.gz |
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 <leandro.melo@nokia.com>
Diffstat (limited to 'src/plugins/cppeditor')
-rw-r--r-- | src/plugins/cppeditor/cppquickfixes.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
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<CppQuickFixOperation::Ptr> match(const QSharedPointer<const CppQuickFixAssistInterface> &interface) + { + const QList<AST *> path = interface->path(); + QList<CppQuickFixOperation::Ptr> 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<const CppQuickFixAssistInterface> &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); } |