From 1b438b0395ba042604f6221712b5de20423130a1 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 10 Sep 2023 14:17:44 -0400 Subject: [PATCH] qmlformat: Don't add unnecessary newlines in empty objects Empty component and object declarations are now collapsed, instead of putting the ending right brace on a new line. This makes code that uses these more readable and reduces the amount of lines used. The test data is updated to reflect this new behavior. Fixes: QTBUG-108660 Pick-to: 6.5 6.6 6.7 Change-Id: Ifecfa2a37d5f787a89718ddcdefba17f305b181c Reviewed-by: Qt CI Bot Reviewed-by: Semih Yavuz --- src/qmldom/qqmldomelements.cpp | 9 ++++++--- .../qml/qmlformat/data/Annotations.formatted.qml | 15 +++++---------- .../qmlformat/data/Example1.formatted.2spaces.qml | 6 ++---- .../qml/qmlformat/data/Example1.formatted.qml | 6 ++---- .../qmlformat/data/Example1.formatted.tabs.qml | 6 ++---- .../qml/qmlformat/data/Example1.formatted2.qml | 6 ++---- .../qml/qmlformat/data/FrontInline.formatted.qml | 3 +-- .../data/blanklinesAfterComment.formatted.qml | 3 +-- .../data/commentInQmlObject.formatted.qml | 3 +-- .../qmlformat/data/functionsSpacing.formatted.qml | 3 +-- .../qmlformat/data/importStatements.formatted.qml | 3 +-- .../qmlformat/data/multilineComment.formatted.qml | 6 ++---- .../qmlformat/data/objectsSpacing.formatted.qml | 3 +++ tests/auto/qml/qmlformat/data/objectsSpacing.qml | 4 ++++ .../auto/qml/qmlformat/data/pragma.formatted.qml | 3 +-- .../data/statesAndTransitions.formatted.qml | 9 +++------ .../domdata/reformatter/file1Reformatted2.qml | 3 +-- .../data/formatting/blanklines.formatted.qml | 3 +-- 18 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/qmldom/qqmldomelements.cpp b/src/qmldom/qqmldomelements.cpp index d13155cf59..d0d5e47447 100644 --- a/src/qmldom/qqmldomelements.cpp +++ b/src/qmldom/qqmldomelements.cpp @@ -719,7 +719,7 @@ void QmlObject::writeOut(const DomItem &self, OutWriter &ow, const QString &onTa ow.writeRegion(IdentifierRegion, name()); if (!onTarget.isEmpty()) ow.space().writeRegion(OnTokenRegion).space().writeRegion(OnTargetRegion, onTarget); - ow.writeRegion(LeftBraceRegion, u" {").newline(); + ow.writeRegion(LeftBraceRegion, u" {"); int baseIndent = ow.increaseIndent(); int spacerId = 0; if (!idStr().isEmpty()) { // *always* put id first @@ -735,8 +735,10 @@ void QmlObject::writeOut(const DomItem &self, OutWriter &ow, const QString &onTa == LineWriterOptions::AttributesSequence::Normalize) { ow.ensureNewline(2); } - if (myId) + if (myId) { myId.writeOutPost(ow); + ow.ensureNewline(1); + } } quint32 counter = ow.counter(); DomItem component; @@ -860,9 +862,10 @@ void QmlObject::writeOut(const DomItem &self, OutWriter &ow, const QString &onTa } else { el.second.writeOut(ow); } + ow.ensureNewline(); } ow.decreaseIndent(1, baseIndent); - ow.ensureNewline().write(u"}"); + ow.write(u"}"); return; } diff --git a/tests/auto/qml/qmlformat/data/Annotations.formatted.qml b/tests/auto/qml/qmlformat/data/Annotations.formatted.qml index 1898585e31..019b7f0141 100644 --- a/tests/auto/qml/qmlformat/data/Annotations.formatted.qml +++ b/tests/auto/qml/qmlformat/data/Annotations.formatted.qml @@ -10,17 +10,14 @@ import QtCharts 2.0 @Pippo { atg1: 3 } -@Annotation2 { -} +@Annotation2 {} Item { - @Annotate { - } + @Annotate {} anchors.fill: parent @AnnotateMore { property int x: 5 } - @AnnotateALot { - } + @AnnotateALot {} property variant othersSlice: 0 //![1] @@ -79,14 +76,12 @@ Item { @BindingAnn { bType: 2 } - val2: Item { - } + val2: Item {} @BindingAnn { bType: 3 } val3: [ - Item { - } + Item {} ] @BindingAnn { bType: 4 diff --git a/tests/auto/qml/qmlformat/data/Example1.formatted.2spaces.qml b/tests/auto/qml/qmlformat/data/Example1.formatted.2spaces.qml index ca870e948a..b1662e8898 100644 --- a/tests/auto/qml/qmlformat/data/Example1.formatted.2spaces.qml +++ b/tests/auto/qml/qmlformat/data/Example1.formatted.2spaces.qml @@ -135,11 +135,9 @@ Item { // This is an orphan // This is a cool text - Text { - }, + Text {}, // This is a cool rectangle - Rectangle { - } + Rectangle {} ] // some_read_only_bool diff --git a/tests/auto/qml/qmlformat/data/Example1.formatted.qml b/tests/auto/qml/qmlformat/data/Example1.formatted.qml index b77ebf1213..c4aef78924 100644 --- a/tests/auto/qml/qmlformat/data/Example1.formatted.qml +++ b/tests/auto/qml/qmlformat/data/Example1.formatted.qml @@ -135,11 +135,9 @@ Item { // This is an orphan // This is a cool text - Text { - }, + Text {}, // This is a cool rectangle - Rectangle { - } + Rectangle {} ] // some_read_only_bool diff --git a/tests/auto/qml/qmlformat/data/Example1.formatted.tabs.qml b/tests/auto/qml/qmlformat/data/Example1.formatted.tabs.qml index 966fba78f1..7cea50213a 100644 --- a/tests/auto/qml/qmlformat/data/Example1.formatted.tabs.qml +++ b/tests/auto/qml/qmlformat/data/Example1.formatted.tabs.qml @@ -135,11 +135,9 @@ Item { // This is an orphan // This is a cool text - Text { - }, + Text {}, // This is a cool rectangle - Rectangle { - } + Rectangle {} ] // some_read_only_bool diff --git a/tests/auto/qml/qmlformat/data/Example1.formatted2.qml b/tests/auto/qml/qmlformat/data/Example1.formatted2.qml index 916e19a46b..7049686900 100644 --- a/tests/auto/qml/qmlformat/data/Example1.formatted2.qml +++ b/tests/auto/qml/qmlformat/data/Example1.formatted2.qml @@ -142,11 +142,9 @@ Item { // This is an orphan // This is a cool text - Text { - }, + Text {}, // This is a cool rectangle - Rectangle { - } + Rectangle {} ] // This comment is related to the property animation PropertyAnimation on x { diff --git a/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml b/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml index 7dfe435ac0..95b4fcf4a3 100644 --- a/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml +++ b/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml @@ -1,4 +1,3 @@ // This comment should be directly above Item after formatting -Item { -} +Item {} diff --git a/tests/auto/qml/qmlformat/data/blanklinesAfterComment.formatted.qml b/tests/auto/qml/qmlformat/data/blanklinesAfterComment.formatted.qml index a7e70d70ea..071e3bc69f 100644 --- a/tests/auto/qml/qmlformat/data/blanklinesAfterComment.formatted.qml +++ b/tests/auto/qml/qmlformat/data/blanklinesAfterComment.formatted.qml @@ -9,5 +9,4 @@ import QtQml -QtObject { -} +QtObject {} diff --git a/tests/auto/qml/qmlformat/data/commentInQmlObject.formatted.qml b/tests/auto/qml/qmlformat/data/commentInQmlObject.formatted.qml index 2a14aed721..749dc65b7c 100644 --- a/tests/auto/qml/qmlformat/data/commentInQmlObject.formatted.qml +++ b/tests/auto/qml/qmlformat/data/commentInQmlObject.formatted.qml @@ -1,5 +1,4 @@ import QtQml // hello world -QtObject { -} +QtObject {} diff --git a/tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml b/tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml index d452ba2b8c..91f520b5fc 100644 --- a/tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml +++ b/tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml @@ -6,8 +6,7 @@ Item { } function test2() { } - Button { - } + Button {} function test4() { } diff --git a/tests/auto/qml/qmlformat/data/importStatements.formatted.qml b/tests/auto/qml/qmlformat/data/importStatements.formatted.qml index 1649b9b9ce..6613becaca 100644 --- a/tests/auto/qml/qmlformat/data/importStatements.formatted.qml +++ b/tests/auto/qml/qmlformat/data/importStatements.formatted.qml @@ -5,5 +5,4 @@ import QtQuick.Controls import org.test.module -QtObject { -} +QtObject {} diff --git a/tests/auto/qml/qmlformat/data/multilineComment.formatted.qml b/tests/auto/qml/qmlformat/data/multilineComment.formatted.qml index 45d04c5887..46e3e963ea 100644 --- a/tests/auto/qml/qmlformat/data/multilineComment.formatted.qml +++ b/tests/auto/qml/qmlformat/data/multilineComment.formatted.qml @@ -1,11 +1,9 @@ Item { - Item { - } + Item {} /* This is a multiline comment. it should stay attached to Commented instead of getting orphaned. */ // This should also stick to Commented - Commented { - } + Commented {} } diff --git a/tests/auto/qml/qmlformat/data/objectsSpacing.formatted.qml b/tests/auto/qml/qmlformat/data/objectsSpacing.formatted.qml index bd0406e595..df26a9b599 100644 --- a/tests/auto/qml/qmlformat/data/objectsSpacing.formatted.qml +++ b/tests/auto/qml/qmlformat/data/objectsSpacing.formatted.qml @@ -1,6 +1,9 @@ Item { + Button {} + Button { + id: foo } height: 360 diff --git a/tests/auto/qml/qmlformat/data/objectsSpacing.qml b/tests/auto/qml/qmlformat/data/objectsSpacing.qml index 6adc89778c..0239b05145 100644 --- a/tests/auto/qml/qmlformat/data/objectsSpacing.qml +++ b/tests/auto/qml/qmlformat/data/objectsSpacing.qml @@ -3,6 +3,10 @@ Item { Button { } + Button { + id: foo + } + height: 360 width: 360 diff --git a/tests/auto/qml/qmlformat/data/pragma.formatted.qml b/tests/auto/qml/qmlformat/data/pragma.formatted.qml index 0bcbfd555c..143db39888 100644 --- a/tests/auto/qml/qmlformat/data/pragma.formatted.qml +++ b/tests/auto/qml/qmlformat/data/pragma.formatted.qml @@ -5,5 +5,4 @@ pragma ValueTypeBehavior: Copy, Addressable import QtQml -QtObject { -} +QtObject {} diff --git a/tests/auto/qml/qmlformat/data/statesAndTransitions.formatted.qml b/tests/auto/qml/qmlformat/data/statesAndTransitions.formatted.qml index 40cf5068da..923f0642d7 100644 --- a/tests/auto/qml/qmlformat/data/statesAndTransitions.formatted.qml +++ b/tests/auto/qml/qmlformat/data/statesAndTransitions.formatted.qml @@ -2,15 +2,12 @@ QtObject { id: foo states: [ - State { - } + State {} ] transitions: [ - Transition { - } + Transition {} ] // This needs to be *before* states and transitions after formatting - Item { - } + Item {} } diff --git a/tests/auto/qmldom/domdata/reformatter/file1Reformatted2.qml b/tests/auto/qmldom/domdata/reformatter/file1Reformatted2.qml index 68827f4a1f..2e7483f453 100644 --- a/tests/auto/qmldom/domdata/reformatter/file1Reformatted2.qml +++ b/tests/auto/qmldom/domdata/reformatter/file1Reformatted2.qml @@ -14,8 +14,7 @@ Window { Rectangle { anchors.fill: parent - Behavior on opacity { - } + Behavior on opacity {} ListView { width: parent.width diff --git a/tests/auto/qmlls/modules/data/formatting/blanklines.formatted.qml b/tests/auto/qmlls/modules/data/formatting/blanklines.formatted.qml index b3c5154ee7..19548e9023 100644 --- a/tests/auto/qmlls/modules/data/formatting/blanklines.formatted.qml +++ b/tests/auto/qmlls/modules/data/formatting/blanklines.formatted.qml @@ -1,5 +1,4 @@ // leading and trailing spaces import QtQuick -Item { -} +Item {}