From 6847d69008952837831b14302e13c3dd0a60146b Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 31 Aug 2023 16:14:40 +0200 Subject: [PATCH] CppEditor: Consider comments when extracting a new function That is, do not place the newly created function in between the function and its comment. Fixes: QTCREATORBUG-6934 Change-Id: I79f564a90c7e3c45188f5d694cbde47029651324 Reviewed-by: Reviewed-by: David Schulz --- src/plugins/cppeditor/cppquickfix_test.cpp | 4 +++- src/plugins/cppeditor/cppquickfixes.cpp | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 72f4f1b0833..5558e56336c 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -7843,7 +7843,8 @@ void QuickfixTest::testExtractFunction_data() QTest::addColumn("expected"); QTest::newRow("basic") - << _("void f()\n" + << _("// Documentation for f\n" + "void f()\n" "{\n" " @{start}g();@{end}\n" "}\n") @@ -7852,6 +7853,7 @@ void QuickfixTest::testExtractFunction_data() " g();\n" "}\n" "\n" + "// Documentation for f\n" "void f()\n" "{\n" " extracted();\n" diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 727f0c84970..5bf8269af46 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -5150,6 +5151,16 @@ public: // formatting) it's simpler to have two different change sets. ChangeSet change; int position = currentFile->startOf(m_refFuncDef); + + // Do not insert right between the function and an associated comment. + const QList functionDoc = commentsForDeclaration( + m_refFuncDef->symbol, m_refFuncDef, *currentFile->document(), + currentFile->cppDocument()); + if (!functionDoc.isEmpty()) { + position = currentFile->cppDocument()->translationUnit()->getTokenPositionInDocument( + functionDoc.first(), currentFile->document()); + } + change.insert(position, funcDef); change.replace(m_extractionStart, m_extractionEnd, funcCall); currentFile->setChangeSet(change);