diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index 239e6dc8441dc9344697302b6759f969593e36cc..64c19e313011d8613d455efc2f65508c8877761a 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -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(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 74e64c8766e03fc5c302ed3af544c4becace9b49..66aa48fd470077ab325e134b5a46e9d481a33c36 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -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 = diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 2c320ab533232bd1b75755c91a63df71dbbe17ff..095e596ad32f25ff4f31f946aa245f9db167e347 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -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;