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

CppEditor: Insert/move member definition right after enclosing class



When triggering move or insert definition outside and no other class
symbol is found in the file, the definition is moved or inserted right
after the enclosing Class.

Change-Id: I74ffa872015753ba7372e21b868d3cf7fa49e84a
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 6558df08
......@@ -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();
......
......@@ -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()
{
......
......@@ -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);
}
......
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