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 <qt_ci_bot@qt-project.org>
Reviewed-by: Semih Yavuz <semih.yavuz@qt.io>
This commit is contained in:
Joshua Goins 2023-09-10 14:17:44 -04:00
parent b70afcb26a
commit 1b438b0395
18 changed files with 39 additions and 55 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -1,4 +1,3 @@
// This comment should be directly above Item after formatting
Item {
}
Item {}

View File

@ -9,5 +9,4 @@
import QtQml
QtObject {
}
QtObject {}

View File

@ -1,5 +1,4 @@
import QtQml
// hello world
QtObject {
}
QtObject {}

View File

@ -6,8 +6,7 @@ Item {
}
function test2() {
}
Button {
}
Button {}
function test4() {
}

View File

@ -5,5 +5,4 @@ import QtQuick.Controls
import org.test.module
QtObject {
}
QtObject {}

View File

@ -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 {}
}

View File

@ -1,6 +1,9 @@
Item {
Button {}
Button {
id: foo
}
height: 360

View File

@ -3,6 +3,10 @@ Item {
Button {
}
Button {
id: foo
}
height: 360
width: 360

View File

@ -5,5 +5,4 @@ pragma ValueTypeBehavior: Copy, Addressable
import QtQml
QtObject {
}
QtObject {}

View File

@ -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 {}
}

View File

@ -14,8 +14,7 @@ Window {
Rectangle {
anchors.fill: parent
Behavior on opacity {
}
Behavior on opacity {}
ListView {
width: parent.width

View File

@ -1,5 +1,4 @@
// leading and trailing spaces
import QtQuick
Item {
}
Item {}