CppEditor: Fix "move definition" quickfix for template member functions

There are a lot more problems in this area (e.g. with nested classes),
but let's tackle them one by one.

Fixes: QTCREATORBUG-24801
Change-Id: I4b3805ea6f8b28373925693650150bbd89508096
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2020-10-26 15:07:55 +01:00
parent b5e9dd006a
commit d2ebc16b92
4 changed files with 49 additions and 2 deletions

View File

@@ -383,6 +383,31 @@ static bool endsWithPtrOrRef(const QString &type)
void TypePrettyPrinter::visit(Function *type)
{
if (_overview->showTemplateParameters) {
QStringList nameParts = _name.split("::");
int i = nameParts.length() - 1;
for (Scope *s = type->enclosingScope(); s && i >= 0; s = s->enclosingScope()) {
if (Template *templ = s->asTemplate()) {
QString &n = nameParts[i];
n += '<';
for (int index = 0; index < templ->templateParameterCount(); ++index) {
if (index)
n += QLatin1String(", ");
QString arg = _overview->prettyName(templ->templateParameterAt(index)->name());
if (arg.isEmpty()) {
arg += 'T';
arg += QString::number(index + 1);
}
n += arg;
}
n += '>';
}
if (s->identifier())
--i;
}
_name = nameParts.join("::");
}
if (_needsParens) {
_text.prepend(QLatin1Char('('));
if (! _name.isEmpty()) {
@@ -417,7 +442,10 @@ void TypePrettyPrinter::visit(Function *type)
if (TypenameArgument *typenameArg = param->asTypenameArgument()) {
templateScope.append(QLatin1String(typenameArg->isClassDeclarator()
? "class " : "typename "));
templateScope.append(_overview->prettyName(typenameArg->name()));
QString name = _overview->prettyName(typenameArg->name());
if (name.isEmpty())
name.append('T').append(QString::number(i + 1));
templateScope.append(name);
} else if (Argument *arg = param->asArgument()) {
templateScope.append(operator()(arg->type(),
_overview->prettyName(arg->name())));

View File

@@ -177,6 +177,7 @@ private slots:
void test_quickfix_MoveFuncDefOutside_respectWsInOperatorNames2();
void test_quickfix_MoveFuncDefOutside_macroUses();
void test_quickfix_MoveFuncDefOutside_template();
void test_quickfix_MoveFuncDefOutside_unnamedTemplate();
void test_quickfix_MoveAllFuncDefOutside_MemberFuncToCpp();
void test_quickfix_MoveAllFuncDefOutside_MemberFuncOutside();

View File

@@ -5573,7 +5573,24 @@ void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_template()
"class Foo { void fu@nc(); };\n"
"\n"
"template<class T>\n"
"void Foo::func() {}\n"; // Should be Foo<T>::func
"void Foo<T>::func() {}\n";
;
MoveFuncDefOutside factory;
QuickFixOperationTest(singleDocument(original, expected), &factory);
}
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_unnamedTemplate()
{
QByteArray original =
"template<typename T, typename>\n"
"class Foo { void fu@nc() {} };\n";
QByteArray expected =
"template<typename T, typename>\n"
"class Foo { void fu@nc(); };\n"
"\n"
"template<typename T, typename T2>\n"
"void Foo<T, T2>::func() {}\n";
;
MoveFuncDefOutside factory;

View File

@@ -5952,6 +5952,7 @@ QString definitionSignature(const CppQuickFixInterface *assist,
oo.showReturnTypes = true;
oo.showArgumentNames = true;
oo.showEnclosingTemplate = true;
oo.showTemplateParameters = true;
const Name *name = func->name();
if (name && nameIncludesOperatorName(name)) {
CoreDeclaratorAST *coreDeclarator = functionDefinitionAST->declarator->core_declarator;