forked from qt-creator/qt-creator
CppEditor: Fix added newlines for GenerateGetterSetter quick fix
As a nice side effect superfluous new lines - introduced by quick fixes that are using InsertionPointLocator::methodDefinition - vanished. Task-number: QTCREATORBUG-13872 Change-Id: Ib3df2b2acbc22449f16f4444092a57ae93d53d35 Reviewed-by: Jochen Becher <jochen_becher@gmx.de> Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
committed by
Nikolai Kosjar
parent
03212d6e49
commit
7879aa5fae
@@ -567,6 +567,7 @@ void CppEditorPlugin::test_quickfix_data()
|
|||||||
" int getIt() const;\n"
|
" int getIt() const;\n"
|
||||||
" void setIt(int value);\n"
|
" void setIt(int value);\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
|
"\n"
|
||||||
"int Something::getIt() const\n"
|
"int Something::getIt() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return it;\n"
|
" return it;\n"
|
||||||
@@ -1098,7 +1099,6 @@ void CppEditorPlugin::test_quickfix_data()
|
|||||||
<< CppQuickFixFactoryPtr(new InsertDefFromDecl) << original
|
<< CppQuickFixFactoryPtr(new InsertDefFromDecl) << original
|
||||||
<< original + _(
|
<< original + _(
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"Foo::Foo()\n"
|
"Foo::Foo()\n"
|
||||||
"{\n\n"
|
"{\n\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
@@ -1772,6 +1772,7 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAn
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"namespace SomeNamespace {\n"
|
"namespace SomeNamespace {\n"
|
||||||
|
"\n"
|
||||||
"int Something::getIt() const\n"
|
"int Something::getIt() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return it;\n"
|
" return it;\n"
|
||||||
@@ -1780,7 +1781,8 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAn
|
|||||||
"void Something::setIt(int value)\n"
|
"void Something::setIt(int value)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" it = value;\n"
|
" it = value;\n"
|
||||||
"}\n\n"
|
"}\n"
|
||||||
|
"\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
|
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
|
||||||
|
|
||||||
@@ -2040,7 +2042,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_basic2()
|
|||||||
"\n"
|
"\n"
|
||||||
"int x;\n"
|
"int x;\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"void f()\n"
|
"void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -2070,7 +2071,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_basic3()
|
|||||||
" Foo()@;\n"
|
" Foo()@;\n"
|
||||||
"};\n";
|
"};\n";
|
||||||
expected = original +
|
expected = original +
|
||||||
"\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"Foo::Foo()\n"
|
"Foo::Foo()\n"
|
||||||
"{\n\n"
|
"{\n\n"
|
||||||
@@ -2142,7 +2142,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_namespace2()
|
|||||||
"using namespace N;\n"
|
"using namespace N;\n"
|
||||||
;
|
;
|
||||||
expected = original +
|
expected = original +
|
||||||
"\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"Foo::Foo()\n"
|
"Foo::Foo()\n"
|
||||||
"{\n\n"
|
"{\n\n"
|
||||||
@@ -2304,7 +2303,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_respectWsInOperatorNames1(
|
|||||||
" Foo &operator =();\n"
|
" Foo &operator =();\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"Foo &Foo::operator =()\n"
|
"Foo &Foo::operator =()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -2328,7 +2326,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_respectWsInOperatorNames2(
|
|||||||
" Foo &operator=();\n"
|
" Foo &operator=();\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"Foo &Foo::operator=()\n"
|
"Foo &Foo::operator=()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -2512,7 +2509,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_findImplementationFile()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"void Foo::baz()\n"
|
"void Foo::baz()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -2551,7 +2547,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_unicodeIdentifier()
|
|||||||
;
|
;
|
||||||
expected = original;
|
expected = original;
|
||||||
expected +=
|
expected +=
|
||||||
"\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"void Foo::" TEST_UNICODE_IDENTIFIER "()\n"
|
"void Foo::" TEST_UNICODE_IDENTIFIER "()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
@@ -3372,7 +3367,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCpp()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"int Foo::number() const\n"
|
"int Foo::number() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 5;\n"
|
" return 5;\n"
|
||||||
@@ -3537,7 +3531,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNS()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"int MyNs::Foo::number() const\n"
|
"int MyNs::Foo::number() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 5;\n"
|
" return 5;\n"
|
||||||
@@ -3581,7 +3574,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNSUsing()
|
|||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"using namespace MyNs;\n"
|
"using namespace MyNs;\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"int Foo::number() const\n"
|
"int Foo::number() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 5;\n"
|
" return 5;\n"
|
||||||
@@ -3643,7 +3635,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCpp()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"int number() const\n"
|
"int number() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 5;\n"
|
" return 5;\n"
|
||||||
@@ -3681,7 +3672,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"int MyNamespace::number() const\n"
|
"int MyNamespace::number() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 5;\n"
|
" return 5;\n"
|
||||||
@@ -3723,7 +3713,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization1()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"Foo::Foo() : a(42), b(3.141) {}\n"
|
"Foo::Foo() : a(42), b(3.141) {}\n"
|
||||||
;
|
;
|
||||||
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
|
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
|
||||||
@@ -3766,7 +3755,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization2()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"Foo::Foo() : member(2)\n"
|
"Foo::Foo() : member(2)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
@@ -3833,7 +3821,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames1
|
|||||||
" Foo &operator =();\n"
|
" Foo &operator =();\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"Foo &Foo::operator =() {}\n"
|
"Foo &Foo::operator =() {}\n"
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -3855,7 +3842,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames2
|
|||||||
" Foo &operator=();\n"
|
" Foo &operator=();\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"Foo &Foo::operator=() {}\n"
|
"Foo &Foo::operator=() {}\n"
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -4292,12 +4278,11 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_MemberFuncToCpp()
|
|||||||
expected =
|
expected =
|
||||||
"#include \"file.h\"\n"
|
"#include \"file.h\"\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\n"
|
|
||||||
"int Foo::numberA() const\n"
|
"int Foo::numberA() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 5;\n"
|
" return 5;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n\n"
|
"\n"
|
||||||
"int Foo::numberB() const\n"
|
"int Foo::numberB() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 5;\n"
|
" return 5;\n"
|
||||||
@@ -4328,12 +4313,12 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_MemberFuncOutside()
|
|||||||
" int f1();\n"
|
" int f1();\n"
|
||||||
" int f2() const;\n"
|
" int f2() const;\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n\n"
|
"\n"
|
||||||
"int Foo::f1()\n"
|
"int Foo::f1()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 1;\n"
|
" return 1;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n\n"
|
"\n"
|
||||||
"int Foo::f2() const\n"
|
"int Foo::f2() const\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 2;\n"
|
" return 2;\n"
|
||||||
@@ -4375,7 +4360,7 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_classWithBaseClass()
|
|||||||
"class Foo : public Bar {\n"
|
"class Foo : public Bar {\n"
|
||||||
" int f1();\n"
|
" int f1();\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n\n"
|
"\n"
|
||||||
"int Foo::f1()\n"
|
"int Foo::f1()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 1;\n"
|
" return 1;\n"
|
||||||
@@ -4403,7 +4388,7 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_ignoreMacroCode()
|
|||||||
" FAKE_Q_OBJECT\n"
|
" FAKE_Q_OBJECT\n"
|
||||||
" int f1();\n"
|
" int f1();\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n\n"
|
"\n"
|
||||||
"int Foo::f1()\n"
|
"int Foo::f1()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" return 1;\n"
|
" return 1;\n"
|
||||||
|
|||||||
@@ -3046,26 +3046,30 @@ public:
|
|||||||
const QString implementationGetterTypeAndNameString = oo.prettyType(
|
const QString implementationGetterTypeAndNameString = oo.prettyType(
|
||||||
getterType, QString::fromLatin1("%1::%2").arg(classString, m_getterName));
|
getterType, QString::fromLatin1("%1::%2").arg(classString, m_getterName));
|
||||||
const QString implementationGetter = QString::fromLatin1(
|
const QString implementationGetter = QString::fromLatin1(
|
||||||
"\n%1()%2\n"
|
"%1()%2\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"return %3;\n"
|
"return %3;\n"
|
||||||
"}\n")
|
"}")
|
||||||
.arg(implementationGetterTypeAndNameString)
|
.arg(implementationGetterTypeAndNameString)
|
||||||
.arg(isStatic ? QString() : QLatin1String(" const"))
|
.arg(isStatic ? QString() : QLatin1String(" const"))
|
||||||
.arg(m_variableString);
|
.arg(m_variableString);
|
||||||
const QString implementationSetter = QString::fromLatin1(
|
const QString implementationSetter = QString::fromLatin1(
|
||||||
"\nvoid %1::%2(%3)\n"
|
"void %1::%2(%3)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
"%4 = %5;\n"
|
"%4 = %5;\n"
|
||||||
"}\n")
|
"}")
|
||||||
.arg(classString).arg(m_setterName)
|
.arg(classString).arg(m_setterName)
|
||||||
.arg(paramString).arg(m_variableString)
|
.arg(paramString).arg(m_variableString)
|
||||||
.arg(paramName);
|
.arg(paramName);
|
||||||
|
|
||||||
QString implementation;
|
QString implementation;
|
||||||
if (generateGetter())
|
if (generateGetter())
|
||||||
implementation += implementationGetter;
|
implementation += implementationGetter;
|
||||||
if (generateSetter())
|
if (generateSetter() && !fullySpecifiedType.isConst()) {
|
||||||
|
if (!implementation.isEmpty())
|
||||||
|
implementation += QLatin1String("\n\n");
|
||||||
implementation += implementationSetter;
|
implementation += implementationSetter;
|
||||||
|
}
|
||||||
|
|
||||||
// Create and apply changes
|
// Create and apply changes
|
||||||
ChangeSet currChanges;
|
ChangeSet currChanges;
|
||||||
@@ -3076,6 +3080,7 @@ public:
|
|||||||
if (sameFile) {
|
if (sameFile) {
|
||||||
InsertionLocation loc = insertLocationForMethodDefinition(m_symbol, false, refactoring,
|
InsertionLocation loc = insertLocationForMethodDefinition(m_symbol, false, refactoring,
|
||||||
currentFile->fileName());
|
currentFile->fileName());
|
||||||
|
implementation = loc.prefix() + implementation + loc.suffix();
|
||||||
currChanges.insert(currentFile->position(loc.line(), loc.column()), implementation);
|
currChanges.insert(currentFile->position(loc.line(), loc.column()), implementation);
|
||||||
} else {
|
} else {
|
||||||
CppRefactoringChanges implRef(snapshot());
|
CppRefactoringChanges implRef(snapshot());
|
||||||
@@ -3083,6 +3088,7 @@ public:
|
|||||||
ChangeSet implChanges;
|
ChangeSet implChanges;
|
||||||
InsertionLocation loc = insertLocationForMethodDefinition(m_symbol, false,
|
InsertionLocation loc = insertLocationForMethodDefinition(m_symbol, false,
|
||||||
implRef, implFileName);
|
implRef, implFileName);
|
||||||
|
implementation = loc.prefix() + implementation + loc.suffix();
|
||||||
const int implInsertPos = implFile->position(loc.line(), loc.column());
|
const int implInsertPos = implFile->position(loc.line(), loc.column());
|
||||||
implChanges.insert(implInsertPos, implementation);
|
implChanges.insert(implInsertPos, implementation);
|
||||||
implFile->setChangeSet(implChanges);
|
implFile->setChangeSet(implChanges);
|
||||||
|
|||||||
@@ -617,8 +617,16 @@ QList<InsertionLocation> InsertionPointLocator::methodDefinition(Symbol *declara
|
|||||||
} else {
|
} else {
|
||||||
QTC_ASSERT(column, return result);
|
QTC_ASSERT(column, return result);
|
||||||
|
|
||||||
prefix = QLatin1String("\n\n");
|
|
||||||
int firstNonSpace = targetFile->position(line, column);
|
int firstNonSpace = targetFile->position(line, column);
|
||||||
|
prefix = QLatin1String("\n\n");
|
||||||
|
// Only one new line if at the end of file
|
||||||
|
if (const QTextDocument *doc = targetFile->document()) {
|
||||||
|
if (firstNonSpace + 1 == doc->characterCount() /* + 1 because zero based index */
|
||||||
|
&& doc->characterAt(firstNonSpace) == QChar::ParagraphSeparator) {
|
||||||
|
prefix = QLatin1String("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QChar c = targetFile->charAt(firstNonSpace);
|
QChar c = targetFile->charAt(firstNonSpace);
|
||||||
while (c == QLatin1Char(' ') || c == QLatin1Char('\t')) {
|
while (c == QLatin1Char(' ') || c == QLatin1Char('\t')) {
|
||||||
++firstNonSpace;
|
++firstNonSpace;
|
||||||
|
|||||||
Reference in New Issue
Block a user