From 429da62c1a54607fc2e784cae058cdbc3196bbe4 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 31 Aug 2023 15:27:55 +0200 Subject: [PATCH] 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 --- .../cppeditor/cppfunctiondecldeflink.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp index aa17721ea37..7055b00d038 100644 --- a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp +++ b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp @@ -9,6 +9,7 @@ #include "cppeditorwidget.h" #include "cpplocalsymbols.h" #include "cppquickfixassistant.h" +#include "cpptoolsreuse.h" #include "symbolfinder.h" #include @@ -18,12 +19,14 @@ #include #include +#include #include #include #include #include #include +#include #include #include @@ -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 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 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()) {