From 9ac953aefdec4481d53cea61b494d6657a61d03e Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Wed, 24 Jun 2020 13:05:55 +0200 Subject: qmlformat: Fix inline components Fixes: QTBUG-85189 Change-Id: I2b30595b44d14b89406b126d3d148f51f8bfbca4 Reviewed-by: Ulf Hermann (cherry picked from commit 88b3d1fd26b6b566ef00ae728051bff29455eea5) --- .../qmlformat/data/inlineComponents.formatted.qml | 10 ++++++ tests/auto/qml/qmlformat/data/inlineComponents.qml | 6 ++++ tests/auto/qml/qmlformat/tst_qmlformat.cpp | 7 ++++ tools/qmlformat/dumpastvisitor.cpp | 41 ++++++++++++++++++++++ tools/qmlformat/dumpastvisitor.h | 3 ++ 5 files changed, 67 insertions(+) create mode 100644 tests/auto/qml/qmlformat/data/inlineComponents.formatted.qml create mode 100644 tests/auto/qml/qmlformat/data/inlineComponents.qml diff --git a/tests/auto/qml/qmlformat/data/inlineComponents.formatted.qml b/tests/auto/qml/qmlformat/data/inlineComponents.formatted.qml new file mode 100644 index 0000000000..5349280f2b --- /dev/null +++ b/tests/auto/qml/qmlformat/data/inlineComponents.formatted.qml @@ -0,0 +1,10 @@ +Item { + component CustomText: Text { + Text { + color: "red" + text: "Test Text" + } + + } + +} diff --git a/tests/auto/qml/qmlformat/data/inlineComponents.qml b/tests/auto/qml/qmlformat/data/inlineComponents.qml new file mode 100644 index 0000000000..6eeb3dd965 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/inlineComponents.qml @@ -0,0 +1,6 @@ +Item { +component CustomText: Text { +color: "red" +text: "Test Text" +} +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 0e0bc53857..7690f78e9d 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -53,6 +53,7 @@ private Q_SLOTS: void testReadOnlyProps(); void testVerbatimStrings(); void testLargeBindings(); + void testInlineComponents(); #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled void testExample(); @@ -225,6 +226,12 @@ void TestQmlformat::testVerbatimStrings() readTestFile("verbatimString.formatted.qml")); } +void TestQmlformat::testInlineComponents() +{ + QCOMPARE(runQmlformat(testFile("inlineComponents.qml"), false, true), + readTestFile("inlineComponents.formatted.qml")); +} + void TestQmlformat::testLargeBindings() { QCOMPARE(runQmlformat(testFile("largeBindings.qml"), false, true), diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index ef6e234cc3..3ad66bc6a1 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -1024,6 +1024,47 @@ QHash findBindings(UiObjectMemberList *list) { return bindings; } +bool DumpAstVisitor::visit(UiInlineComponent *node) +{ + if (scope().m_firstObject) { + if (scope().m_firstOfAll) + scope().m_firstOfAll = false; + else + addNewLine(); + + scope().m_firstObject = false; + } + + addLine(getComment(node, Comment::Location::Front)); + addLine(getComment(node, Comment::Location::Front_Inline)); + addLine("component " + node->name + ": " + + parseUiQualifiedId(node->component->qualifiedTypeNameId) + " {"); + + m_indentLevel++; + + ScopeProperties props; + props.m_bindings = findBindings(node->component->initializer->members); + m_scope_properties.push(props); + + m_result += getOrphanedComments(node); + + return true; +} + +void DumpAstVisitor::endVisit(UiInlineComponent *node) +{ + m_indentLevel--; + + m_scope_properties.pop(); + + bool need_comma = scope().m_inArrayBinding && scope().m_lastInArrayBinding != node; + + addLine(need_comma ? "}," : "}"); + addLine(getComment(node, Comment::Location::Back)); + if (!scope().m_inArrayBinding) + addNewLine(); +} + bool DumpAstVisitor::visit(UiObjectDefinition *node) { if (scope().m_firstObject) { if (scope().m_firstOfAll) diff --git a/tools/qmlformat/dumpastvisitor.h b/tools/qmlformat/dumpastvisitor.h index 01b7e418e2..9b908dd101 100644 --- a/tools/qmlformat/dumpastvisitor.h +++ b/tools/qmlformat/dumpastvisitor.h @@ -59,6 +59,9 @@ public: bool visit(FunctionDeclaration *node) override; + bool visit(UiInlineComponent *node) override; + void endVisit(UiInlineComponent *node) override; + bool visit(UiObjectDefinition *node) override; void endVisit(UiObjectDefinition *node) override; -- cgit v1.2.3