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()); ow.writeRegion(IdentifierRegion, name());
if (!onTarget.isEmpty()) if (!onTarget.isEmpty())
ow.space().writeRegion(OnTokenRegion).space().writeRegion(OnTargetRegion, onTarget); ow.space().writeRegion(OnTokenRegion).space().writeRegion(OnTargetRegion, onTarget);
ow.writeRegion(LeftBraceRegion, u" {").newline(); ow.writeRegion(LeftBraceRegion, u" {");
int baseIndent = ow.increaseIndent(); int baseIndent = ow.increaseIndent();
int spacerId = 0; int spacerId = 0;
if (!idStr().isEmpty()) { // *always* put id first 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) { == LineWriterOptions::AttributesSequence::Normalize) {
ow.ensureNewline(2); ow.ensureNewline(2);
} }
if (myId) if (myId) {
myId.writeOutPost(ow); myId.writeOutPost(ow);
ow.ensureNewline(1);
}
} }
quint32 counter = ow.counter(); quint32 counter = ow.counter();
DomItem component; DomItem component;
@ -860,9 +862,10 @@ void QmlObject::writeOut(const DomItem &self, OutWriter &ow, const QString &onTa
} else { } else {
el.second.writeOut(ow); el.second.writeOut(ow);
} }
ow.ensureNewline();
} }
ow.decreaseIndent(1, baseIndent); ow.decreaseIndent(1, baseIndent);
ow.ensureNewline().write(u"}"); ow.write(u"}");
return; return;
} }

View File

@ -10,17 +10,14 @@ import QtCharts 2.0
@Pippo { @Pippo {
atg1: 3 atg1: 3
} }
@Annotation2 { @Annotation2 {}
}
Item { Item {
@Annotate { @Annotate {}
}
anchors.fill: parent anchors.fill: parent
@AnnotateMore { @AnnotateMore {
property int x: 5 property int x: 5
} }
@AnnotateALot { @AnnotateALot {}
}
property variant othersSlice: 0 property variant othersSlice: 0
//![1] //![1]
@ -79,14 +76,12 @@ Item {
@BindingAnn { @BindingAnn {
bType: 2 bType: 2
} }
val2: Item { val2: Item {}
}
@BindingAnn { @BindingAnn {
bType: 3 bType: 3
} }
val3: [ val3: [
Item { Item {}
}
] ]
@BindingAnn { @BindingAnn {
bType: 4 bType: 4

View File

@ -135,11 +135,9 @@ Item {
// This is an orphan // This is an orphan
// This is a cool text // This is a cool text
Text { Text {},
},
// This is a cool rectangle // This is a cool rectangle
Rectangle { Rectangle {}
}
] ]
// some_read_only_bool // some_read_only_bool

View File

@ -135,11 +135,9 @@ Item {
// This is an orphan // This is an orphan
// This is a cool text // This is a cool text
Text { Text {},
},
// This is a cool rectangle // This is a cool rectangle
Rectangle { Rectangle {}
}
] ]
// some_read_only_bool // some_read_only_bool

View File

@ -135,11 +135,9 @@ Item {
// This is an orphan // This is an orphan
// This is a cool text // This is a cool text
Text { Text {},
},
// This is a cool rectangle // This is a cool rectangle
Rectangle { Rectangle {}
}
] ]
// some_read_only_bool // some_read_only_bool

View File

@ -142,11 +142,9 @@ Item {
// This is an orphan // This is an orphan
// This is a cool text // This is a cool text
Text { Text {},
},
// This is a cool rectangle // This is a cool rectangle
Rectangle { Rectangle {}
}
] ]
// This comment is related to the property animation // This comment is related to the property animation
PropertyAnimation on x { PropertyAnimation on x {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +1,9 @@
Item { Item {
Item { Item {}
}
/* This is a multiline comment. /* This is a multiline comment.
it should stay attached to Commented instead of getting orphaned. it should stay attached to Commented instead of getting orphaned.
*/ */
// This should also stick to Commented // This should also stick to Commented
Commented { Commented {}
}
} }

View File

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

View File

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

View File

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

View File

@ -2,15 +2,12 @@ QtObject {
id: foo id: foo
states: [ states: [
State { State {}
}
] ]
transitions: [ transitions: [
Transition { Transition {}
}
] ]
// This needs to be *before* states and transitions after formatting // This needs to be *before* states and transitions after formatting
Item { Item {}
}
} }

View File

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

View File

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