From 20370505b3b38a5eaa73692557cd80a0ecc60bff Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Wed, 18 Mar 2020 12:16:12 +0100 Subject: qmlformat: Improve comment attachment - Fixes UiPublicMember nodes having a newline between comment and first node - Implements a Front_Inline type so comments at the beginning of object definitions are handled properly Fixes: QTBUG-82259 Change-Id: I0b40290037ce88a9ffe16390d72cbf3d704db41a Reviewed-by: Fabian Kosmale --- tests/auto/qml/qmlformat/data/FrontInline.formatted.qml | 3 +++ tests/auto/qml/qmlformat/data/FrontInline.qml | 2 ++ tests/auto/qml/qmlformat/tst_qmlformat.cpp | 6 ++++++ tools/qmlformat/commentastvisitor.cpp | 12 ++++++++++-- tools/qmlformat/commentastvisitor.h | 5 +++-- tools/qmlformat/dumpastvisitor.cpp | 6 +++++- 6 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 tests/auto/qml/qmlformat/data/FrontInline.formatted.qml create mode 100644 tests/auto/qml/qmlformat/data/FrontInline.qml diff --git a/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml b/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml new file mode 100644 index 0000000000..620fbf4120 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/FrontInline.formatted.qml @@ -0,0 +1,3 @@ +// This comment should be directly above Item after formatting +Item { +} diff --git a/tests/auto/qml/qmlformat/data/FrontInline.qml b/tests/auto/qml/qmlformat/data/FrontInline.qml new file mode 100644 index 0000000000..c63265481c --- /dev/null +++ b/tests/auto/qml/qmlformat/data/FrontInline.qml @@ -0,0 +1,2 @@ +Item { // This comment should be directly above Item after formatting +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index bc48e07ae4..c38fe85e24 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -47,6 +47,7 @@ private Q_SLOTS: void testAnnotations(); void testAnnotationsNoSort(); void testLineEndings(); + void testFrontInline(); void testReadOnlyProps(); @@ -199,6 +200,11 @@ void TestQmlformat::testAnnotationsNoSort() QCOMPARE(runQmlformat(testFile("Annotations.qml"), false, true), readTestFile("Annotations.formatted.nosort.qml")); } +void TestQmlformat::testFrontInline() +{ + QCOMPARE(runQmlformat(testFile("FrontInline.qml"), false, true), readTestFile("FrontInline.formatted.qml")); +} + void TestQmlformat::testReadOnlyProps() { QCOMPARE(runQmlformat(testFile("readOnlyProps.qml"), false, true), readTestFile("readOnlyProps.formatted.qml")); diff --git a/tools/qmlformat/commentastvisitor.cpp b/tools/qmlformat/commentastvisitor.cpp index 4dd241ff93..9383fa29aa 100644 --- a/tools/qmlformat/commentastvisitor.cpp +++ b/tools/qmlformat/commentastvisitor.cpp @@ -126,6 +126,14 @@ Comment CommentAstVisitor::findComment(SourceLocation first, SourceLocation last return Comment(m_engine, Comment::Location::Front, comments); } + if (locations & Comment::Location::Front_Inline) { + quint32 searchAt = first.startLine; + + const auto comments = findCommentsInLine(searchAt); + if (!comments.isEmpty()) + return Comment(m_engine, Comment::Location::Front_Inline, comments); + } + if (locations & Comment::Location::Back_Inline) { quint32 searchAt = last.startLine; @@ -198,13 +206,13 @@ void CommentAstVisitor::endVisit(StatementList *node) bool CommentAstVisitor::visit(UiObjectBinding *node) { - attachComment(node, Comment::Front | Comment::Back); + attachComment(node, Comment::Front | Comment::Front_Inline | Comment::Back); return true; } bool CommentAstVisitor::visit(UiObjectDefinition *node) { - attachComment(node, Comment::Front | Comment::Back); + attachComment(node, Comment::Front | Comment::Front_Inline | Comment::Back); return true; } diff --git a/tools/qmlformat/commentastvisitor.h b/tools/qmlformat/commentastvisitor.h index d3de0a9b9d..21c7eb6416 100644 --- a/tools/qmlformat/commentastvisitor.h +++ b/tools/qmlformat/commentastvisitor.h @@ -45,10 +45,11 @@ struct Comment enum Location : int { Front = 1, - Back = Front << 1, + Front_Inline = Front << 1, + Back = Front_Inline << 1, Back_Inline = Back << 1, DefaultLocations = Front | Back_Inline, - AllLocations = Front | Back | Back_Inline + AllLocations = Front | Back | Front_Inline | Back_Inline } m_location = Front; Comment() = default; diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index 7f1e285423..565e188160 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -843,8 +843,8 @@ QString DumpAstVisitor::parseStatementList(StatementList *list) } bool DumpAstVisitor::visit(UiPublicMember *node) { - addLine(getComment(node, Comment::Location::Front)); + QString commentFront = getComment(node, Comment::Location::Front); QString commentBackInline = getComment(node, Comment::Location::Back_Inline); switch (node->type) @@ -859,6 +859,7 @@ bool DumpAstVisitor::visit(UiPublicMember *node) { scope().m_firstSignal = false; } + addLine(commentFront); addLine("signal "+node->name.toString()+"("+parseUiParameterList(node->parameters) + ")" + commentBackInline); break; @@ -897,6 +898,7 @@ bool DumpAstVisitor::visit(UiPublicMember *node) { if (has_type_modifier) member_type = node->typeModifier + "<" + member_type + ">"; + addLine(commentFront); if (is_readonly && statement.isEmpty() && scope().m_bindings.contains(node->name.toString())) { m_result += formatLine(prefix + "property " + member_type + " ", false); @@ -1003,6 +1005,7 @@ bool DumpAstVisitor::visit(UiObjectDefinition *node) { } addLine(getComment(node, Comment::Location::Front)); + addLine(getComment(node, Comment::Location::Front_Inline)); addLine(parseUiQualifiedId(node->qualifiedTypeNameId) + " {"); m_indentLevel++; @@ -1198,6 +1201,7 @@ bool DumpAstVisitor::visit(UiObjectBinding *node) { } else { addNewLine(); addLine(getComment(node, Comment::Location::Front)); + addLine(getComment(node, Comment::Location::Front_Inline)); addLine(result + " {"); } -- cgit v1.2.3