Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmldomattachedinfo.cpp
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
4#include "qqmldomelements_p.h"
5
7namespace QQmlJS {
8namespace Dom {
9
30{
31 bool cont = true;
32#ifdef QmlDomAddCodeStr
33 bool hasCode = false;
34 QString codeStr = self.fileObject().field(Fields::code).value().toString();
35 auto loc2str = [&self, &codeStr](SourceLocation loc) {
36 if (loc.offset < codeStr.length() && loc.end() <= codeStr.length())
37 return QStringView(codeStr).mid(loc.offset, loc.length);
38 return QStringView();
39 };
40#else
41 auto loc2str = [](SourceLocation) { return QStringView(); };
42#endif
43 cont = cont && self.dvValueLazyField(visitor, Fields::fullRegion, [this]() {
45 });
46 cont = cont && self.dvItemField(visitor, Fields::regions, [this, &self, &loc2str]() {
47 return self.subMapItem(Map::fromMapRef<SourceLocation>(
48 self.pathFromOwner().field(Fields::regions), regions,
49 [&loc2str](DomItem &map, const PathEls::PathComponent &key, SourceLocation &el) {
50 return map.subLocationItem(key, el, loc2str(el));
51 }));
52 });
53 cont = cont
54 && self.dvItemField(visitor, Fields::preCommentLocations, [this, &self, &loc2str]() {
55 return self.subMapItem(Map::fromMapRef<QList<SourceLocation>>(
56 self.pathFromOwner().field(Fields::preCommentLocations),
58 [&loc2str](DomItem &map, const PathEls::PathComponent &key,
60 return map.subListItem(List::fromQListRef<SourceLocation>(
61 map.pathFromOwner().appendComponent(key), el,
62 [&loc2str](DomItem &list, const PathEls::PathComponent &idx,
63 SourceLocation &el) {
64 return list.subLocationItem(idx, el, loc2str(el));
65 }));
66 }));
67 });
68 cont = cont
69 && self.dvItemField(visitor, Fields::postCommentLocations, [this, &self, &loc2str]() {
70 return self.subMapItem(Map::fromMapRef<QList<SourceLocation>>(
71 self.pathFromOwner().field(Fields::postCommentLocations),
72 postCommentLocations,
73 [&loc2str](DomItem &map, const PathEls::PathComponent &key,
75 return map.subListItem(List::fromQListRef<SourceLocation>(
76 map.pathFromOwner().appendComponent(key), el,
77 [&loc2str](DomItem &list, const PathEls::PathComponent &idx,
79 return list.subLocationItem(idx, el, loc2str(el));
80 }));
81 }));
82 });
83 return cont;
84}
85
86void FileLocations::ensureCommentLocations(QList<QString> keys)
87{
88 for (auto k : keys) {
89 preCommentLocations[k];
90 postCommentLocations[k];
91 }
92}
93
94FileLocations::Tree FileLocations::createTree(Path basePath){
96}
97
99 return AttachedInfoT<FileLocations>::ensure(base, basePath, pType);
100}
101
103FileLocations::findAttachedInfo(DomItem &item, AttachedInfo::FindOptions options)
104{
105 return AttachedInfoT<FileLocations>::findAttachedInfo(item, Fields::fileLocationsTree, options);
106}
107
112FileLocations::Tree FileLocations::treeOf(DomItem &item)
113{
114 return AttachedInfoT<FileLocations>::treePtr(item, Fields::fileLocationsTree);
115}
116
121const FileLocations *FileLocations::fileLocationsOf(DomItem &item)
122{
123 if (FileLocations::Tree t = treeOf(item))
124 return &(t->info());
125 return nullptr;
126}
127
128void FileLocations::updateFullLocation(FileLocations::Tree fLoc, SourceLocation loc) {
129 Q_ASSERT(fLoc);
130 if (loc != SourceLocation()) {
131 FileLocations::Tree p = fLoc;
132 while (p) {
133 SourceLocation &l = p->info().fullRegion;
134 if (loc.begin() < l.begin() || loc.end() > l.end())
135 l = combine(l, loc);
136 else
137 break;
138 p = p->parent();
139 }
140 }
141}
142
143void FileLocations::addRegion(FileLocations::Tree fLoc, QString locName, SourceLocation loc) {
144 Q_ASSERT(fLoc);
145 fLoc->info().regions[locName] = loc;
146 updateFullLocation(fLoc, loc);
147}
148
149void FileLocations::addRegion(FileLocations::Tree fLoc, QStringView locName, SourceLocation loc) {
150 addRegion(fLoc, locName.toString(), loc);
151}
152
171bool AttachedInfo::iterateDirectSubpaths(DomItem &self, DirectVisitor visitor)
172{
173 bool cont = true;
174 if (Ptr p = parent())
175 cont = cont && self.dvItemField(visitor, Fields::parent, [&self, p]() {
176 return self.copy(p, self.m_ownerPath.dropTail(2), p.get());
177 });
178 cont = cont
179 && self.dvValueLazyField(visitor, Fields::path, [this]() { return path().toString(); });
180 cont = cont && self.dvItemField(visitor, Fields::subItems, [this, &self]() {
181 return self.subMapItem(Map(
182 Path::Field(Fields::subItems),
183 [this](DomItem &map, QString key) {
184 Path p = Path::fromString(key);
185 return map.copy(m_subItems.value(p), map.canonicalPath().key(key));
186 },
187 [this](DomItem &) {
189 for (auto p : m_subItems.keys())
190 res.insert(p.toString());
191 return res;
192 },
193 QLatin1String("AttachedInfo")));
194 });
195 cont = cont && self.dvItemField(visitor, Fields::infoItem, [&self, this]() {
196 return infoItem(self);
197 });
198 return cont;
199}
200
201AttachedInfo::AttachedInfo(const AttachedInfo &o):
202 OwningItem(o),
203 m_parent(o.m_parent)
204{
205}
206
214 switch (pType) {
215 case PathType::Canonical: {
216 if (!path)
217 return nullptr;
218 Q_ASSERT(self);
219 Path removed = path.mid(0, self->path().length());
220 Q_ASSERT(removed == self->path());
221 path = path.mid(self->path().length());
222 } break;
224 Q_ASSERT(self);
225 break;
226 }
227 Ptr res = self;
228 for (auto p : path) {
229 if (AttachedInfo::Ptr subEl = res->m_subItems.value(p)) {
230 res = subEl;
231 } else {
232 AttachedInfo::Ptr newEl = res->instantiate(res, p);
233 res->m_subItems.insert(p, newEl);
234 res = newEl;
235 }
236 }
237 return res;
238}
239
241 if (pType == PathType::Canonical) {
242 if (!self) return nullptr;
243 Path removed = p.mid(0, self->path().length());
244 if (removed != self->path())
245 return nullptr;
246 p = p.dropFront(self->path().length());
247 }
248 AttachedInfo::Ptr res = self;
249 Path rest = p;
250 while (rest) {
251 if (!res)
252 break;
253 res = res->m_subItems.value(rest.head());
254 rest = rest.dropFront();
255 }
256 return res;
257}
258
261 AttachedInfo::FindOptions options)
262{
263 Path p;
264 DomItem fLoc = item.field(fieldName);
265 if (!fLoc) {
266 // owner or container.owner should be a file, so this works, but we could simply use the
267 // canonical path, and PathType::Canonical instead...
268 DomItem o = item.owner();
269 p = item.pathFromOwner();
270 fLoc = o.field(fieldName);
271 while (!fLoc && o) {
272 DomItem c = o.container();
273 p = c.pathFromOwner().path(o.canonicalPath().last()).path(p);
274 o = c.owner();
275 fLoc = o.field(fieldName);
276 }
277 }
279 res.lookupPath = p;
280 if (AttachedInfo::Ptr fLocPtr = fLoc.ownerAs<AttachedInfo>())
281 if (AttachedInfo::Ptr foundTree =
283 res.foundTree = foundTree;
285 res.rootTreePath = fLoc.canonicalPath();
286 if (options & FindOption::SetFoundTreePath) {
287 Path foundTreePath = res.rootTreePath.value();
288 if (res.lookupPath) {
289 foundTreePath = foundTreePath.key(res.lookupPath.head().toString());
290 for (Path pEl : res.lookupPath.mid(1))
291 foundTreePath = foundTreePath.field(Fields::subItems).key(pEl.toString());
292 }
293 res.foundTreePath = foundTreePath;
294 }
295 return res;
296}
297
299{
300 bool cont = true;
301 cont = cont && self.dvWrapField(visitor, Fields::expr, expr);
302 return cont;
303}
304
306{
308}
309
311 Path basePath,
313{
315}
316
318UpdatedScriptExpression::findAttachedInfo(DomItem &item, AttachedInfo::FindOptions options)
319{
321 item, Fields::updatedScriptExpressions, options);
322}
323
325{
326 return AttachedInfoT<UpdatedScriptExpression>::treePtr(item, Fields::updatedScriptExpressions);
327}
328
330{
332 return &(t->info());
333 return nullptr;
334}
335
337 Path basePath)
338{
340}
341
342} // namespace Dom
343} // namespace QQmlJS
345
346#include "moc_qqmldomattachedinfo_p.cpp"
Definition qlist.h:74
Key key(const T &value, const Key &defaultKey=Key()) const
Definition qmap.h:348
static bool visitTree(Ptr base, function_ref< bool(Path, Ptr)>visitor, Path basePath=Path())
static Ptr treePtr(DomItem &item, QStringView fieldName)
static Ptr ensure(Ptr self, Path path, PathType pType=PathType::Relative)
static Ptr createTree(Path p=Path())
static AttachedInfoLookupResult< Ptr > findAttachedInfo(DomItem &item, QStringView fieldName, AttachedInfo::FindOptions options)
Attached info creates a tree to attach extra info to DomItems.
static Ptr find(Ptr self, Path p, PathType pType=PathType::Relative)
static AttachedInfoLookupResult< Ptr > findAttachedInfo(DomItem &item, QStringView treeFieldName, FindOptions options=AttachedInfo::FindOption::None)
static Ptr ensure(Ptr self, Path path, PathType pType=PathType::Relative)
Returns that the AttachedInfo corresponding to the given path, creating it if it does not exists.
std::shared_ptr< AttachedInfo > Ptr
std::shared_ptr< T > ownerAs()
Represents and maintains a mapping between elements and their location in a file.
QMap< QString, QList< SourceLocation > > preCommentLocations
std::shared_ptr< AttachedInfoT< FileLocations > > Tree
bool iterateDirectSubpaths(DomItem &self, DirectVisitor)
QMap< QString, SourceLocation > regions
Path key(QString name) const
Path field(QString name) const
Path head() const
Path mid(int offset, int length) const
Path dropFront(int n=1) const
std::shared_ptr< AttachedInfoT< UpdatedScriptExpression > > Tree
static bool visitTree(Tree base, function_ref< bool(Path, Tree)> visitor, Path basePath=Path())
static Tree ensure(Tree base, Path basePath, AttachedInfo::PathType pType)
static AttachedInfoLookupResult< Tree > findAttachedInfo(DomItem &item, AttachedInfo::FindOptions options=AttachedInfo::FindOption::Default)
std::shared_ptr< ScriptExpression > expr
static const UpdatedScriptExpression * exprPtr(DomItem &)
bool iterateDirectSubpaths(DomItem &self, DirectVisitor)
Definition qset.h:18
\inmodule QtCore
Definition qstringview.h:76
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1014
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString & insert(qsizetype i, QChar c)
Definition qstring.cpp:3110
qsizetype length() const
Returns the number of characters in this string.
Definition qstring.h:187
QMap< QString, QString > map
[6]
QMLDOM_EXPORT QCborValue locationToData(SourceLocation loc, QStringView strValue=u"")
Combined button and popup list for selecting options.
GLuint64 key
GLuint res
const GLubyte * c
GLdouble GLdouble t
Definition qopenglext.h:243
GLsizei const GLchar *const * path
GLfloat GLfloat p
[1]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
QList< QPair< QString, QString > > Map
QList< int > list
[14]
QStringList keys
QGraphicsItem * item
QStringView el
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent