From 7879aa5fae37968173b0c29d06ea4bade85c358e Mon Sep 17 00:00:00 2001 From: Lorenz Haas <lykurg@gmail.com> Date: Sun, 1 Feb 2015 11:32:45 +0100 Subject: [PATCH] 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> --- src/plugins/cppeditor/cppquickfix_test.cpp | 33 +++++-------------- src/plugins/cppeditor/cppquickfixes.cpp | 16 ++++++--- .../cpptools/insertionpointlocator.cpp | 10 +++++- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 84e30d751d6..38e4d22d7e7 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -567,6 +567,7 @@ void CppEditorPlugin::test_quickfix_data() " int getIt() const;\n" " void setIt(int value);\n" "};\n" + "\n" "int Something::getIt() const\n" "{\n" " return it;\n" @@ -1098,7 +1099,6 @@ void CppEditorPlugin::test_quickfix_data() << CppQuickFixFactoryPtr(new InsertDefFromDecl) << original << original + _( "\n" - "\n" "Foo::Foo()\n" "{\n\n" "}\n" @@ -1772,6 +1772,7 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAn expected = "#include \"file.h\"\n" "namespace SomeNamespace {\n" + "\n" "int Something::getIt() const\n" "{\n" " return it;\n" @@ -1780,7 +1781,8 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAn "void Something::setIt(int value)\n" "{\n" " it = value;\n" - "}\n\n" + "}\n" + "\n" "}\n"; testDocuments << QuickFixTestDocument::create("file.cpp", original, expected); @@ -2040,7 +2042,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_basic2() "\n" "int x;\n" "\n" - "\n" "void f()\n" "{\n" "\n" @@ -2070,7 +2071,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_basic3() " Foo()@;\n" "};\n"; expected = original + - "\n" "\n" "Foo::Foo()\n" "{\n\n" @@ -2142,7 +2142,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_headerSource_namespace2() "using namespace N;\n" ; expected = original + - "\n" "\n" "Foo::Foo()\n" "{\n\n" @@ -2304,7 +2303,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_respectWsInOperatorNames1( " Foo &operator =();\n" "};\n" "\n" - "\n" "Foo &Foo::operator =()\n" "{\n" "\n" @@ -2328,7 +2326,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_respectWsInOperatorNames2( " Foo &operator=();\n" "};\n" "\n" - "\n" "Foo &Foo::operator=()\n" "{\n" "\n" @@ -2512,7 +2509,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_findImplementationFile() expected = "#include \"file.h\"\n" "\n" - "\n" "void Foo::baz()\n" "{\n" "\n" @@ -2551,7 +2547,6 @@ void CppEditorPlugin::test_quickfix_InsertDefFromDecl_unicodeIdentifier() ; expected = original; expected += - "\n" "\n" "void Foo::" TEST_UNICODE_IDENTIFIER "()\n" "{\n" @@ -3372,7 +3367,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCpp() expected = "#include \"file.h\"\n" "\n" - "\n" "int Foo::number() const\n" "{\n" " return 5;\n" @@ -3537,7 +3531,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNS() expected = "#include \"file.h\"\n" "\n" - "\n" "int MyNs::Foo::number() const\n" "{\n" " return 5;\n" @@ -3581,7 +3574,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCppNSUsing() "#include \"file.h\"\n" "using namespace MyNs;\n" "\n" - "\n" "int Foo::number() const\n" "{\n" " return 5;\n" @@ -3643,7 +3635,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCpp() expected = "#include \"file.h\"\n" "\n" - "\n" "int number() const\n" "{\n" " return 5;\n" @@ -3681,7 +3672,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_FreeFuncToCppNS() expected = "#include \"file.h\"\n" "\n" - "\n" "int MyNamespace::number() const\n" "{\n" " return 5;\n" @@ -3723,7 +3713,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization1() expected = "#include \"file.h\"\n" "\n" - "\n" "Foo::Foo() : a(42), b(3.141) {}\n" ; testDocuments << QuickFixTestDocument::create("file.cpp", original, expected); @@ -3766,7 +3755,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_CtorWithInitialization2() expected = "#include \"file.h\"\n" "\n" - "\n" "Foo::Foo() : member(2)\n" "{\n" "}\n" @@ -3833,7 +3821,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames1 " Foo &operator =();\n" "};\n" "\n" - "\n" "Foo &Foo::operator =() {}\n" ; @@ -3855,7 +3842,6 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames2 " Foo &operator=();\n" "};\n" "\n" - "\n" "Foo &Foo::operator=() {}\n" ; @@ -4292,12 +4278,11 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_MemberFuncToCpp() expected = "#include \"file.h\"\n" "\n" - "\n" "int Foo::numberA() const\n" "{\n" " return 5;\n" "}\n" - "\n\n" + "\n" "int Foo::numberB() const\n" "{\n" " return 5;\n" @@ -4328,12 +4313,12 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_MemberFuncOutside() " int f1();\n" " int f2() const;\n" "};\n" - "\n\n" + "\n" "int Foo::f1()\n" "{\n" " return 1;\n" "}\n" - "\n\n" + "\n" "int Foo::f2() const\n" "{\n" " return 2;\n" @@ -4375,7 +4360,7 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_classWithBaseClass() "class Foo : public Bar {\n" " int f1();\n" "};\n" - "\n\n" + "\n" "int Foo::f1()\n" "{\n" " return 1;\n" @@ -4403,7 +4388,7 @@ void CppEditorPlugin::test_quickfix_MoveAllFuncDefOutside_ignoreMacroCode() " FAKE_Q_OBJECT\n" " int f1();\n" "};\n" - "\n\n" + "\n" "int Foo::f1()\n" "{\n" " return 1;\n" diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 7d408ebf9a3..98516a3aedf 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -3046,26 +3046,30 @@ public: const QString implementationGetterTypeAndNameString = oo.prettyType( getterType, QString::fromLatin1("%1::%2").arg(classString, m_getterName)); const QString implementationGetter = QString::fromLatin1( - "\n%1()%2\n" + "%1()%2\n" "{\n" "return %3;\n" - "}\n") + "}") .arg(implementationGetterTypeAndNameString) .arg(isStatic ? QString() : QLatin1String(" const")) .arg(m_variableString); const QString implementationSetter = QString::fromLatin1( - "\nvoid %1::%2(%3)\n" + "void %1::%2(%3)\n" "{\n" "%4 = %5;\n" - "}\n") + "}") .arg(classString).arg(m_setterName) .arg(paramString).arg(m_variableString) .arg(paramName); + QString implementation; if (generateGetter()) implementation += implementationGetter; - if (generateSetter()) + if (generateSetter() && !fullySpecifiedType.isConst()) { + if (!implementation.isEmpty()) + implementation += QLatin1String("\n\n"); implementation += implementationSetter; + } // Create and apply changes ChangeSet currChanges; @@ -3076,6 +3080,7 @@ public: if (sameFile) { InsertionLocation loc = insertLocationForMethodDefinition(m_symbol, false, refactoring, currentFile->fileName()); + implementation = loc.prefix() + implementation + loc.suffix(); currChanges.insert(currentFile->position(loc.line(), loc.column()), implementation); } else { CppRefactoringChanges implRef(snapshot()); @@ -3083,6 +3088,7 @@ public: ChangeSet implChanges; InsertionLocation loc = insertLocationForMethodDefinition(m_symbol, false, implRef, implFileName); + implementation = loc.prefix() + implementation + loc.suffix(); const int implInsertPos = implFile->position(loc.line(), loc.column()); implChanges.insert(implInsertPos, implementation); implFile->setChangeSet(implChanges); diff --git a/src/plugins/cpptools/insertionpointlocator.cpp b/src/plugins/cpptools/insertionpointlocator.cpp index 529eada08c9..2301fc05383 100644 --- a/src/plugins/cpptools/insertionpointlocator.cpp +++ b/src/plugins/cpptools/insertionpointlocator.cpp @@ -617,8 +617,16 @@ QList<InsertionLocation> InsertionPointLocator::methodDefinition(Symbol *declara } else { QTC_ASSERT(column, return result); - prefix = QLatin1String("\n\n"); 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); while (c == QLatin1Char(' ') || c == QLatin1Char('\t')) { ++firstNonSpace; -- GitLab