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: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2023-08-31 16:14:40 +02:00
parent 429da62c1a
commit 6847d69008
2 changed files with 14 additions and 1 deletions

View File

@@ -7843,7 +7843,8 @@ void QuickfixTest::testExtractFunction_data()
QTest::addColumn<QByteArray>("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"

View File

@@ -27,6 +27,7 @@
#include <cplusplus/ASTPath.h>
#include <cplusplus/CPlusPlusForwardDeclarations.h>
#include <cplusplus/CppRewriter.h>
#include <cplusplus/declarationcomments.h>
#include <cplusplus/NamePrettyPrinter.h>
#include <cplusplus/TypeOfExpression.h>
#include <cplusplus/TypePrettyPrinter.h>
@@ -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<Token> 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);