CppEditor: Consider parameter comments in "apply changes to decl/def"

That is, when a function parameter name is changed in the declaration,
apply the renaming also to the function comments at the definition and
vice versa.

Task-number: QTCREATORBUG-12051
Task-number: QTCREATORBUG-15425
Change-Id: I9104512ef8cdca8c37e4f8ac87117476f850117a
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2023-08-31 15:27:55 +02:00
parent 4ee863060a
commit 429da62c1a

View File

@@ -9,6 +9,7 @@
#include "cppeditorwidget.h"
#include "cpplocalsymbols.h"
#include "cppquickfixassistant.h"
#include "cpptoolsreuse.h"
#include "symbolfinder.h"
#include <coreplugin/actionmanager/actionmanager.h>
@@ -18,12 +19,14 @@
#include <cplusplus/ASTPath.h>
#include <cplusplus/CppRewriter.h>
#include <cplusplus/declarationcomments.h>
#include <cplusplus/Overview.h>
#include <cplusplus/TypeOfExpression.h>
#include <utils/async.h>
#include <utils/proxyaction.h>
#include <utils/qtcassert.h>
#include <utils/textutils.h>
#include <utils/tooltip/tooltip.h>
#include <QRegularExpression>
@@ -847,6 +850,41 @@ ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targetOffse
newTargetParameters);
}
// Change parameter names in function documentation.
[&] {
if (renamedTargetParameters.isEmpty())
return;
const QList<Token> functionComments = commentsForDeclaration(
targetFunction, targetDeclaration, *targetFile->document(),
targetFile->cppDocument());
if (functionComments.isEmpty())
return;
const QString &content = targetFile->document()->toPlainText();
const QStringView docView = QStringView(content);
for (auto it = renamedTargetParameters.cbegin();
it != renamedTargetParameters.cend(); ++it) {
const QString paramName = Overview().prettyName(it.key()->name());
for (const Token &tok : functionComments) {
const TranslationUnit * const tu = targetFile->cppDocument()->translationUnit();
const int tokenStartPos = tu->getTokenPositionInDocument(
tok, targetFile->document());
const int tokenEndPos = tu->getTokenEndPositionInDocument(
tok, targetFile->document());
const QStringView tokenView = docView.mid(tokenStartPos,
tokenEndPos - tokenStartPos);
const QList<Text::Range> ranges = symbolOccurrencesInText(
*targetFile->document(), tokenView, tokenStartPos, paramName);
for (const Text::Range &r : ranges) {
const int startPos = Text::positionInText(
targetFile->document(), r.begin.line, r.begin.column + 1);
const int endPos = Text::positionInText(
targetFile->document(), r.end.line, r.end.column + 1);
changes.replace(startPos, endPos, it.value());
}
}
}
}();
// for function definitions, rename the local usages
FunctionDefinitionAST *targetDefinition = targetDeclaration->asFunctionDefinition();
if (targetDefinition && !renamedTargetParameters.isEmpty()) {