From 5d9b0d30df831649ceed58fee778bb37ac6f630e Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Fri, 4 Sep 2020 11:22:12 +0200 Subject: qmlformat: Fix multiline comment attachment Pick-to: 5.15 Change-Id: I3ba8a4cd683df3309dd6df31b1fd426a0875f8fa Reviewed-by: Fabian Kosmale --- tools/qmlformat/commentastvisitor.cpp | 5 +++-- tools/qmlformat/commentastvisitor.h | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'tools/qmlformat') 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 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 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; }; -- cgit v1.2.3