diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index ea58b290e48..66b94fe6fcf 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -147,6 +147,8 @@ private slots: void test_quickfix_InsertDefFromDecl_unicodeIdentifier(); void test_quickfix_InsertDeclFromDef(); + void test_quickfix_InsertDeclFromDef_templateFuncTypename(); + void test_quickfix_InsertDeclFromDef_templateFuncInt(); void test_quickfix_InsertDeclFromDef_notTriggeredForTemplateFunc(); void test_quickfix_AddIncludeForUndefinedIdentifier_data(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index 5b2c7a9e258..d9fd1d38b33 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -2715,6 +2715,56 @@ void CppEditorPlugin::test_quickfix_InsertDeclFromDef() insertToSectionDeclFromDef("private slots", 5); } +void CppEditorPlugin::test_quickfix_InsertDeclFromDef_templateFuncTypename() +{ + QByteArray original = + "class Foo\n" + "{\n" + "};\n" + "\n" + "template\n" + "void Foo::fu@nc() {}\n"; + + QByteArray expected = + "class Foo\n" + "{\n" + "public:\n" + " template\n" + " void func();\n" + "};\n" + "\n" + "template\n" + "void Foo::fu@nc() {}\n"; + + InsertDeclFromDef factory; + QuickFixOperationTest(singleDocument(original, expected), &factory, {}, 0); +} + +void CppEditorPlugin::test_quickfix_InsertDeclFromDef_templateFuncInt() +{ + QByteArray original = + "class Foo\n" + "{\n" + "};\n" + "\n" + "template\n" + "void Foo::fu@nc() {}\n"; + + QByteArray expected = + "class Foo\n" + "{\n" + "public:\n" + " template\n" + " void func();\n" + "};\n" + "\n" + "template\n" + "void Foo::fu@nc() {}\n"; + + InsertDeclFromDef factory; + QuickFixOperationTest(singleDocument(original, expected), &factory, {}, 0); +} + void CppEditorPlugin::test_quickfix_InsertDeclFromDef_notTriggeredForTemplateFunc() { QByteArray contents = diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 371ec66f8e2..d281fb68206 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -2508,9 +2508,15 @@ QString InsertDeclOperation::generateDeclaration(const Function *function) oo.showFunctionSignatures = true; oo.showReturnTypes = true; oo.showArgumentNames = true; + oo.showEnclosingTemplate = true; + const Symbol *symbol = function; + if (const Scope *enclosingScope = function->enclosingScope()) { + if (const Template *templ = enclosingScope->asTemplate()) + symbol = templ; + } QString decl; - decl += oo.prettyType(function->type(), function->unqualifiedName()); + decl += oo.prettyType(symbol->type(), function->unqualifiedName()); decl += QLatin1String(";\n"); return decl;