Commit 39d00043 authored by Lorenz Haas's avatar Lorenz Haas Committed by Nikolai Kosjar
Browse files

CppEditor: Quick fix "move definition" keeps ctor-initialization list



Task-number: QTCREATORBUG-9157

Change-Id: Ic46086ba07a86292bbf48de62b69e3f33628fd86
Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 7d2a7685
......@@ -140,6 +140,7 @@ private slots:
void test_quickfix_MoveFuncDefOutside_MemberFuncOutsideWithNs();
void test_quickfix_MoveFuncDefOutside_FreeFuncToCpp();
void test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS();
void test_quickfix_MoveFuncDefOutside_CtorWithInitialization();
void test_quickfix_MoveFuncDefToDecl_MemberFunc();
void test_quickfix_MoveFuncDefToDecl_MemberFuncOutside();
......@@ -148,6 +149,7 @@ private slots:
void test_quickfix_MoveFuncDefToDecl_MemberFuncOutsideWithNs();
void test_quickfix_MoveFuncDefToDecl_FreeFuncToCpp();
void test_quickfix_MoveFuncDefToDecl_FreeFuncToCppNS();
void test_quickfix_MoveFuncDefToDecl_CtorWithInitialization();
// The following tests depend on the projects that are loaded on startup
// and will be skipped in case no projects are loaded.
......
......@@ -1351,6 +1351,46 @@ void CppPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS()
data.run(&factory);
}
/// Check: Move Ctor with member initialization list (QTCREATORBUG-9157).
void CppPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization()
{
QList<TestDocumentPtr> testFiles;
QByteArray original;
QByteArray expected;
// Header File
original =
"class Foo {\n"
"public:\n"
" Fo@o() : a(42), b(3.141) {}\n"
"private:\n"
" int a;\n"
" float b;\n"
"};";
expected =
"class Foo {\n"
"public:\n"
" Foo();\n"
"private:\n"
" int a;\n"
" float b;\n"
"};\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));
// Source File
original ="#include \"file.h\"\n";
expected =
"#include \"file.h\"\n"
"\n"
"Foo::Foo() : a(42), b(3.141) {}\n"
"\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));
MoveFuncDefOutside factory;
TestCase data(testFiles);
data.run(&factory);
}
/// Check: revert test_quickfix_MoveFuncDefOutside_MemberFuncToCpp()
void CppPlugin::test_quickfix_MoveFuncDefToDecl_MemberFunc()
{
......@@ -1578,3 +1618,43 @@ void CppPlugin::test_quickfix_MoveFuncDefToDecl_FreeFuncToCppNS()
TestCase data(testFiles);
data.run(&factory);
}
/// Check: revert test_quickfix_MoveFuncDefOutside_CtorWithInitialization()
void CppPlugin::test_quickfix_MoveFuncDefToDecl_CtorWithInitialization()
{
QList<TestDocumentPtr> testFiles;
QByteArray original;
QByteArray expected;
// Header File
original =
"class Foo {\n"
"public:\n"
" Foo();\n"
"private:\n"
" int a;\n"
" float b;\n"
"};";
expected =
"class Foo {\n"
"public:\n"
" Foo() : a(42), b(3.141) {}\n"
"private:\n"
" int a;\n"
" float b;\n"
"};\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));
// Source File
original =
"#include \"file.h\"\n"
"\n"
"Foo::F@oo() : a(42), b(3.141) {}"
;
expected ="#include \"file.h\"\n\n\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));
MoveFuncDefToDecl factory;
TestCase data(testFiles);
data.run(&factory);
}
......@@ -3645,7 +3645,6 @@ public:
{
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr fromFile = refactoring.file(m_headerFileName);
const QString textFuncBody = fromFile->textOf(m_funcDef->function_body);
CppRefactoringFilePtr toFile;
int insertPos = 0;
Scope *scopeAtInsertPos = 0;
......@@ -3666,6 +3665,10 @@ public:
// construct definition
const QString funcDec = getDefinitionSignature(assistInterface(), m_func, toFile,
scopeAtInsertPos);
QString textFuncBody;
if (m_funcDef->ctor_initializer)
textFuncBody = fromFile->textOf(m_funcDef->ctor_initializer) + QLatin1Char(' ');
textFuncBody += fromFile->textOf(m_funcDef->function_body);
QString funcDef = QString::fromLatin1("\n%1 %2\n")
.arg(funcDec)
.arg(textFuncBody);
......@@ -3792,8 +3795,11 @@ public:
CppRefactoringFilePtr toFile = refactoring.file(m_toFileName);
ChangeSet::Range fromRange = fromFile->range(m_funcAST);
const QString definitionText = fromFile->textOf(m_funcAST->function_body);
const QString wholeFunctionText = QString::fromLatin1("%1 %2").arg(m_declarationText)
.arg(definitionText);
QString wholeFunctionText = m_declarationText;
if (m_funcAST->ctor_initializer)
wholeFunctionText += QLatin1Char(' ') + fromFile->textOf(m_funcAST->ctor_initializer);
wholeFunctionText += QLatin1Char(' ') + definitionText;
// Replace declaration with function and delete old definition
Utils::ChangeSet toTarget;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment