CppEditor: Use trailing return type when moving function definition

... if the original definition also used a trailing return type.

Task-number: QTCREATORBUG-27132
Change-Id: Iaeeeac08601f1d931aabe12be9f89ca0240d97a2
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Christian Kandeler
2022-04-05 12:57:17 +02:00
parent 00df4c6675
commit b8144e0c51
5 changed files with 27 additions and 6 deletions

View File

@@ -86,6 +86,7 @@ Overview::Overview()
showTemplateParameters(false), showTemplateParameters(false),
showEnclosingTemplate(false), showEnclosingTemplate(false),
includeWhiteSpaceInOperatorName(true), includeWhiteSpaceInOperatorName(true),
trailingReturnType(false),
markedArgument(0), markedArgument(0),
markedArgumentBegin(0), markedArgumentBegin(0),
markedArgumentEnd(0) markedArgumentEnd(0)

View File

@@ -69,6 +69,7 @@ public:
bool showTemplateParameters: 1; bool showTemplateParameters: 1;
bool showEnclosingTemplate: 1; bool showEnclosingTemplate: 1;
bool includeWhiteSpaceInOperatorName: 1; /// "operator =()" vs "operator=()" bool includeWhiteSpaceInOperatorName: 1; /// "operator =()" vs "operator=()"
bool trailingReturnType: 1;
int markedArgument; int markedArgument;
int markedArgumentBegin; int markedArgumentBegin;

View File

@@ -435,11 +435,17 @@ void TypePrettyPrinter::visit(Function *type)
retAndArgOverview.showTemplateParameters = true; retAndArgOverview.showTemplateParameters = true;
if (_overview->showReturnTypes) { if (_overview->showReturnTypes) {
const QString returnType = retAndArgOverview.prettyType(type->returnType()); if (_overview->trailingReturnType) {
if (!returnType.isEmpty()) { _text.prepend("auto ");
if (!endsWithPtrOrRef(returnType) || !(_overview->starBindFlags & Overview::BindToIdentifier)) } else {
_text.prepend(QLatin1Char(' ')); const QString returnType = retAndArgOverview.prettyType(type->returnType());
_text.prepend(returnType); if (!returnType.isEmpty()) {
if (!endsWithPtrOrRef(returnType)
|| !(_overview->starBindFlags & Overview::BindToIdentifier)) {
_text.prepend(QLatin1Char(' '));
}
_text.prepend(returnType);
}
} }
} }
@@ -529,6 +535,12 @@ void TypePrettyPrinter::visit(Function *type)
_text += QLatin1String(spec->chars()); _text += QLatin1String(spec->chars());
} }
} }
if (_overview->showReturnTypes && _overview->trailingReturnType) {
const QString returnType = retAndArgOverview.prettyType(type->returnType());
if (!returnType.isEmpty())
_text.append(" -> ").append(returnType);
}
} }
void TypePrettyPrinter::appendSpace() void TypePrettyPrinter::appendSpace()

View File

@@ -6137,7 +6137,7 @@ struct Derived : public Base {
original = "#include \"file.h\"\n"; original = "#include \"file.h\"\n";
expected = R"DELIM(#include "file.h" expected = R"DELIM(#include "file.h"
void Derived::func() const &&noexcept {} auto Derived::func() const &&noexcept -> void {}
)DELIM"; )DELIM";
testDocuments << CppTestDocument::create("file.cpp", original, expected); testDocuments << CppTestDocument::create("file.cpp", original, expected);

View File

@@ -6234,6 +6234,13 @@ QString definitionSignature(const CppQuickFixInterface *assist,
oo.showArgumentNames = true; oo.showArgumentNames = true;
oo.showEnclosingTemplate = true; oo.showEnclosingTemplate = true;
oo.showTemplateParameters = true; oo.showTemplateParameters = true;
oo.trailingReturnType = functionDefinitionAST->declarator
&& functionDefinitionAST->declarator->postfix_declarator_list
&& functionDefinitionAST->declarator->postfix_declarator_list->value
&& functionDefinitionAST->declarator->postfix_declarator_list
->value->asFunctionDeclarator()
&& functionDefinitionAST->declarator->postfix_declarator_list
->value->asFunctionDeclarator()->trailing_return_type;
const Name *name = func->name(); const Name *name = func->name();
if (name && nameIncludesOperatorName(name)) { if (name && nameIncludesOperatorName(name)) {
CoreDeclaratorAST *coreDeclarator = functionDefinitionAST->declarator->core_declarator; CoreDeclaratorAST *coreDeclarator = functionDefinitionAST->declarator->core_declarator;