aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Goins <joshua.goins@kdab.com>2023-09-13 16:32:46 -0400
committerJoshua Goins <joshua.goins@kdab.com>2024-01-17 08:44:53 -0500
commitdd6346e0f8ba8a1b6672ec3f398cbac013365857 (patch)
tree9f2a335aa700c5ca9f6ce13e250cf0dc1f52a726
parentdc1a16345214db9665d305934c39ebd7eca2a62d (diff)
qmlformat: Fix object declaration indentation in arrays
Object declarations inside of array patterns did not insert newlines correctly. This fixes those while not touching other types of array initializations (such as numeric literals) and the one of test data. When extra commas are added to the end of arrays, they are now kept on the correct line as well. Pick-to: 6.5 Change-Id: I8fe67ef066b84f56237449c7695990daa915b4b6 Reviewed-by: Sami Shalayel <sami.shalayel@qt.io> (cherry picked from commit 295a1ce389cda8f360835c89d9a3b32cbcba7eb6) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 2b1bcfd3b6b2bdcd387e3dc645f35afa9672404b)
-rw-r--r--src/qmldom/qqmldomreformatter.cpp21
-rw-r--r--tests/auto/qml/qmlformat/data/arrayEndComma.formatted.qml4
-rw-r--r--tests/auto/qml/qmlformat/data/arrayEndComma.qml8
-rw-r--r--tests/auto/qml/qmlformat/data/dontRemoveComments.formatted.qml6
-rw-r--r--tests/auto/qml/qmlformat/data/dontRemoveComments.qml8
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp6
6 files changed, 42 insertions, 11 deletions
diff --git a/src/qmldom/qqmldomreformatter.cpp b/src/qmldom/qqmldomreformatter.cpp
index 9249eb4a7e..98ce433b84 100644
--- a/src/qmldom/qqmldomreformatter.cpp
+++ b/src/qmldom/qqmldomreformatter.cpp
@@ -267,9 +267,16 @@ protected:
{
out(ast->lbracketToken);
int baseIndent = lw.increaseIndent(1);
- if (ast->elements)
+ if (ast->elements) {
accept(ast->elements);
- out(ast->commaToken);
+ out(ast->commaToken);
+ auto lastElement = lastListElement(ast->elements);
+ if (lastElement->element && cast<ObjectPattern *>(lastElement->element->initializer)) {
+ newLine();
+ }
+ } else {
+ out(ast->commaToken);
+ }
lw.decreaseIndent(1, baseIndent);
out(ast->rbracketToken);
return false;
@@ -291,14 +298,22 @@ protected:
bool visit(PatternElementList *ast) override
{
for (PatternElementList *it = ast; it; it = it->next) {
+ const bool isObjectInitializer =
+ it->element && cast<ObjectPattern *>(it->element->initializer);
+ if (isObjectInitializer)
+ newLine();
+
if (it->elision)
accept(it->elision);
if (it->elision && it->element)
out(", ");
if (it->element)
accept(it->element);
- if (it->next)
+ if (it->next) {
out(", ");
+ if (isObjectInitializer)
+ newLine();
+ }
}
return false;
}
diff --git a/tests/auto/qml/qmlformat/data/arrayEndComma.formatted.qml b/tests/auto/qml/qmlformat/data/arrayEndComma.formatted.qml
new file mode 100644
index 0000000000..8ae4dd7c88
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/arrayEndComma.formatted.qml
@@ -0,0 +1,4 @@
+Item {
+ // should keep its comma
+ property var some_array_literal: [30, 20, 0.3,]
+}
diff --git a/tests/auto/qml/qmlformat/data/arrayEndComma.qml b/tests/auto/qml/qmlformat/data/arrayEndComma.qml
new file mode 100644
index 0000000000..1aec09515c
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/arrayEndComma.qml
@@ -0,0 +1,8 @@
+Item {
+ // should keep its comma
+ property var some_array_literal: [
+ 30,
+ 20,
+ 0.3,
+ ]
+}
diff --git a/tests/auto/qml/qmlformat/data/dontRemoveComments.formatted.qml b/tests/auto/qml/qmlformat/data/dontRemoveComments.formatted.qml
index c847e1e04f..8eaac71178 100644
--- a/tests/auto/qml/qmlformat/data/dontRemoveComments.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/dontRemoveComments.formatted.qml
@@ -1,8 +1,10 @@
Item {
- property var test: [{
+ property var test: [
+ {
// Testing
"foo": "bar"
- }]
+ }
+ ]
onTestChanged: {
fooBar(test, {
diff --git a/tests/auto/qml/qmlformat/data/dontRemoveComments.qml b/tests/auto/qml/qmlformat/data/dontRemoveComments.qml
index 1797834879..2d2b4b6705 100644
--- a/tests/auto/qml/qmlformat/data/dontRemoveComments.qml
+++ b/tests/auto/qml/qmlformat/data/dontRemoveComments.qml
@@ -1,8 +1,10 @@
Item {
- property var test: [{
-// Testing
+ property var test: [
+ {
+ // Testing
"foo": "bar"
- }]
+ }
+ ]
onTestChanged: {
fooBar(test, {
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index cc0b73ab15..eff3d518f2 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -343,9 +343,9 @@ void TestQmlformat::testFormat_data()
QTest::newRow("ellipsisFunctionArgument")
<< "ellipsisFunctionArgument.qml"
<< "ellipsisFunctionArgument.formatted.qml" << QStringList{} << RunOption::OnCopy;
- QTest::newRow("escapeChars")
- << "escapeChars.qml"
- << "escapeChars.formatted.qml" << QStringList{} << RunOption::OnCopy;
+ QTest::newRow("arrayEndComma")
+ << "arrayEndComma.qml"
+ << "arrayEndComma.formatted.qml" << QStringList{} << RunOption::OnCopy;
}
void TestQmlformat::testFormat()