diff options
author | Xavier BESSON <developer@xavi-b.fr> | 2022-11-23 19:33:40 +0100 |
---|---|---|
committer | Xavier BESSON <developer@xavi-b.fr> | 2022-11-24 11:30:03 +0100 |
commit | 0e4acad6ab3a4caf02a4b372152e646f0c11c68d (patch) | |
tree | 6f2bea1da55bca50e45c783ea4aaf0fd63bd2e23 | |
parent | f8006c960edf613967acf7f8b55661f9aab414fe (diff) |
qmlformat: add 'ensure new line between functions' option
Fixes: QTBUG-108659
Change-Id: I3e21727bd946089d89ce9c42f640f05230de8c8e
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/qmldom/qqmldomelements.cpp | 7 | ||||
-rw-r--r-- | src/qmldom/qqmldomlinewriter_p.h | 1 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml | 21 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/functionsSpacing.qml | 22 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.formatted.qml | 26 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.qml | 23 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat.cpp | 8 | ||||
-rw-r--r-- | tools/qmlformat/qmlformat.cpp | 11 |
8 files changed, 118 insertions, 1 deletions
diff --git a/src/qmldom/qqmldomelements.cpp b/src/qmldom/qqmldomelements.cpp index b0de6be295..3a5b2bb590 100644 --- a/src/qmldom/qqmldomelements.cpp +++ b/src/qmldom/qqmldomelements.cpp @@ -942,8 +942,13 @@ void QmlObject::writeOut(DomItem &self, OutWriter &ow, QString onTarget) const ow.removeTextAddCallback(spacerId); if (counter != ow.counter()) spacerId = ow.addNewlinesAutospacerCallback(2); + bool first = true; for (auto &method : methodList) { + if (!first && ow.lineWriter.options().functionsSpacing) { + ow.newline(); + } ow.ensureNewline(); + first = false; method.writeOut(ow); ow.ensureNewline(); } @@ -985,7 +990,7 @@ void QmlObject::writeOut(DomItem &self, OutWriter &ow, QString onTarget) const ow.removeTextAddCallback(spacerId); if (counter != ow.counter()) spacerId = ow.addNewlinesAutospacerCallback(2); - bool first = true; + first = true; for (auto c : field(self, Fields::children).values()) { if (!first && ow.lineWriter.options().objectsSpacing) { ow.newline().newline(); diff --git a/src/qmldom/qqmldomlinewriter_p.h b/src/qmldom/qqmldomlinewriter_p.h index 18f1b9916a..eb858f5b7d 100644 --- a/src/qmldom/qqmldomlinewriter_p.h +++ b/src/qmldom/qqmldomlinewriter_p.h @@ -96,6 +96,7 @@ public: Updates updateOptions = Update::Default; AttributesSequence attributesSequence = AttributesSequence::Normalize; bool objectsSpacing = false; + bool functionsSpacing = false; }; Q_DECLARE_OPERATORS_FOR_FLAGS(LineWriterOptions::Updates) diff --git a/tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml b/tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml new file mode 100644 index 0000000000..d452ba2b8c --- /dev/null +++ b/tests/auto/qml/qmlformat/data/functionsSpacing.formatted.qml @@ -0,0 +1,21 @@ +Item { + id: test + + signal testSignal + function test1() { + } + function test2() { + } + Button { + } + + function test4() { + } + + function test5() { + } + height: 360 + width: 360 + function test3() { + } +} diff --git a/tests/auto/qml/qmlformat/data/functionsSpacing.qml b/tests/auto/qml/qmlformat/data/functionsSpacing.qml new file mode 100644 index 0000000000..d8a5071445 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/functionsSpacing.qml @@ -0,0 +1,22 @@ +Item { + id: test + + signal testSignal + function test1() { + } + function test2() { + } + Button { + } + + function test4() { + } + + + function test5() { + } + height: 360 + width: 360 + function test3() { + } +} diff --git a/tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.formatted.qml b/tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.formatted.qml new file mode 100644 index 0000000000..5b41da753b --- /dev/null +++ b/tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.formatted.qml @@ -0,0 +1,26 @@ +Item { + id: test + + signal testSignal + + function test1() { + } + + function test2() { + } + + function test3() { + } + + function test4() { + } + + function test5() { + } + + height: 360 + width: 360 + + Button { + } +} diff --git a/tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.qml b/tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.qml new file mode 100644 index 0000000000..5451e0e66a --- /dev/null +++ b/tests/auto/qml/qmlformat/data/normalizedFunctionsSpacing.qml @@ -0,0 +1,23 @@ +Item { + id: test + + signal testSignal + function test1() { + } + function test2() { + } + Button { + } + + function test4() { + } + + + function test5() { + } + height: 360 + width: 360 + function test3() { + } + +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 87543ab306..450e50465e 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -290,6 +290,14 @@ void TestQmlformat::testFormat_data() QTest::newRow("ids new lines") << "checkIdsNewline.qml" << "checkIdsNewline.formatted.qml" << QStringList { "-n" } << RunOption::OnCopy; + + QTest::newRow("functions spacing (no changes)") + << "functionsSpacing.qml" + << "functionsSpacing.formatted.qml" << QStringList { "--functions-spacing" } << RunOption::OnCopy; + + QTest::newRow("normalize + functions spacing") + << "normalizedFunctionsSpacing.qml" + << "normalizedFunctionsSpacing.formatted.qml" << QStringList { "-n", "--functions-spacing" } << RunOption::OnCopy; } void TestQmlformat::testFormat() diff --git a/tools/qmlformat/qmlformat.cpp b/tools/qmlformat/qmlformat.cpp index 0da477af82..b1e0bb75de 100644 --- a/tools/qmlformat/qmlformat.cpp +++ b/tools/qmlformat/qmlformat.cpp @@ -34,6 +34,7 @@ struct Options bool ignoreSettings = false; bool writeDefaultSettings = false; bool objectsSpacing = false; + bool functionsSpacing = false; int indentWidth = 4; bool indentWidthSet = false; @@ -120,6 +121,7 @@ bool parseFile(const QString &filename, const Options &options) checks = WriteOutCheck::None; lwOptions.objectsSpacing = options.objectsSpacing; + lwOptions.functionsSpacing = options.functionsSpacing; MutableDomItem res; if (options.inplace) { @@ -190,6 +192,8 @@ Options buildCommandLineOptions(const QCoreApplication &app) parser.addOption(QCommandLineOption(QStringList() << "objects-spacing", QStringLiteral("Ensure spaces between objects (only works with normalize option)."))); + parser.addOption(QCommandLineOption(QStringList() << "functions-spacing", QStringLiteral("Ensure spaces between functions (only works with normalize option)."))); + parser.addPositionalArgument("filenames", "files to be processed by qmlformat"); parser.process(app); @@ -234,6 +238,7 @@ Options buildCommandLineOptions(const QCoreApplication &app) options.normalize = parser.isSet("normalize"); options.ignoreSettings = parser.isSet("ignore-settings"); options.objectsSpacing = parser.isSet("objects-spacing"); + options.functionsSpacing = parser.isSet("functions-spacing"); options.valid = true; options.indentWidth = indentWidth; @@ -270,6 +275,9 @@ int main(int argc, char *argv[]) const QString &objectsSpacingSetting = QStringLiteral("ObjectsSpacing"); settings.addOption(objectsSpacingSetting); + const QString &functionsSpacingSetting = QStringLiteral("FunctionsSpacing"); + settings.addOption(functionsSpacingSetting); + const auto options = buildCommandLineOptions(app); if (!options.valid) { for (const auto &error : options.errors) { @@ -305,6 +313,9 @@ int main(int argc, char *argv[]) if (settings.isSet(objectsSpacingSetting)) perFileOptions.objectsSpacing = settings.value(objectsSpacingSetting).toBool(); + if (settings.isSet(functionsSpacingSetting)) + perFileOptions.functionsSpacing = settings.value(functionsSpacingSetting).toBool(); + return perFileOptions; }; |