forked from qt-creator/qt-creator
CppEditor: Move RearrangeParamDeclarationList quickfix to its own files
Change-Id: I71354ebf11d943531fef1f417e389ae28b964ccb Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -122,6 +122,7 @@ add_qtc_plugin(CppEditor
|
||||
quickfixes/logicaloperationquickfixes.cpp quickfixes/logicaloperationquickfixes.h
|
||||
quickfixes/moveclasstoownfile.cpp quickfixes/moveclasstoownfile.h
|
||||
quickfixes/movefunctiondefinition.cpp quickfixes/movefunctiondefinition.h
|
||||
quickfixes/rearrangeparamdeclarationlist.cpp quickfixes/rearrangeparamdeclarationlist.h
|
||||
quickfixes/removeusingnamespace.cpp quickfixes/removeusingnamespace.h
|
||||
quickfixes/rewritecomment.cpp quickfixes/rewritecomment.cpp
|
||||
quickfixes/rewritecontrolstatements.cpp quickfixes/rewritecontrolstatements.h
|
||||
|
@@ -273,6 +273,8 @@ QtcPlugin {
|
||||
"moveclasstoownfile.h",
|
||||
"movefunctiondefinition.cpp",
|
||||
"movefunctiondefinition.h",
|
||||
"rearrangeparamdeclarationlist.cpp",
|
||||
"rearrangeparamdeclarationlist.h",
|
||||
"removeusingnamespace.cpp",
|
||||
"removeusingnamespace.h",
|
||||
"rewritecomment.cpp",
|
||||
|
@@ -31,6 +31,7 @@
|
||||
#include "logicaloperationquickfixes.h"
|
||||
#include "moveclasstoownfile.h"
|
||||
#include "movefunctiondefinition.h"
|
||||
#include "rearrangeparamdeclarationlist.h"
|
||||
#include "removeusingnamespace.h"
|
||||
#include "rewritecomment.h"
|
||||
#include "rewritecontrolstatements.h"
|
||||
@@ -135,87 +136,6 @@ namespace Internal {
|
||||
|
||||
namespace {
|
||||
|
||||
class RearrangeParamDeclarationListOp: public CppQuickFixOperation
|
||||
{
|
||||
public:
|
||||
enum Target { TargetPrevious, TargetNext };
|
||||
|
||||
RearrangeParamDeclarationListOp(const CppQuickFixInterface &interface, AST *currentParam,
|
||||
AST *targetParam, Target target)
|
||||
: CppQuickFixOperation(interface)
|
||||
, m_currentParam(currentParam)
|
||||
, m_targetParam(targetParam)
|
||||
{
|
||||
QString targetString;
|
||||
if (target == TargetPrevious)
|
||||
targetString = Tr::tr("Switch with Previous Parameter");
|
||||
else
|
||||
targetString = Tr::tr("Switch with Next Parameter");
|
||||
setDescription(targetString);
|
||||
}
|
||||
|
||||
void perform() override
|
||||
{
|
||||
CppRefactoringChanges refactoring(snapshot());
|
||||
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
|
||||
|
||||
int targetEndPos = currentFile->endOf(m_targetParam);
|
||||
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;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void RearrangeParamDeclarationList::doMatch(const CppQuickFixInterface &interface,
|
||||
QuickFixOperations &result)
|
||||
{
|
||||
const QList<AST *> path = interface.path();
|
||||
|
||||
ParameterDeclarationAST *paramDecl = nullptr;
|
||||
int index = path.size() - 1;
|
||||
for (; index != -1; --index) {
|
||||
paramDecl = path.at(index)->asParameterDeclaration();
|
||||
if (paramDecl)
|
||||
break;
|
||||
}
|
||||
|
||||
if (index < 1)
|
||||
return;
|
||||
|
||||
ParameterDeclarationClauseAST *paramDeclClause = path.at(index-1)->asParameterDeclarationClause();
|
||||
QTC_ASSERT(paramDeclClause && paramDeclClause->parameter_declaration_list, return);
|
||||
|
||||
ParameterDeclarationListAST *paramListNode = paramDeclClause->parameter_declaration_list;
|
||||
ParameterDeclarationListAST *prevParamListNode = nullptr;
|
||||
while (paramListNode) {
|
||||
if (paramDecl == paramListNode->value)
|
||||
break;
|
||||
prevParamListNode = paramListNode;
|
||||
paramListNode = paramListNode->next;
|
||||
}
|
||||
|
||||
if (!paramListNode)
|
||||
return;
|
||||
|
||||
if (prevParamListNode)
|
||||
result << new RearrangeParamDeclarationListOp(interface, paramListNode->value,
|
||||
prevParamListNode->value, RearrangeParamDeclarationListOp::TargetPrevious);
|
||||
if (paramListNode->next)
|
||||
result << new RearrangeParamDeclarationListOp(interface, paramListNode->value,
|
||||
paramListNode->next->value, RearrangeParamDeclarationListOp::TargetNext);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class ReformatPointerDeclarationOp: public CppQuickFixOperation
|
||||
{
|
||||
public:
|
||||
@@ -384,7 +304,6 @@ void ExtraRefactoringOperations::doMatch(const CppQuickFixInterface &interface,
|
||||
|
||||
void createCppQuickFixes()
|
||||
{
|
||||
new RearrangeParamDeclarationList;
|
||||
new ReformatPointerDeclaration;
|
||||
|
||||
new ApplyDeclDefLinkChanges;
|
||||
@@ -411,6 +330,7 @@ void createCppQuickFixes()
|
||||
registerSplitSimpleDeclarationQuickfix();
|
||||
registerConvertNumericLiteralQuickfix();
|
||||
registerConvertToCamelCaseQuickfix();
|
||||
registerRearrangeParamDeclarationListQuickfix();
|
||||
|
||||
new ExtraRefactoringOperations;
|
||||
}
|
||||
|
@@ -28,18 +28,6 @@ public:
|
||||
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override;
|
||||
};
|
||||
|
||||
/*!
|
||||
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 RearrangeParamDeclarationList : public CppQuickFixFactory
|
||||
{
|
||||
public:
|
||||
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override;
|
||||
};
|
||||
|
||||
/*!
|
||||
Reformats a pointer, reference or rvalue reference type/declaration.
|
||||
|
||||
|
@@ -0,0 +1,124 @@
|
||||
// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "rearrangeparamdeclarationlist.h"
|
||||
|
||||
#include "../cppeditortr.h"
|
||||
#include "../cpprefactoringchanges.h"
|
||||
#include "cppquickfix.h"
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
#include <QObject>
|
||||
#endif
|
||||
|
||||
using namespace CPlusPlus;
|
||||
|
||||
namespace CppEditor::Internal {
|
||||
namespace {
|
||||
|
||||
class RearrangeParamDeclarationListOp: public CppQuickFixOperation
|
||||
{
|
||||
public:
|
||||
enum Target { TargetPrevious, TargetNext };
|
||||
|
||||
RearrangeParamDeclarationListOp(const CppQuickFixInterface &interface, AST *currentParam,
|
||||
AST *targetParam, Target target)
|
||||
: CppQuickFixOperation(interface)
|
||||
, m_currentParam(currentParam)
|
||||
, m_targetParam(targetParam)
|
||||
{
|
||||
QString targetString;
|
||||
if (target == TargetPrevious)
|
||||
targetString = Tr::tr("Switch with Previous Parameter");
|
||||
else
|
||||
targetString = Tr::tr("Switch with Next Parameter");
|
||||
setDescription(targetString);
|
||||
}
|
||||
|
||||
void perform() override
|
||||
{
|
||||
CppRefactoringChanges refactoring(snapshot());
|
||||
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
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 RearrangeParamDeclarationList : public CppQuickFixFactory
|
||||
{
|
||||
#ifdef WITH_TESTS
|
||||
public:
|
||||
static QObject *createTest() { return new QObject; }
|
||||
#endif
|
||||
|
||||
void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
|
||||
{
|
||||
const QList<AST *> path = interface.path();
|
||||
|
||||
ParameterDeclarationAST *paramDecl = nullptr;
|
||||
int index = path.size() - 1;
|
||||
for (; index != -1; --index) {
|
||||
paramDecl = path.at(index)->asParameterDeclaration();
|
||||
if (paramDecl)
|
||||
break;
|
||||
}
|
||||
|
||||
if (index < 1)
|
||||
return;
|
||||
|
||||
ParameterDeclarationClauseAST *paramDeclClause
|
||||
= path.at(index - 1)->asParameterDeclarationClause();
|
||||
QTC_ASSERT(paramDeclClause && paramDeclClause->parameter_declaration_list, return);
|
||||
|
||||
ParameterDeclarationListAST *paramListNode = paramDeclClause->parameter_declaration_list;
|
||||
ParameterDeclarationListAST *prevParamListNode = nullptr;
|
||||
while (paramListNode) {
|
||||
if (paramDecl == paramListNode->value)
|
||||
break;
|
||||
prevParamListNode = paramListNode;
|
||||
paramListNode = paramListNode->next;
|
||||
}
|
||||
|
||||
if (!paramListNode)
|
||||
return;
|
||||
|
||||
if (prevParamListNode)
|
||||
result << new RearrangeParamDeclarationListOp(
|
||||
interface,
|
||||
paramListNode->value,
|
||||
prevParamListNode->value,
|
||||
RearrangeParamDeclarationListOp::TargetPrevious);
|
||||
if (paramListNode->next)
|
||||
result << new RearrangeParamDeclarationListOp(
|
||||
interface,
|
||||
paramListNode->value,
|
||||
paramListNode->next->value,
|
||||
RearrangeParamDeclarationListOp::TargetNext);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void registerRearrangeParamDeclarationListQuickfix()
|
||||
{
|
||||
CppQuickFixFactory::registerFactory<RearrangeParamDeclarationList>();
|
||||
}
|
||||
|
||||
} // namespace CppEditor::Internal
|
@@ -0,0 +1,8 @@
|
||||
// Copyright (C) 2024 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace CppEditor::Internal {
|
||||
void registerRearrangeParamDeclarationListQuickfix();
|
||||
} // namespace CppEditor::Internal
|
Reference in New Issue
Block a user