Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmldomattachedinfo_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QMLDOMATTACHEDINFO_P_H
5#define QMLDOMATTACHEDINFO_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qqmldom_global.h"
19#include "qqmldomitem_p.h"
20
21#include <memory>
22#include <optional>
23
25
26namespace QQmlJS {
27namespace Dom {
28template<typename TreePtr>
30{
31public:
32 TreePtr foundTree;
33 Path lookupPath; // relative path used to reach result
34 std::optional<Path> rootTreePath; // path of the root TreePath
35 std::optional<Path> foundTreePath;
36 operator bool() { return bool(foundTree); }
37 template<typename T>
39 {
41 res.foundTree = std::static_pointer_cast<T>(foundTree);
42 res.lookupPath = lookupPath;
43 res.rootTreePath = rootTreePath;
44 return res;
45 }
46};
47
50public:
51 enum class PathType {
52 Relative,
53 Canonical
54 };
55 Q_ENUM(PathType)
56 enum class FindOption {
57 None = 0,
58 SetRootTreePath = 0x1,
59 SetFoundTreePath = 0x2,
60 Default = 0x3
61 };
62 Q_DECLARE_FLAGS(FindOptions, FindOption)
63 Q_FLAG(FindOptions)
64
65 constexpr static DomType kindValue = DomType::AttachedInfo;
66 using Ptr = std::shared_ptr<AttachedInfo>;
67
68 DomType kind() const override { return kindValue; }
69 Path canonicalPath(DomItem &self) const override { return self.m_ownerPath; }
70 bool iterateDirectSubpaths(DomItem &self, DirectVisitor visitor) override;
71
73 {
74 return std::static_pointer_cast<AttachedInfo>(doCopy(self));
75 }
76
77 Ptr parent() const { return m_parent.lock(); }
78 Path path() const { return m_path; }
79 void setPath(Path p) { m_path = p; }
80
81 AttachedInfo(Ptr parent = nullptr, Path p = Path()) : m_path(p), m_parent(parent) {}
83
84 static Ptr ensure(Ptr self, Path path, PathType pType = PathType::Relative);
85 static Ptr find(Ptr self, Path p, PathType pType = PathType::Relative);
87 findAttachedInfo(DomItem &item, QStringView treeFieldName,
88 FindOptions options = AttachedInfo::FindOption::None);
89 static Ptr treePtr(DomItem &item, QStringView fieldName)
90 {
91 return findAttachedInfo(item, fieldName, FindOption::None).foundTree;
92 }
93
94 DomItem itemAtPath(DomItem &self, Path p, PathType pType = PathType::Relative) const
95 {
96 if (Ptr resPtr = find(self.ownerAs<AttachedInfo>(), p, pType)) {
97 if (pType == PathType::Canonical)
98 p = p.mid(m_path.length());
99 Path resPath = self.canonicalPath();
100 for (Path pEl : p) {
101 resPath = resPath.field(Fields::subItems).key(pEl.toString());
102 }
103 return self.copy(resPtr, resPath);
104 }
105 return DomItem();
106 }
107
108 DomItem infoAtPath(DomItem &self, Path p, PathType pType = PathType::Relative) const
109 {
110 return itemAtPath(self, p, pType).field(Fields::infoItem);
111 }
112
114 PathType pType = PathType::Relative)
115 {
116 if (Ptr resPtr = ensure(self.ownerAs<AttachedInfo>(), p, pType)) {
117 if (pType == PathType::Canonical)
118 p = p.mid(m_path.length());
119 Path resPath = self.canonicalPath();
120 for (Path pEl : p) {
121 resPath = resPath.field(Fields::subItems).key(pEl.toString());
122 }
123 return MutableDomItem(self.item().copy(resPtr, resPath));
124 }
125 return MutableDomItem();
126 }
127
129 PathType pType = PathType::Relative)
130 {
131 return ensureItemAtPath(self, p, pType).field(Fields::infoItem);
132 }
133
135 virtual DomItem infoItem(DomItem &self) = 0;
137 {
138 return const_cast<AttachedInfo *>(this)->infoItem(self);
139 }
141 return m_subItems;
142 }
144 m_subItems = v;
145 }
146protected:
148 std::weak_ptr<AttachedInfo> m_parent;
150};
151Q_DECLARE_OPERATORS_FOR_FLAGS(AttachedInfo::FindOptions)
152
153template<typename Info>
155{
156public:
157 constexpr static DomType kindValue = DomType::AttachedInfo;
158 using Ptr = std::shared_ptr<AttachedInfoT>;
159 using InfoType = Info;
160
164 m_info(o.m_info)
165 {
166 auto end = o.m_subItems.end();
167 auto i = o.m_subItems.begin();
168 while (i != end) {
169 m_subItems.insert(i.key(), Ptr(
170 new AttachedInfoT(*std::static_pointer_cast<AttachedInfoT>(i.value()).get())));
171 }
172 }
173
174 static Ptr createTree(Path p = Path()) {
175 return Ptr(new AttachedInfoT(nullptr, p));
176 }
177
178 static Ptr ensure(Ptr self, Path path, PathType pType = PathType::Relative){
179 return std::static_pointer_cast<AttachedInfoT>(AttachedInfo::ensure(self, path, pType));
180 }
181
182 static Ptr find(Ptr self, Path p, PathType pType = PathType::Relative){
183 return std::static_pointer_cast<AttachedInfoT>(AttachedInfo::find(self, p, pType));
184 }
185
187 AttachedInfo::FindOptions options)
188 {
189 return AttachedInfo::findAttachedInfo(item, fieldName, options)
190 .template as<AttachedInfoT>();
191 }
192 static Ptr treePtr(DomItem &item, QStringView fieldName)
193 {
194 return std::static_pointer_cast<AttachedInfoT>(AttachedInfo::treePtr(item, fieldName));
195 }
196 static bool visitTree(Ptr base, function_ref<bool(Path, Ptr)>visitor, Path basePath = Path()) {
197 if (base) {
198 Path pNow = basePath.path(base->path());
199 if (visitor(pNow, base)) {
200 auto it = base->m_subItems.cbegin();
201 auto end = base->m_subItems.cend();
202 while (it != end) {
203 if (!visitTree(std::static_pointer_cast<AttachedInfoT>(it.value()), visitor, pNow))
204 return false;
205 ++it;
206 }
207 } else {
208 return false;
209 }
210 }
211 return true;
212 }
213
215 return Ptr(new AttachedInfoT(std::static_pointer_cast<AttachedInfoT>(parent), p));
216 }
217 DomItem infoItem(DomItem &self) override { return self.wrapField(Fields::infoItem, m_info); }
218
219 Ptr makeCopy(DomItem &self) const
220 {
221 return std::static_pointer_cast<AttachedInfoT>(doCopy(self));
222 }
223
224 Ptr parent() const { return std::static_pointer_cast<AttachedInfoT>(AttachedInfo::parent()); }
225
226 const Info &info() const { return m_info; }
227 Info &info() { return m_info; }
228protected:
229 std::shared_ptr<OwningItem> doCopy(DomItem &) const override
230 {
231 return Ptr(new AttachedInfoT(*this));
232 }
233
234private:
235 Info m_info;
236};
237
239public:
240 using Tree = std::shared_ptr<AttachedInfoT<FileLocations>>;
241 constexpr static DomType kindValue = DomType::FileLocations;
242 DomType kind() const { return kindValue; }
243 bool iterateDirectSubpaths(DomItem &self, DirectVisitor);
244 void ensureCommentLocations(QList<QString> keys);
245
246 static Tree createTree(Path basePath);
247 static Tree ensure(Tree base, Path basePath,
248 AttachedInfo::PathType pType = AttachedInfo::PathType::Relative);
249 static Tree find(Tree self, Path p,
250 AttachedInfo::PathType pType = AttachedInfo::PathType::Relative)
251 {
252 return AttachedInfoT<FileLocations>::find(self, p, pType);
253 }
254
255 // returns the path looked up and the found tree when looking for the info attached to item
257 findAttachedInfo(DomItem &item,
258 AttachedInfo::FindOptions options = AttachedInfo::FindOption::Default);
259 static FileLocations::Tree treeOf(DomItem &);
260 static const FileLocations *fileLocationsOf(DomItem &);
261
262 static void updateFullLocation(Tree fLoc, SourceLocation loc);
263 static void addRegion(Tree fLoc, QString locName, SourceLocation loc);
264 static void addRegion(Tree fLoc, QStringView locName, SourceLocation loc);
265
270};
271
273{
275public:
276 using Tree = std::shared_ptr<AttachedInfoT<UpdatedScriptExpression>>;
277 constexpr static DomType kindValue = DomType::UpdatedScriptExpression;
278 DomType kind() const { return kindValue; }
279 bool iterateDirectSubpaths(DomItem &self, DirectVisitor);
280
281 static Tree createTree(Path basePath);
282 static Tree ensure(Tree base, Path basePath, AttachedInfo::PathType pType);
283
284 // returns the path looked up and the found tree when looking for the info attached to item
286 findAttachedInfo(DomItem &item,
287 AttachedInfo::FindOptions options = AttachedInfo::FindOption::Default);
288 // convenience: find FileLocations::Tree attached to the given item
289 static Tree treePtr(DomItem &);
290 // convenience: find FileLocations* attached to the given item (if there is one)
291 static const UpdatedScriptExpression *exprPtr(DomItem &);
292
293 static bool visitTree(Tree base, function_ref<bool(Path, Tree)> visitor,
294 Path basePath = Path());
295
296 std::shared_ptr<ScriptExpression> expr;
297};
298
299} // end namespace Dom
300} // end namespace QQmlJS
301
303#endif // QMLDOMATTACHEDINFO_P_H
Definition qlist.h:74
Definition qmap.h:186
AttachedInfoLookupResult< std::shared_ptr< T > > as() const
AttachedInfoT(Ptr parent=nullptr, Path p=Path())
Ptr makeCopy(DomItem &self) const
static bool visitTree(Ptr base, function_ref< bool(Path, Ptr)>visitor, Path basePath=Path())
static Ptr treePtr(DomItem &item, QStringView fieldName)
static Ptr find(Ptr self, Path p, PathType pType=PathType::Relative)
std::shared_ptr< AttachedInfoT > Ptr
static Ptr ensure(Ptr self, Path path, PathType pType=PathType::Relative)
DomItem infoItem(DomItem &self) override
AttachedInfo::Ptr instantiate(AttachedInfo::Ptr parent, Path p=Path()) const override
static Ptr createTree(Path p=Path())
std::shared_ptr< OwningItem > doCopy(DomItem &) const override
AttachedInfoT(const AttachedInfoT &o)
static AttachedInfoLookupResult< Ptr > findAttachedInfo(DomItem &item, QStringView fieldName, AttachedInfo::FindOptions options)
Attached info creates a tree to attach extra info to DomItems.
AttachedInfo(Ptr parent=nullptr, Path p=Path())
DomItem infoAtPath(DomItem &self, Path p, PathType pType=PathType::Relative) const
void setSubItems(QMap< Path, Ptr > v)
Path canonicalPath(DomItem &self) const override
virtual AttachedInfo::Ptr instantiate(AttachedInfo::Ptr parent, Path p=Path()) const =0
AttachedInfo::Ptr makeCopy(DomItem &self) const
DomItem infoItem(DomItem &self) const
MutableDomItem ensureInfoAtPath(MutableDomItem &self, Path p, PathType pType=PathType::Relative)
virtual DomItem infoItem(DomItem &self)=0
static Ptr treePtr(DomItem &item, QStringView fieldName)
MutableDomItem ensureItemAtPath(MutableDomItem &self, Path p, PathType pType=PathType::Relative)
std::shared_ptr< AttachedInfo > Ptr
std::weak_ptr< AttachedInfo > m_parent
DomItem itemAtPath(DomItem &self, Path p, PathType pType=PathType::Relative) const
QMap< Path, Ptr > subItems() const
DomItem field(QStringView name)
Represents and maintains a mapping between elements and their location in a file.
QMap< QString, QList< SourceLocation > > preCommentLocations
std::shared_ptr< AttachedInfoT< FileLocations > > Tree
QMap< QString, QList< SourceLocation > > postCommentLocations
static Tree find(Tree self, Path p, AttachedInfo::PathType pType=AttachedInfo::PathType::Relative)
QMap< QString, SourceLocation > regions
MutableDomItem field(QStringView name)
Path key(QString name) const
Path path(Path toAdd, bool avoidToAddAsBase=false) const
Path field(QString name) const
std::shared_ptr< AttachedInfoT< UpdatedScriptExpression > > Tree
std::shared_ptr< ScriptExpression > expr
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
const_iterator cbegin() const
Definition qstring.h:1201
const_iterator cend() const
Definition qstring.h:1209
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
GLsizei const GLfloat * v
[13]
GLuint GLuint end
GLuint res
GLsizei const GLchar *const * path
GLfloat GLfloat p
[1]
#define QMLDOM_EXPORT
static QT_BEGIN_NAMESPACE const uint Default
Definition qsplitter_p.h:25
#define Q_ENUM(x)
#define Q_FLAG(x)
#define Q_GADGET
QStringList keys
QGraphicsItem * item
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent