aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/qmldom/qqmldomreformatter.cpp24
-rw-r--r--src/qmldom/qqmldomreformatter_p.h2
-rw-r--r--tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml2
-rw-r--r--tests/auto/qml/qmlformat/data/escapeChars.formatted.qml1
-rw-r--r--tests/auto/qml/qmlformat/data/forOf.formatted.qml1
-rw-r--r--tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml2
-rw-r--r--tests/auto/qml/qmlformat/data/nestedIf.formatted.qml1
-rw-r--r--tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml6
-rw-r--r--tests/auto/qml/qmlformat/data/twoFunctions.formatted.js1
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp1
-rw-r--r--tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml1
-rw-r--r--tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml1
-rw-r--r--tests/auto/qmldom/reformatter/tst_reformatter.h59
13 files changed, 99 insertions, 3 deletions
diff --git a/src/qmldom/qqmldomreformatter.cpp b/src/qmldom/qqmldomreformatter.cpp
index b51498d567..5001154027 100644
--- a/src/qmldom/qqmldomreformatter.cpp
+++ b/src/qmldom/qqmldomreformatter.cpp
@@ -11,6 +11,7 @@
#include <QString>
+#include <algorithm>
#include <limits>
QT_BEGIN_NAMESPACE
@@ -826,8 +827,27 @@ bool ScriptFormatter::visit(StatementList *ast)
}
accept(it->statement);
- if (it->next)
- newLine();
+ if (it->next) {
+ // There might be a post-comment attached to the current
+ // statement or a pre-comment attached to the next
+ // statmente or both.
+ // If any of those are present they will take care of
+ // handling the spacing between the statements so we
+ // don't need to push any newline.
+ auto *commentForCurrentStatement = comments->commentForNode(it->statement);
+ auto *commentForNextStatement = comments->commentForNode(it->next->statement);
+
+ if (
+ (commentForCurrentStatement && !commentForCurrentStatement->postComments().empty())
+ || (commentForNextStatement && !commentForNextStatement->preComments().empty())
+ ) continue;
+
+ quint32 lineDelta = it->next->firstSourceLocation().startLine
+ - it->statement->lastSourceLocation().startLine;
+ lineDelta = std::clamp(lineDelta, quint32{ 1 }, quint32{ 2 });
+
+ newLine(lineDelta);
+ }
}
--expressionDepth;
return false;
diff --git a/src/qmldom/qqmldomreformatter_p.h b/src/qmldom/qqmldomreformatter_p.h
index f5e3b147b9..565d021822 100644
--- a/src/qmldom/qqmldomreformatter_p.h
+++ b/src/qmldom/qqmldomreformatter_p.h
@@ -46,7 +46,7 @@ protected:
if (loc.length != 0)
out(loc2Str(loc));
}
- inline void newLine() { lw.ensureNewline(); }
+ inline void newLine(quint32 count = 1) { lw.ensureNewline(count); }
inline void accept(AST::Node *node) { AST::Node::accept(node, this); }
void lnAcceptIndented(AST::Node *node);
diff --git a/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml
index edbb12c6e6..de6a23f9c7 100644
--- a/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml
@@ -4,11 +4,13 @@ Item {
function f() {
var count = 0;
+
class Person {
constructor(name){
this._name = name;
}
}
+
class Employee extends Person {
constructor(name, age){
super(name);
diff --git a/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml b/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml
index 3cb99b704a..3f6807834f 100644
--- a/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml
@@ -6,6 +6,7 @@ Item {
let a = {
"\"": "\\"
};
+
let patron = {
"\\\"\n\n": "\?\?\\\"",
"": "",
diff --git a/tests/auto/qml/qmlformat/data/forOf.formatted.qml b/tests/auto/qml/qmlformat/data/forOf.formatted.qml
index fa9ff9c631..0cc4f9fecd 100644
--- a/tests/auto/qml/qmlformat/data/forOf.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/forOf.formatted.qml
@@ -3,6 +3,7 @@ import QtQml 2.0
QtObject {
Component.onCompleted: {
var list = [[1, 2], [3, 4], [5, 6]];
+
for (const [x, y] of list)
console.log("X: " + x + "; Y: " + y);
for (let [x, y] of list)
diff --git a/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml b/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml
index 5536ecf513..fc1915f647 100644
--- a/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml
@@ -1,11 +1,13 @@
Item {
function a() {
function nested() {}
+
foo();
}
function b() {
function nested() {}
+
bar();
}
}
diff --git a/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml b/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml
index 4ff5a40a23..ebb125f36d 100644
--- a/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml
@@ -26,6 +26,7 @@ Item {
x();
}
}
+
if (x && y)
if (x < y)
return 0;
diff --git a/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml b/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml
index 3feac73688..94e97076b1 100644
--- a/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml
@@ -14,6 +14,7 @@ QtObject {
push
}] = array;
const [a4, b4, ...[c, d]] = array;
+
const obj = {
_a: 1,
_b: 2
@@ -52,6 +53,7 @@ QtObject {
push
}] = array;
[a, b, ...[c, d]] = array;
+
const obj = {
_a: 1,
_b: 2
@@ -64,6 +66,7 @@ QtObject {
a: a1,
b: b1
} = obj);
+
const complicatedObject = {
a: 1,
b: {
@@ -75,6 +78,7 @@ QtObject {
},
g: [7, 8, 9]
};
+
const {
patron,
b: {
@@ -104,8 +108,10 @@ QtObject {
}
}
};
+
myLambda(myObject);
};
+
myFunction(({
a,
b: {
diff --git a/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js b/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js
index 5368f74172..b7414de053 100644
--- a/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js
+++ b/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js
@@ -4,6 +4,7 @@ function one() {
a = 5;
}
}
+
function two(a, b) {
for (; b < 5; ++b) {
a = a * b;
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index f1a7ffbc4a..0bf19bbe9f 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -390,6 +390,7 @@ void TestQmlformat::testFormat_data()
QTest::newRow("javascriptBlock")
<< "javascriptBlock.qml"
<< "javascriptBlock.formatted.qml" << QStringList{} << RunOption::OnCopy;
+
//plainJS
QTest::newRow("nestedLambdaWithIfElse")
<< "lambdaWithIfElseInsideLambda.js"
diff --git a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml
index fe3403cef1..ab722f94be 100644
--- a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml
+++ b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml
@@ -29,6 +29,7 @@ Item {
if (y == 6) // if comment
console.log("pippo");
+
a + b; // comment
// footer
diff --git a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml
index 9a5aba106e..c6e33f9389 100644
--- a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml
+++ b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml
@@ -29,6 +29,7 @@ Item {
if (y == 6) // if comment
console.log("pippo");
+
a + b; // comment
// footer
diff --git a/tests/auto/qmldom/reformatter/tst_reformatter.h b/tests/auto/qmldom/reformatter/tst_reformatter.h
index 630cef03ed..31d80097c1 100644
--- a/tests/auto/qmldom/reformatter/tst_reformatter.h
+++ b/tests/auto/qmldom/reformatter/tst_reformatter.h
@@ -705,6 +705,65 @@ private slots:
QCOMPARE(formattedMethod, expectedFormattedMethod);
}
+ void statementList_data()
+ {
+ QTest::addColumn<QString>("codeToBeFormatted");
+ QTest::addColumn<QString>("expectedFormattedCode");
+
+ QTest::newRow("StatementsOnTheSameLine")
+ << QStringLiteral(u"a=1;b=1;") << QStringLiteral(u"a = 1;\nb = 1;");
+
+ QTest::newRow("StatementsOnSuccessiveLines")
+ << QStringLiteral(u"a=1;\nb=1;") << QStringLiteral(u"a = 1;\nb = 1;");
+
+ QTest::newRow("EmptyLineBetweenStatements")
+ << QStringLiteral(u"a=1;\n\nb=1;") << QStringLiteral(u"a = 1;\n\nb = 1;");
+
+ QTest::newRow("MultipleEmptyLinesBetweenStatements")
+ << QStringLiteral(u"a=1;\n\n\n\n\n\nb=1;") << QStringLiteral(u"a = 1;\n\nb = 1;");
+
+ QTest::newRow("MultilineStatementWithStatementOnTheFollowingLine")
+ << QStringLiteral(u"console.log(\n\n);\nb = 1;")
+ << QStringLiteral(u"console.log();\nb = 1;");
+
+ QTest::newRow("StatementWithPostCommentAndStatementOnTheFollowingLine")
+ << QStringLiteral(u"a=1;//\nb=1;") << QStringLiteral(u"a = 1;//\nb = 1;");
+
+ QTest::newRow("StatementWithPostCommentAndEmptyLineToNextStatement")
+ << QStringLiteral(u"a=1;//\n\nb=1;") << QStringLiteral(u"a = 1;//\n\nb = 1;");
+
+ QTest::newRow("StatementWithPostCommentAndMultipleEmptyLinesToNextStatement")
+ << QStringLiteral(u"a=1;//\n\n\n\n\nb=1;") << QStringLiteral(u"a = 1;//\n\nb = 1;");
+
+ QTest::newRow("StatementsWithCommentInBetweenThem")
+ << QStringLiteral(u"a=1;\n//\nb=1;") << QStringLiteral(u"a = 1;\n//\nb = 1;");
+
+ QTest::newRow("StatementsWithCommentAndSingleEmptyLineInBetweenThem")
+ << QStringLiteral(u"a=1;\n\n//\n\nb=1;")
+ << QStringLiteral(u"a = 1;\n\n//\n\nb = 1;");
+
+ QTest::newRow("StatementsWithCommentAndMultipleEmptyLinesInBetweenThem")
+ << QStringLiteral(u"a=1;\n\n\n\n//\n\n\nb=1;")
+ << QStringLiteral(u"a = 1;\n\n//\n\nb = 1;");
+
+ QTest::newRow("StatementWithSingleEmptyLineAndPreCommentOnNextStatement")
+ << QStringLiteral(u"a=1;\n\n//\nb=1;") << QStringLiteral(u"a = 1;\n\n//\nb = 1;");
+
+ QTest::newRow("StatementWithMultipleEmptyLinesAndPreCommentOnNextStatement")
+ << QStringLiteral(u"a=1;\n\n\n\n\n\n\n\n//\nb=1;")
+ << QStringLiteral(u"a = 1;\n\n//\nb = 1;");
+ }
+
+ void statementList()
+ {
+ QFETCH(QString, codeToBeFormatted);
+ QFETCH(QString, expectedFormattedCode);
+
+ QString formattedCode = formatJSCode(codeToBeFormatted);
+
+ QCOMPARE(formattedCode, expectedFormattedCode);
+ }
+
private:
};