aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmldom/qqmldomcomments_p.h
diff options
context:
space:
mode:
authorSemih Yavuz <semih.yavuz@qt.io>2024-01-15 15:40:04 +0100
committerSemih Yavuz <semih.yavuz@qt.io>2024-01-22 22:40:47 +0100
commitf6fc3cc66e8c669b051c6a5eb3fa46a4d202a579 (patch)
tree40b5dd5eee58b8976c8466c7cd3332bab6c90268 /src/qmldom/qqmldomcomments_p.h
parentdfb65530fe7377ad4c5f9aa2ef44512419e0c3b3 (diff)
qqmldomcomments: Add comment type in Comment class
Comment class obviously represents a comment. By design, a comment can be attached to a node or a dom region. Attachment can take place as a preComment or a postComment. Introduce an enum that represents the comment type. Also, provide better encapsulation in comment handling classes by hiding members and remove some methods from the class interface. Task-number: QTBUG-120484 Change-Id: I995fe97e2fc0a2980e43a6d529309cb18efba434 Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'src/qmldom/qqmldomcomments_p.h')
-rw-r--r--src/qmldom/qqmldomcomments_p.h52
1 files changed, 40 insertions, 12 deletions
diff --git a/src/qmldom/qqmldomcomments_p.h b/src/qmldom/qqmldomcomments_p.h
index 559783724c..39fa5a1878 100644
--- a/src/qmldom/qqmldomcomments_p.h
+++ b/src/qmldom/qqmldomcomments_p.h
@@ -73,11 +73,14 @@ public:
constexpr static DomType kindValue = DomType::Comment;
DomType kind() const { return kindValue; }
- Comment(const QString &c, int newlinesBefore = 1)
- : m_comment(c), m_newlinesBefore(newlinesBefore)
+ enum CommentType {Pre, Post};
+
+ Comment(const QString &c, int newlinesBefore = 1, CommentType type = Pre)
+ : m_comment(c), m_newlinesBefore(newlinesBefore), m_type(type)
{
}
- Comment(QStringView c, int newlinesBefore = 1) : m_comment(c), m_newlinesBefore(newlinesBefore)
+ Comment(QStringView c, int newlinesBefore = 1, CommentType type = Pre)
+ : m_comment(c), m_newlinesBefore(newlinesBefore), m_type(type)
{
}
@@ -88,6 +91,8 @@ public:
CommentInfo info() const { return CommentInfo(m_comment); }
void write(OutWriter &lw, SourceLocation *commentLocation = nullptr) const;
+ CommentType type() const { return m_type; }
+
friend bool operator==(const Comment &c1, const Comment &c2)
{
return c1.m_newlinesBefore == c2.m_newlinesBefore && c1.m_comment == c2.m_comment;
@@ -97,6 +102,7 @@ public:
private:
QStringView m_comment;
int m_newlinesBefore;
+ CommentType m_type;
};
class QMLDOM_EXPORT CommentedElement
@@ -112,15 +118,27 @@ public:
friend bool operator==(const CommentedElement &c1, const CommentedElement &c2)
{
- return c1.preComments == c2.preComments && c1.postComments == c2.postComments;
+ return c1.m_preComments == c2.m_preComments && c1.m_postComments == c2.m_postComments;
}
friend bool operator!=(const CommentedElement &c1, const CommentedElement &c2)
{
return !(c1 == c2);
}
- QList<Comment> preComments;
- QList<Comment> postComments;
+ void addComment(const Comment &comment)
+ {
+ if (comment.type() == Comment::CommentType::Pre)
+ m_preComments.append(comment);
+ else
+ m_postComments.append(comment);
+ }
+
+ const QList<Comment> &preComments() const { return m_preComments;}
+ const QList<Comment> &postComments() const { return m_postComments;}
+
+private:
+ QList<Comment> m_preComments;
+ QList<Comment> m_postComments;
};
class QMLDOM_EXPORT RegionComments
@@ -133,18 +151,28 @@ public:
friend bool operator==(const RegionComments &c1, const RegionComments &c2)
{
- return c1.regionComments == c2.regionComments;
+ return c1.m_regionComments == c2.m_regionComments;
}
friend bool operator!=(const RegionComments &c1, const RegionComments &c2)
{
return !(c1 == c2);
}
+ const QMap<FileLocationRegion, CommentedElement> &regionComments() const { return m_regionComments;}
+ Path addComment(const Comment &comment, FileLocationRegion region)
+ {
+ if (comment.type() == Comment::CommentType::Pre)
+ return addPreComment(comment, region);
+ else
+ return addPostComment(comment, region);
+ }
+
+private:
Path addPreComment(const Comment &comment, FileLocationRegion region)
{
- auto &preList = regionComments[region].preComments;
+ auto &preList = m_regionComments[region].preComments();
index_type idx = preList.size();
- preList.append(comment);
+ m_regionComments[region].addComment(comment);
return Path::Field(Fields::regionComments)
.key(fileLocationRegionName(region))
.field(Fields::preComments)
@@ -153,16 +181,16 @@ public:
Path addPostComment(const Comment &comment, FileLocationRegion region)
{
- auto &postList = regionComments[region].postComments;
+ auto &postList = m_regionComments[region].postComments();
index_type idx = postList.size();
- postList.append(comment);
+ m_regionComments[region].addComment(comment);
return Path::Field(Fields::regionComments)
.key(fileLocationRegionName(region))
.field(Fields::postComments)
.index(idx);
}
- QMap<FileLocationRegion, CommentedElement> regionComments;
+ QMap<FileLocationRegion, CommentedElement> m_regionComments;
};
class QMLDOM_EXPORT AstComments final : public OwningItem