CppEditor: Fix moving function definition out of specialized class

Fixes: QTCREATORBUG-25808
Change-Id: I5950c7f66b736c6ee30411e8fdc0356d78faa518
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-07-26 10:33:51 +02:00
parent 20b5182665
commit c5d080570b
3 changed files with 46 additions and 12 deletions

View File

@@ -389,11 +389,14 @@ void TypePrettyPrinter::visit(Function *type)
for (Scope *s = type->enclosingScope(); s && i >= 0; s = s->enclosingScope()) { for (Scope *s = type->enclosingScope(); s && i >= 0; s = s->enclosingScope()) {
if (Template *templ = s->asTemplate()) { if (Template *templ = s->asTemplate()) {
QString &n = nameParts[i]; QString &n = nameParts[i];
const int paramCount = templ->templateParameterCount();
if (paramCount > 0) {
n += '<'; n += '<';
for (int index = 0; index < templ->templateParameterCount(); ++index) { for (int index = 0; index < paramCount; ++index) {
if (index) if (index)
n += QLatin1String(", "); n += QLatin1String(", ");
QString arg = _overview->prettyName(templ->templateParameterAt(index)->name()); QString arg = _overview->prettyName(
templ->templateParameterAt(index)->name());
if (arg.isEmpty()) { if (arg.isEmpty()) {
arg += 'T'; arg += 'T';
arg += QString::number(index + 1); arg += QString::number(index + 1);
@@ -402,6 +405,7 @@ void TypePrettyPrinter::visit(Function *type)
} }
n += '>'; n += '>';
} }
}
if (s->identifier()) if (s->identifier())
--i; --i;
} }
@@ -435,7 +439,8 @@ void TypePrettyPrinter::visit(Function *type)
if (_overview->showEnclosingTemplate) { if (_overview->showEnclosingTemplate) {
if (Template *templ = type->enclosingTemplate()) { if (Template *templ = type->enclosingTemplate()) {
QString templateScope = "template<"; QString templateScope = "template<";
for (int i = 0, total = templ->templateParameterCount(); i < total; ++i) { const int paramCount = templ->templateParameterCount();
for (int i = 0; i < paramCount; ++i) {
if (Symbol *param = templ->templateParameterAt(i)) { if (Symbol *param = templ->templateParameterAt(i)) {
if (i > 0) if (i > 0)
templateScope.append(", "); templateScope.append(", ");
@@ -452,6 +457,7 @@ void TypePrettyPrinter::visit(Function *type)
} }
} }
} }
if (paramCount > 0)
_text.prepend(templateScope + ">\n"); _text.prepend(templateScope + ">\n");
} }
} }

View File

@@ -203,6 +203,7 @@ private slots:
void test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames2(); void test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames2();
void test_quickfix_MoveFuncDefOutside_macroUses(); void test_quickfix_MoveFuncDefOutside_macroUses();
void test_quickfix_MoveFuncDefOutside_template(); void test_quickfix_MoveFuncDefOutside_template();
void test_quickfix_MoveFuncDefOutside_template_specializedClass();
void test_quickfix_MoveFuncDefOutside_unnamedTemplate(); void test_quickfix_MoveFuncDefOutside_unnamedTemplate();
void test_quickfix_MoveFuncDefOutside_MemberFuncToCpp_Static(); void test_quickfix_MoveFuncDefOutside_MemberFuncToCpp_Static();
void test_quickfix_MoveFuncDefOutside_MemberFuncToCpp_WithInlinePartOfName(); void test_quickfix_MoveFuncDefOutside_MemberFuncToCpp_WithInlinePartOfName();

View File

@@ -6521,6 +6521,33 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_template()
QuickFixOperationTest(singleDocument(original, expected), &factory); QuickFixOperationTest(singleDocument(original, expected), &factory);
} }
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_template_specializedClass()
{
QByteArray original = R"(
template<typename T> class base {};
template<>
class base<int>
{
public:
void @bar() {}
};
)";
QByteArray expected = R"(
template<typename T> class base {};
template<>
class base<int>
{
public:
void bar();
};
void base<int>::bar() {}
)";
MoveFuncDefOutside factory;
QuickFixOperationTest(singleDocument(original, expected), &factory);
}
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_unnamedTemplate() void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_unnamedTemplate()
{ {
QByteArray original = QByteArray original =