From 758f79923fc2d53fe480b0de5e2305d40d563732 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 5 Jun 2020 14:29:09 +0200 Subject: [PATCH] CppEditor: Fix operator refactoring The refactoring option was not triggered if the operator definition had a different amount of whitespace after "operator" than the declaration. Fixes: QTCREATORBUG-6236 Change-Id: Idf6438203e28d3f1effe0a0375d6563f813a9726 Reviewed-by: Cristian Adam --- .../cppeditor/cppfunctiondecldeflink.cpp | 25 ++++++++++++++++++- .../cppeditor/cppfunctiondecldeflink.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp index bb05d69bb73..d0494f8b0fe 100644 --- a/src/plugins/cppeditor/cppfunctiondecldeflink.cpp +++ b/src/plugins/cppeditor/cppfunctiondecldeflink.cpp @@ -573,7 +573,7 @@ ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targetOffse // abort if the name of the newly parsed function is not the expected one DeclaratorIdAST *newDeclId = getDeclaratorId(newDef->declarator); if (!newDeclId || !newDeclId->name || !newDeclId->name->name - || overview.prettyName(newDeclId->name->name) != nameInitial) { + || overview.prettyName(newDeclId->name->name) != normalizedInitialName()) { return changes; } @@ -978,5 +978,28 @@ ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targetOffse return changes; } +// Only has an effect with operators. +// Makes sure there is exactly one space between the "operator" string +// and the actual operator, as that is what it will be compared against. +QString FunctionDeclDefLink::normalizedInitialName() const +{ + QString n = nameInitial; + const QString op = "operator"; + int index = n.indexOf(op); + if (index == -1) + return n; + if (index > 0 && n.at(index - 1).isLetterOrNumber()) + return n; + index += op.length(); + if (index == n.length()) + return n; + if (n.at(index).isLetterOrNumber()) + return n; + n.insert(index++, ' '); + while (index < n.length() && n.at(index) == ' ') + n.remove(index, 1); + return n; +} + } // namespace Internal } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppfunctiondecldeflink.h b/src/plugins/cppeditor/cppfunctiondecldeflink.h index 0f11fabc8df..4b4dc053f03 100644 --- a/src/plugins/cppeditor/cppfunctiondecldeflink.h +++ b/src/plugins/cppeditor/cppfunctiondecldeflink.h @@ -104,6 +104,8 @@ public: CPlusPlus::FunctionDeclaratorAST *targetFunctionDeclarator = nullptr; private: + QString normalizedInitialName() const; + bool hasMarker = false; friend class FunctionDeclDefLinkFinder;