From c8d7c074f56922153923356581fcbd6d15f79110 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 27 May 2020 17:13:10 +0200 Subject: [PATCH] CppEditor: Consider namespaces in "extract function" quickfix Fixes: QTCREATORBUG-23256 Change-Id: I99b1271907767c3607e35adb49bd4109f3eca18c Reviewed-by: Nikolai Kosjar --- src/plugins/cppeditor/cppquickfix_test.cpp | 28 ++++++++++++++++++++++ src/plugins/cppeditor/cppquickfixes.cpp | 7 +++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 206e50d89d5..09207c1dbe6 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -5125,6 +5125,34 @@ void CppEditorPlugin::test_quickfix_ExtractFunction_data() "{\n" " extracted();\n" "}\n"); + + QTest::newRow("class in namespace") + << _("namespace NS {\n" + "class C {\n" + " void f(C &c);\n" + "};\n" + "}\n" + "void NS::C::f(NS::C &c)\n" + "{\n" + " @{start}C *c = &c;@{end}\n" + "}\n") + << _("namespace NS {\n" + "class C {\n" + " void f(C &c);\n" + "\n" + "public:\n" + " void extracted(NS::C &c);\n" // TODO: Remove non-required qualification + "};\n" + "}\n" + "void NS::C::extracted(NS::C &c)\n" + "{\n" + " C *c = &c;\n" + "}\n" + "\n" + "void NS::C::f(NS::C &c)\n" + "{\n" + " extracted(c);\n" + "}\n"); } void CppEditorPlugin::test_quickfix_ExtractFunction() diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index b52e7be4939..24c43d24ca2 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -3438,12 +3438,17 @@ public: // Write class qualification, if any. if (matchingClass) { - Class *current = matchingClass; + const Scope *current = matchingClass; QVector classes{matchingClass->name()}; while (current->enclosingScope()->asClass()) { current = current->enclosingScope()->asClass(); classes.prepend(current->name()); } + while (current->enclosingScope() && current->enclosingScope()->asNamespace()) { + current = current->enclosingScope()->asNamespace(); + if (current->name()) + classes.prepend(current->name()); + } for (const Name *n : classes) { const Name *name = rewriteName(n, &env, control); funcDef.append(printer.prettyName(name));