Commit 1738ff64 authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Erik Verbruggen
Browse files

CppEditor: Fix moving definitions



* Move initializer lists completely
* Respect whitespace as it is

Task-number: QTCREATORBUG-9462
Task-number: QTCREATORBUG-9463

Change-Id: Id9c41394b4aa55b0a85957286ab42f2ce8966c08
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 28df27d9
......@@ -178,7 +178,8 @@ 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_MoveFuncDefOutside_CtorWithInitialization1();
void test_quickfix_MoveFuncDefOutside_CtorWithInitialization2();
void test_quickfix_MoveFuncDefToDecl_MemberFunc();
void test_quickfix_MoveFuncDefToDecl_MemberFuncOutside();
......
......@@ -1310,7 +1310,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCpp()
// Header File
original =
"class Foo {\n"
" inline int numbe@r() const {\n"
" inline int numbe@r() const\n"
" {\n"
" return 5;\n"
" }\n"
"\n"
......@@ -1332,7 +1333,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCpp()
"#include \"file.h\"\n"
"\n"
"\n"
"int Foo::number() const {\n"
"int Foo::number() const\n"
"{\n"
" return 5;\n"
"}\n"
"\n";
......@@ -1348,7 +1350,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutside()
{
QByteArray original =
"class Foo {\n"
" inline int numbe@r() const {\n"
" inline int numbe@r() const\n"
" {\n"
" return 5;\n"
" }\n"
"};";
......@@ -1357,7 +1360,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutside()
" inline int number() const;\n"
"};\n"
"\n"
"int Foo::number() const {\n"
"int Foo::number() const\n"
"{\n"
" return 5;\n"
"}"
"\n\n";
......@@ -1378,7 +1382,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNS()
original =
"namespace MyNs {\n"
"class Foo {\n"
" inline int numbe@r() const {\n"
" inline int numbe@r() const\n"
" {\n"
" return 5;\n"
" }\n"
"};\n"
......@@ -1399,7 +1404,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNS()
"#include \"file.h\"\n"
"\n"
"\n"
"int MyNs::Foo::number() const {\n"
"int MyNs::Foo::number() const\n"
"{\n"
" return 5;\n"
"}\n"
"\n";
......@@ -1421,7 +1427,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNSUsing()
original =
"namespace MyNs {\n"
"class Foo {\n"
" inline int numbe@r() const {\n"
" inline int numbe@r() const\n"
" {\n"
" return 5;\n"
" }\n"
"};\n"
......@@ -1444,7 +1451,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNSUsing()
"using namespace MyNs;\n"
"\n"
"\n"
"int Foo::number() const {\n"
"int Foo::number() const\n"
"{\n"
" return 5;\n"
"}\n"
"\n";
......@@ -1461,7 +1469,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutsideWithNs()
QByteArray original =
"namespace MyNs {\n"
"class Foo {\n"
" inline int numbe@r() const {\n"
" inline int numbe@r() const\n"
" {\n"
" return 5;\n"
" }\n"
"};}";
......@@ -1471,7 +1480,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutsideWithNs()
" inline int number() const;\n"
"};\n"
"\n"
"int Foo::number() const {\n"
"int Foo::number() const\n"
"{\n"
" return 5;\n"
"}"
"\n}\n";
......@@ -1490,7 +1500,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCpp()
// Header File
original =
"int numbe@r() const {\n"
"int numbe@r() const\n"
"{\n"
" return 5;\n"
"}\n";
expected =
......@@ -1506,7 +1517,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCpp()
"#include \"file.h\"\n"
"\n"
"\n"
"int number() const {\n"
"int number() const\n"
"{\n"
" return 5;\n"
"}\n"
"\n";
......@@ -1527,7 +1539,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS()
// Header File
original =
"namespace MyNamespace {\n"
"int numbe@r() const {\n"
"int numbe@r() const\n"
"{\n"
" return 5;\n"
"}\n"
"}\n";
......@@ -1546,7 +1559,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS()
"#include \"file.h\"\n"
"\n"
"\n"
"int MyNamespace::number() const {\n"
"int MyNamespace::number() const\n"
"{\n"
" return 5;\n"
"}\n"
"\n";
......@@ -1558,7 +1572,7 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS()
}
/// Check: Move Ctor with member initialization list (QTCREATORBUG-9157).
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization()
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization1()
{
QList<TestDocumentPtr> testFiles;
QByteArray original;
......@@ -1597,6 +1611,51 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization()
data.run(&factory);
}
/// Check: Move Ctor with member initialization list (QTCREATORBUG-9462).
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization2()
{
QList<TestDocumentPtr> testFiles;
QByteArray original;
QByteArray expected;
// Header File
original =
"class Foo\n"
"{\n"
"public:\n"
" Fo@o() : member(2)\n"
" {\n"
" }\n"
"\n"
" int member;\n"
"};";
expected =
"class Foo\n"
"{\n"
"public:\n"
" Foo();\n"
"\n"
" int member;\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() : member(2)\n"
"{\n"
"}\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 CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFunc()
{
......@@ -1605,8 +1664,14 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFunc()
QByteArray expected;
// Header File
original = "class Foo {inline int number() const;};\n";
expected = "class Foo {inline int number() const {return 5;}};\n\n";
original =
"class Foo {\n"
" inline int number() const;\n"
"};\n";
expected =
"class Foo {\n"
" inline int number() const {return 5;}\n"
"};\n\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));
// Source File
......@@ -1632,13 +1697,15 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFuncOutside()
" inline int number() const;\n"
"};\n"
"\n"
"int Foo::num@ber() const {\n"
"int Foo::num@ber() const\n"
"{\n"
" return 5;\n"
"}\n";
QByteArray expected =
"class Foo {\n"
" inline int number() const {\n"
" inline int number() const\n"
" {\n"
" return 5;\n"
" }\n"
"};\n"
......@@ -1666,7 +1733,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFuncToCppNS()
expected =
"namespace MyNs {\n"
"class Foo {\n"
" inline int number() const {\n"
" inline int number() const\n"
" {\n"
" return 5;\n"
" }\n"
"};\n"
......@@ -1677,7 +1745,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFuncToCppNS()
original =
"#include \"file.h\"\n"
"\n"
"int MyNs::Foo::num@ber() const {\n"
"int MyNs::Foo::num@ber() const\n"
"{\n"
" return 5;\n"
"}\n";
expected = "#include \"file.h\"\n\n\n\n";
......@@ -1705,7 +1774,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFuncToCppNSUsing()
expected =
"namespace MyNs {\n"
"class Foo {\n"
" inline int number() const {\n"
" inline int number() const\n"
" {\n"
" return 5;\n"
" }\n"
"};\n"
......@@ -1717,7 +1787,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFuncToCppNSUsing()
"#include \"file.h\"\n"
"using namespace MyNs;\n"
"\n"
"int Foo::num@ber() const {\n"
"int Foo::num@ber() const\n"
"{\n"
" return 5;\n"
"}\n";
expected =
......@@ -1740,14 +1811,16 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFuncOutsideWithNs()
" inline int number() const;\n"
"};\n"
"\n"
"int Foo::numb@er() const {\n"
"int Foo::numb@er() const\n"
"{\n"
" return 5;\n"
"}"
"\n}\n";
QByteArray expected =
"namespace MyNs {\n"
"class Foo {\n"
" inline int number() const {\n"
" inline int number() const\n"
" {\n"
" return 5;\n"
" }\n"
"};\n\n\n}\n\n";
......@@ -1767,7 +1840,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_FreeFuncToCpp()
// Header File
original = "int number() const;\n";
expected =
"int number() const {\n"
"int number() const\n"
"{\n"
" return 5;\n"
"}\n\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));
......@@ -1777,7 +1851,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_FreeFuncToCpp()
"#include \"file.h\"\n"
"\n"
"\n"
"int numb@er() const {\n"
"int numb@er() const\n"
"{\n"
" return 5;\n"
"}\n";
expected = "#include \"file.h\"\n\n\n\n\n";
......@@ -1802,7 +1877,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_FreeFuncToCppNS()
"}\n";
expected =
"namespace MyNamespace {\n"
"int number() const {\n"
"int number() const\n"
"{\n"
" return 5;\n"
"}\n"
"}\n\n";
......@@ -1812,7 +1888,8 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_FreeFuncToCppNS()
original =
"#include \"file.h\"\n"
"\n"
"int MyNamespace::nu@mber() const {\n"
"int MyNamespace::nu@mber() const\n"
"{\n"
" return 5;\n"
"}\n";
expected =
......
......@@ -3816,13 +3816,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);
QString funcDef = QLatin1String("\n") + funcDec;
const int startPosition = fromFile->endOf(m_funcDef->declarator);
const int endPosition = fromFile->endOf(m_funcDef->function_body);
funcDef += fromFile->textOf(startPosition, endPosition) + QLatin1String("\n");
if (m_cppFileName.isEmpty() || !m_insideHeader)
funcDef = QLatin1String("\n") + funcDef;
......@@ -3840,7 +3837,7 @@ public:
headerTarget.remove(fromFile->range(m_funcDef));
} else {
QString textFuncDecl = fromFile->textOf(m_funcDef);
textFuncDecl.remove(-textFuncBody.length(), textFuncBody.length());
textFuncDecl.truncate(startPosition - fromFile->startOf(m_funcDef));
textFuncDecl = textFuncDecl.trimmed() + QLatin1String(";");
headerTarget.replace(fromFile->range(m_funcDef), textFuncDecl);
}
......@@ -3945,12 +3942,10 @@ public:
CppRefactoringFilePtr fromFile = refactoring.file(m_fromFileName);
CppRefactoringFilePtr toFile = refactoring.file(m_toFileName);
ChangeSet::Range fromRange = fromFile->range(m_funcAST);
const QString definitionText = fromFile->textOf(m_funcAST->function_body);
QString wholeFunctionText = m_declarationText;
if (m_funcAST->ctor_initializer)
wholeFunctionText += QLatin1Char(' ') + fromFile->textOf(m_funcAST->ctor_initializer);
wholeFunctionText += QLatin1Char(' ') + definitionText;
const QString wholeFunctionText = m_declarationText
+ fromFile->textOf(fromFile->endOf(m_funcAST->declarator),
fromFile->endOf(m_funcAST->function_body));
// 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