diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2020-03-18 12:16:12 +0100 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2020-03-25 09:01:14 +0100 |
commit | 20370505b3b38a5eaa73692557cd80a0ecc60bff (patch) | |
tree | baaf2c0ec8635e0724babf74a8d267c1249066eb /tools | |
parent | 4faf242d1a18e206ddd9c567649a9dddbf6217df (diff) |
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 <fabian.kosmale@qt.io>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qmlformat/commentastvisitor.cpp | 12 | ||||
-rw-r--r-- | tools/qmlformat/commentastvisitor.h | 5 | ||||
-rw-r--r-- | tools/qmlformat/dumpastvisitor.cpp | 6 |
3 files changed, 18 insertions, 5 deletions
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 + " {"); } |