diff options
-rw-r--r-- | tests/auto/qml/qmlformat/data/multilineComment.formatted.qml | 12 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/multilineComment.qml | 10 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat.cpp | 7 | ||||
-rw-r--r-- | tools/qmlformat/commentastvisitor.cpp | 5 | ||||
-rw-r--r-- | tools/qmlformat/commentastvisitor.h | 8 |
5 files changed, 40 insertions, 2 deletions
diff --git a/tests/auto/qml/qmlformat/data/multilineComment.formatted.qml b/tests/auto/qml/qmlformat/data/multilineComment.formatted.qml new file mode 100644 index 0000000000..830ff32095 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/multilineComment.formatted.qml @@ -0,0 +1,12 @@ +Item { + Item { + } + + // This is a multiline comment. + // it should stay attached to Commented instead of getting orphaned. + // + // This should also stick to Commented + Commented { + } + +} diff --git a/tests/auto/qml/qmlformat/data/multilineComment.qml b/tests/auto/qml/qmlformat/data/multilineComment.qml new file mode 100644 index 0000000000..27439c41fa --- /dev/null +++ b/tests/auto/qml/qmlformat/data/multilineComment.qml @@ -0,0 +1,10 @@ +Item { + Item {} + + /* This is a multiline comment. + it should stay attached to Commented instead of getting orphaned. + */ + // This should also stick to Commented + Commented {} + +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index a3f5cd1a2b..3b1f232139 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -49,6 +49,7 @@ private Q_SLOTS: void testLineEndings(); void testFrontInline(); void testIfBlocks(); + void testMultilineComments(); void testReadOnlyProps(); void testVerbatimStrings(); @@ -221,6 +222,12 @@ void TestQmlformat::testIfBlocks() QCOMPARE(runQmlformat(testFile("IfBlocks.qml"), false, true), readTestFile("IfBlocks.formatted.qml")); } +void TestQmlformat::testMultilineComments() +{ + QCOMPARE(runQmlformat(testFile("multilineComment.qml"), false, true), readTestFile("multilineComment.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 9383fa29aa..b8d916d3fb 100644 --- a/tools/qmlformat/commentastvisitor.cpp +++ b/tools/qmlformat/commentastvisitor.cpp @@ -68,7 +68,8 @@ QList<SourceLocation> CommentAstVisitor::findCommentsInLine(quint32 line, bool i return results; for (const auto &location : m_engine->comments()) { - if (location.startLine != line) + Comment comment(m_engine, Comment::Location::Front, { location }); + if (line < location.startLine || line > comment.endLine()) continue; if (isCommentAttached(location)) @@ -78,7 +79,7 @@ QList<SourceLocation> CommentAstVisitor::findCommentsInLine(quint32 line, bool i if (includePrevious) { // See if we can find any more comments above this one - auto previous = findCommentsInLine(line - 1, true); + auto previous = findCommentsInLine(location.startLine - 1, true); // Iterate it in reverse to restore the correct order for (auto it = previous.rbegin(); it != previous.rend(); it++) { diff --git a/tools/qmlformat/commentastvisitor.h b/tools/qmlformat/commentastvisitor.h index 21c7eb6416..6ebf8246ba 100644 --- a/tools/qmlformat/commentastvisitor.h +++ b/tools/qmlformat/commentastvisitor.h @@ -78,6 +78,14 @@ struct Comment return false; } + quint32 endLine() const + { + if (isSyntheticMultiline() || !isValid()) + return 0; + + return m_srcLocations[0].startLine + m_text.count(QLatin1Char('\n')); + } + QString m_text; }; |