diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index df975c805b48ee31fc65ef27b90288fa1134115f..224f3edebace6754063ccba6c187767b6f787fb9 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -151,6 +151,7 @@ private slots: void test_quickfix_ReformatPointerDeclaration(); void test_quickfix_InsertDefFromDecl_basic(); + void test_quickfix_InsertDefFromDecl_afterClass(); void test_quickfix_InsertDefFromDecl_headerSource_basic1(); void test_quickfix_InsertDefFromDecl_headerSource_basic2(); void test_quickfix_InsertDefFromDecl_headerSource_namespace1(); @@ -196,6 +197,7 @@ private slots: void test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS(); void test_quickfix_MoveFuncDefOutside_CtorWithInitialization1(); void test_quickfix_MoveFuncDefOutside_CtorWithInitialization2(); + void test_quickfix_MoveFuncDefOutside_afterClass(); 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 eeb2f3415655291858e944c812a7c51a9d46aae5..a3931f57030804cab4369709c7f5db7366ac9d63 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -781,6 +781,50 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_basic() data.run(&factory); } +/// Check if definition is inserted right after class for insert definition outside +void CppEditorPlugin::test_quickfix_InsertDefFromDecl_afterClass() +{ + QList<TestDocumentPtr> testFiles; + QByteArray original; + QByteArray expected; + + // Header File + original = + "class Foo\n" + "{\n" + " Foo();\n" + " void a@();\n" + "};\n" + "\n" + "class Bar {};\n"; + expected = + "class Foo\n" + "{\n" + " Foo();\n" + " void a();\n" + "};\n" + "\n" + "void Foo::a()\n" + "{\n\n}\n" + "\n" + "class Bar {};\n\n"; + testFiles << TestDocument::create(original, expected, QLatin1String("file.h")); + + // Source File + original = + "#include \"file.h\"\n" + "\n" + "Foo::Foo()\n" + "{\n\n" + "}\n"; + expected = original + "\n"; + testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp")); + + InsertDefFromDecl factory; + TestCase data(testFiles); + data.run(&factory, 1); +} + /// Check from header file: If there is a source file, insert the definition in the source file. void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_basic1() { @@ -1877,7 +1921,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncOutside2() " void f3();\n" "};\n" "\n" - "\n" "int Foo::f2()\n" "{\n" " return 1;\n" @@ -2180,6 +2223,49 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization2() data.run(&factory); } +/// Check if definition is inserted right after class for move definition outside +void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_afterClass() +{ + QList<TestDocumentPtr> testFiles; + QByteArray original; + QByteArray expected; + + // Header File + original = + "class Foo\n" + "{\n" + " Foo();\n" + " void a@() {}\n" + "};\n" + "\n" + "class Bar {};\n"; + expected = + "class Foo\n" + "{\n" + " Foo();\n" + " void a();\n" + "};\n" + "\n" + "void Foo::a() {}\n" + "\n" + "class Bar {};\n\n"; + testFiles << TestDocument::create(original, expected, QLatin1String("file.h")); + + // Source File + original = + "#include \"file.h\"\n" + "\n" + "Foo::Foo()\n" + "{\n\n" + "}\n"; + expected = original + "\n"; + testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp")); + + MoveFuncDefOutside factory; + TestCase data(testFiles); + data.run(&factory, 1); +} + /// Check: revert test_quickfix_MoveFuncDefOutside_MemberFuncToCpp() void CppEditorPlugin::test_quickfix_MoveFuncDefToDecl_MemberFunc() { diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index e75b7776651a5ea9a3943bbaf8b17f4536917e8c..56f9189173110c2391a64fb0b32bb7289a86e5dc 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -161,15 +161,28 @@ InsertionLocation insertLocationForMethodDefinition(Symbol *symbol, } } - // ...failed, so return location at end of file + // ...failed, + // if class member try to get position right after class CppRefactoringFilePtr file = refactoring.file(fileName); + unsigned line = 0, column = 0; + if (Class *clazz = symbol->enclosingClass()) { + if (symbol->fileName() == fileName.toUtf8()) { + file->cppDocument()->translationUnit()->getPosition(clazz->endOffset(), &line, &column); + if (line != 0) { + ++column; // Skipping the ";" + return InsertionLocation(fileName, QLatin1String("\n\n"), QLatin1String(""), + line, column); + } + } + } + + // fall through: position at end of file const QTextDocument *doc = file->document(); int pos = qMax(0, doc->characterCount() - 1); //TODO watch for matching namespace //TODO watch for moc-includes - unsigned line, column; file->lineAndColumn(pos, &line, &column); return InsertionLocation(fileName, QLatin1String("\n\n"), QLatin1String("\n"), line, column); }