From 21ca06fc7cbb2be37c3c8face1d43cf2cbda3b4f Mon Sep 17 00:00:00 2001 From: Semih Yavuz Date: Thu, 20 Apr 2023 11:04:06 +0200 Subject: [PATCH] Codeformatter: Support indenting of type annotated function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: QTCREATORBUG-29046 Change-Id: Ie4a4d85b7aa00ddf4dd3ea4bade6ffa57af7b4e0 Reviewed-by: MikoĊ‚aj Boc Reviewed-by: Ulf Hermann Reviewed-by: Christian Kandeler Reviewed-by: Qt CI Bot Reviewed-by: Fabian Kosmale --- src/libs/qmljs/qmljscodeformatter.cpp | 7 +++++++ src/libs/qmljs/qmljscodeformatter.h | 1 + .../qmlcodeformatter/tst_qmlcodeformatter.cpp | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index 55ed2e6190c..a2f944700f9 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -253,9 +253,16 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case function_arglist_closed: switch (kind) { case LeftBrace: turnInto(jsblock_open); break; + case Colon: turnInto(function_type_annotated_return); break; default: leave(true); continue; // error recovery } break; + case function_type_annotated_return: + switch (kind) { + case LeftBrace: turnInto(jsblock_open); break; + default: break; + } break; + case expression_or_objectdefinition: switch (kind) { case Dot: diff --git a/src/libs/qmljs/qmljscodeformatter.h b/src/libs/qmljs/qmljscodeformatter.h index 4800fccfdf0..abef85a782d 100644 --- a/src/libs/qmljs/qmljscodeformatter.h +++ b/src/libs/qmljs/qmljscodeformatter.h @@ -99,6 +99,7 @@ public: // must be public to make Q_GADGET introspection work function_start, // after 'function' function_arglist_open, // after '(' starting function argument list function_arglist_closed, // after ')' in argument list, expecting '{' + function_type_annotated_return, // after ':' expecting a type binding_or_objectdefinition, // after an identifier diff --git a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp index 8d2194086b2..934bf5d317d 100644 --- a/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp +++ b/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter.cpp @@ -83,6 +83,7 @@ private Q_SLOTS: void bug1(); void bug2(); void bug3(); + void indentFunctionWithReturnTypeAnnotation(); }; enum { DontCheck = -2, DontIndent = -1 }; @@ -1564,6 +1565,18 @@ void tst_QMLCodeFormatter::bug3() checkIndent(data); } + +void tst_QMLCodeFormatter::indentFunctionWithReturnTypeAnnotation() +{ + QList data; + data << Line("function test() : void {", 0) + << Line("", 4) + << Line(" }", 0) + ; + checkIndent(data); +} + + QTEST_GUILESS_MAIN(tst_QMLCodeFormatter) #include "tst_qmlcodeformatter.moc"