diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/parser/qqmljsengine_p.h | 4 | ||||
-rw-r--r-- | src/qmldom/qqmldomcomments.cpp | 30 |
2 files changed, 23 insertions, 11 deletions
diff --git a/src/qml/parser/qqmljsengine_p.h b/src/qml/parser/qqmljsengine_p.h index 568bf4d35b..37f78a30e1 100644 --- a/src/qml/parser/qqmljsengine_p.h +++ b/src/qml/parser/qqmljsengine_p.h @@ -71,8 +71,8 @@ public: void addComment(int pos, int len, int line, int col) { - if (len > 0) - _comments.append(QQmlJS::SourceLocation(pos, len, line, col)); + Q_ASSERT(len >= 0); + _comments.append(QQmlJS::SourceLocation(pos, len, line, col)); } QList<SourceLocation> comments() const { return _comments; } diff --git a/src/qmldom/qqmldomcomments.cpp b/src/qmldom/qqmldomcomments.cpp index 559f7c0d73..b9fc3e9ed5 100644 --- a/src/qmldom/qqmldomcomments.cpp +++ b/src/qmldom/qqmldomcomments.cpp @@ -99,8 +99,6 @@ CommentInfo::CommentInfo(QStringView rawComment) : rawComment(rawComment) } commentEnd = commentBegin + commentStartStr.size(); quint32 rawEnd = quint32(rawComment.size()); - while (commentEnd < rawEnd && rawComment.at(commentEnd).isSpace()) - ++commentEnd; commentContentEnd = commentContentBegin = commentEnd; QChar e1 = ((expectedEnd.isEmpty()) ? QChar::fromLatin1(0) : expectedEnd.at(0)); while (commentEnd < rawEnd) { @@ -115,7 +113,8 @@ CommentInfo::CommentInfo(QStringView rawComment) : rawComment(rawComment) commentContentEnd = commentEnd; } } else { - commentEndStr = rawComment.mid(++commentEnd - 1, 1); + // Comment ends with \n, treat as it is not part of the comment but post whitespace + commentEndStr = rawComment.mid(commentEnd - 1, 1); break; } } else if (!c.isSpace()) { @@ -503,6 +502,7 @@ void AstComments::collectComments(std::shared_ptr<Engine> engine, AST::Node *n, // do not add newline before, but add the one after quint32 iPre = cLoc.begin(); int preNewline = 0; + int postNewline = 0; QStringView commentStartStr; while (iPre > 0) { QChar c = code.at(iPre - 1); @@ -531,8 +531,10 @@ void AstComments::collectComments(std::shared_ptr<Engine> engine, AST::Node *n, } --iPre; } + if (iPre == 0) preNewline = 1; + qsizetype iPost = cLoc.end(); while (iPost < code.size()) { QChar c = code.at(iPost); @@ -545,16 +547,25 @@ void AstComments::collectComments(std::shared_ptr<Engine> engine, AST::Node *n, } else { break; } + } else { + if (c == QLatin1Char('\n')) { + ++postNewline; + if (iPost + 1 < code.size() && code.at(iPost + 1) == QLatin1Char('\n')) { + ++iPost; + ++postNewline; + } + } else if (c == QLatin1Char('\r')) { + if (iPost + 1 < code.size() && code.at(iPost + 1) == QLatin1Char('\n')) { + ++iPost; + ++postNewline; + } + } } ++iPost; - if (c == QLatin1Char('\n')) - break; - if (c == QLatin1Char('\r')) { - if (iPost < code.size() && code.at(iPost) == QLatin1Char('\n')) - ++iPost; + if (postNewline > 1) break; - } } + ElementRef commentEl; bool pre = true; auto iStart = ranges.starts.lowerBound(cLoc.begin()); @@ -653,6 +664,7 @@ void AstComments::collectComments(std::shared_ptr<Engine> engine, AST::Node *n, commentEl.size = n->lastSourceLocation().end() - n->firstSourceLocation().begin(); } } + Comment comment(code.mid(iPre, iPost - iPre), preNewline); if (commentEl.element.index() == 0 && std::get<0>(commentEl.element)) { CommentedElement &cEl = commentedElements[std::get<0>(commentEl.element)]; |