aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2020-06-18 14:36:37 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2020-07-03 10:44:45 +0200
commit8cf45b19d5c5c65053a1c41f6293df37947e685d (patch)
treebba7cc0a6088c90abc4eba1f47af5c807f755aba
parentf9925dba66a839614fdf1f3e735265f03258f526 (diff)
qmlformat: Fix nested functions
Fixes: QTBUG-85035 Change-Id: I5e1cb003b7b84547e3408a086eebf3be740e6860 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> (cherry picked from commit eb90e8ee3313bee547e6721a2649bf9ba84e3e5c) Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml1
-rw-r--r--tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml16
-rw-r--r--tests/auto/qml/qmlformat/data/nestedFunctions.qml14
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp8
-rw-r--r--tools/qmlformat/dumpastvisitor.cpp17
-rw-r--r--tools/qmlformat/dumpastvisitor.h2
6 files changed, 53 insertions, 5 deletions
diff --git a/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
index b26361d5bf..ddba82c312 100644
--- a/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
@@ -1,5 +1,4 @@
Item {
-
function test() {
//// The following if blocks should NOT HAVE braces
// Single branch, no braces
diff --git a/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml b/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml
new file mode 100644
index 0000000000..5a3489e98f
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml
@@ -0,0 +1,16 @@
+Item {
+ function a() {
+ function nested() {
+ }
+
+ foo();
+ }
+
+ function b() {
+ function nested() {
+ }
+
+ bar();
+ }
+
+}
diff --git a/tests/auto/qml/qmlformat/data/nestedFunctions.qml b/tests/auto/qml/qmlformat/data/nestedFunctions.qml
new file mode 100644
index 0000000000..592e3197ab
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/nestedFunctions.qml
@@ -0,0 +1,14 @@
+Item {
+function a() {
+ function nested() {}
+
+ foo();
+}
+
+function b() {
+ function nested() {}
+
+ bar();
+}
+
+} \ No newline at end of file
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index a1bcada5b4..a3f5cd1a2b 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -59,6 +59,8 @@ private Q_SLOTS:
void testNestedIf();
+ void testNestedFunctions();
+
#if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
void testExample();
void testExample_data();
@@ -271,6 +273,12 @@ void TestQmlformat::testQtbug85003()
readTestFile("QtBug85003.formatted.qml"));
}
+void TestQmlformat::testNestedFunctions()
+{
+ QCOMPARE(runQmlformat(testFile("nestedFunctions.qml"), false, true),
+ readTestFile("nestedFunctions.formatted.qml"));
+}
+
#if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
void TestQmlformat::testExample_data()
{
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp
index 06a6f6335f..3b23ddf128 100644
--- a/tools/qmlformat/dumpastvisitor.cpp
+++ b/tools/qmlformat/dumpastvisitor.cpp
@@ -1232,8 +1232,14 @@ void DumpAstVisitor::endVisit(UiArrayBinding *) {
}
bool DumpAstVisitor::visit(FunctionDeclaration *node) {
+ if (scope().m_firstFunction) {
+ if (scope().m_firstOfAll)
+ scope().m_firstOfAll = false;
+ else
+ addNewLine();
- addNewLine();
+ scope().m_firstFunction = false;
+ }
addLine(getComment(node, Comment::Location::Front));
@@ -1251,13 +1257,16 @@ bool DumpAstVisitor::visit(FunctionDeclaration *node) {
addLine(head);
m_indentLevel++;
+
+ return true;
+}
+
+void DumpAstVisitor::endVisit(FunctionDeclaration *node)
+{
m_result += parseStatementList(node->body);
m_indentLevel--;
addLine("}");
-
addNewLine();
-
- return true;
}
bool DumpAstVisitor::visit(UiObjectBinding *node) {
diff --git a/tools/qmlformat/dumpastvisitor.h b/tools/qmlformat/dumpastvisitor.h
index 9b908dd101..f84c4859ae 100644
--- a/tools/qmlformat/dumpastvisitor.h
+++ b/tools/qmlformat/dumpastvisitor.h
@@ -58,6 +58,7 @@ public:
void endVisit(UiObjectBinding *node) override;
bool visit(FunctionDeclaration *node) override;
+ void endVisit(FunctionDeclaration *node) override;
bool visit(UiInlineComponent *node) override;
void endVisit(UiInlineComponent *node) override;
@@ -86,6 +87,7 @@ private:
bool m_firstProperty = true;
bool m_firstBinding = true;
bool m_firstObject = true;
+ bool m_firstFunction = true;
bool m_inArrayBinding = false;
bool m_pendingBinding = false;