From e4100c31fc2cf1e96a70a4a9783e46e94e869c64 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 22 Jul 2020 16:37:18 +0200 Subject: [PATCH] CppEditor: Fix trailing whitespace after extracting a function ... from a block of code. We must not insert an empty line into the newly created function, because it gets indented in the first indent operation, resulting in trailing characters after the actual code has been inserted later. Fixes: QTCREATORBUG-12118 Change-Id: If438d4de379cae90baa846c112866f2777b44fce Reviewed-by: Christian Stenger --- src/plugins/cppeditor/cppquickfix_test.cpp | 20 ++++++++++++++++++++ src/plugins/cppeditor/cppquickfixes.cpp | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 402f1d01eac..558efbee274 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -5627,6 +5627,26 @@ void CppEditorPlugin::test_quickfix_ExtractFunction_data() "{\n" " extracted(c);\n" "}\n"); + + QTest::newRow("if-block") + << _("inline void func()\n" + "{\n" + " int dummy = 0;\n" + " @{start}if@{end} (dummy < 10) {\n" + " ++dummy;\n" + " }\n" + "}\n") + << _("inline void extracted(int dummy)\n" + "{\n" + " if (dummy < 10) {\n" + " ++dummy;\n" + " }\n" + "}\n\n" + "inline void func()\n" + "{\n" + " int dummy = 0;\n" + " extracted(dummy);\n" + "}\n"); } void CppEditorPlugin::test_quickfix_ExtractFunction() diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 8c65166aa1d..e259810182c 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -3885,7 +3885,7 @@ public: funcDef.append(QLatin1String(" const")); funcDecl.append(QLatin1String(" const")); } - funcDef.append(QLatin1String("\n{\n")); + funcDef.append(QLatin1String("\n{")); if (matchingClass) funcDecl.append(QLatin1String(";\n")); if (m_funcReturn) { @@ -3925,7 +3925,7 @@ public: QTC_ASSERT(tc.hasSelection(), return); position = tc.selectionEnd() + 1; change.clear(); - change.insert(position, extract); + change.insert(position, extract + '\n'); currentFile->setChangeSet(change); currentFile->appendReindentRange(ChangeSet::Range(position, position + 1)); currentFile->apply();