Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
3#include "qqmldom_fwd_p.h"
5#include "qqmldomelements_p.h"
6#include "qqmldompath_p.h"
7
9namespace QQmlJS {
10namespace Dom {
11
12using namespace Qt::StringLiterals;
13
14
35{
36 bool cont = true;
37 cont = cont && self.dvValueLazyField(visitor, Fields::fullRegion, [this]() {
39 });
40 cont = cont && self.dvItemField(visitor, Fields::regions, [this, &self]() -> DomItem {
41 const Path pathFromOwner = self.pathFromOwner().field(Fields::regions);
42 auto map = Map::fromFileRegionMap(pathFromOwner, regions);
43 return self.subMapItem(map);
44 });
45 cont = cont
46 && self.dvItemField(visitor, Fields::preCommentLocations, [this, &self]() -> DomItem {
47 const Path pathFromOwner =
48 self.pathFromOwner().field(Fields::preCommentLocations);
49 auto map = Map::fromFileRegionListMap(pathFromOwner, preCommentLocations);
50 return self.subMapItem(map);
51 });
52 cont = cont
53 && self.dvItemField(visitor, Fields::postCommentLocations, [this, &self]() -> DomItem {
54 const Path pathFromOwner =
55 self.pathFromOwner().field(Fields::postCommentLocations);
56 auto map = Map::fromFileRegionListMap(pathFromOwner, postCommentLocations);
57 return self.subMapItem(map);
58 });
59 return cont;
60}
61
65
71
81AttachedInfoLookupResult<FileLocations::Tree>
86
95
101{
102 if (const FileLocations::Tree &t = treeOf(item))
103 return &(t->info());
104 return nullptr;
105}
106
108{
109 Q_ASSERT(fLoc);
110 if (loc != SourceLocation()) {
111 FileLocations::Tree p = fLoc;
112 while (p) {
113 SourceLocation &l = p->info().fullRegion;
114 if (loc.begin() < l.begin() || loc.end() > l.end()) {
115 l = combine(l, loc);
116 p->info().regions[MainRegion] = l;
117 } else {
118 break;
119 }
120 p = p->parent();
121 }
122 }
123}
124
125// Adding a new region to file location regions might break down qmlformat because
126// comments might be linked to new region undesirably. We might need to add an
127// exception to AstRangesVisitor::shouldSkipRegion when confronted those cases.
129 SourceLocation loc)
130{
131 Q_ASSERT(fLoc);
132 fLoc->info().regions[region] = loc;
133 updateFullLocation(fLoc, loc);
134}
135
137{
138 Q_ASSERT(fLoc);
139 const auto &regions = fLoc->info().regions;
140 if (auto it = regions.constFind(region); it != regions.constEnd() && it->isValid()) {
141 return *it;
142 }
143
144 if (region == MainRegion)
145 return fLoc->info().fullRegion;
146
147 return SourceLocation{};
148}
149
169{
170 bool cont = true;
171 if (Ptr p = parent())
172 cont = cont && self.dvItemField(visitor, Fields::parent, [&self, p]() {
173 return self.copy(p, self.m_ownerPath.dropTail(2), p.get());
174 });
175 cont = cont
176 && self.dvValueLazyField(visitor, Fields::path, [this]() { return path().toString(); });
177 cont = cont && self.dvItemField(visitor, Fields::subItems, [this, &self]() {
178 return self.subMapItem(Map(
179 Path::Field(Fields::subItems),
180 [this](const DomItem &map, const QString &key) {
182 return map.copy(m_subItems.value(p), map.canonicalPath().key(key));
183 },
184 [this](const DomItem &) {
185 QSet<QString> res;
186 for (const auto &p : m_subItems.keys())
187 res.insert(p.toString());
188 return res;
189 },
190 QLatin1String("AttachedInfo")));
191 });
192 cont = cont && self.dvItemField(visitor, Fields::infoItem, [&self, this]() {
193 return infoItem(self);
194 });
195 return cont;
196}
197
199 OwningItem(o),
200 m_parent(o.m_parent)
201{
202}
203
211 const AttachedInfo::Ptr &self, const Path &path, AttachedInfo::PathType pType){
212 Path relative;
213 switch (pType) {
214 case PathType::Canonical: {
215 if (!path)
216 return nullptr;
217 Q_ASSERT(self);
218 Path removed = path.mid(0, self->path().length());
219 Q_ASSERT(removed == self->path());
220 relative = path.mid(self->path().length());
221 } break;
223 Q_ASSERT(self);
224 relative = path;
225 break;
226 }
227 Ptr res = self;
228 for (const auto &p : std::as_const(relative)) {
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 const AttachedInfo::Ptr &self, const Path &p, AttachedInfo::PathType pType)
242{
243 Path rest;
244 if (pType == PathType::Canonical) {
245 if (!self) return nullptr;
246 Path removed = p.mid(0, self->path().length());
247 if (removed != self->path())
248 return nullptr;
249 rest = p.dropFront(self->path().length());
250 } else {
251 rest = p;
252 }
253
254 AttachedInfo::Ptr res = self;
255 while (rest) {
256 if (!res)
257 break;
258 res = res->m_subItems.value(rest.head());
259 rest = rest.dropFront();
260 }
261 return res;
262}
263
264AttachedInfoLookupResult<AttachedInfo::Ptr>
266{
267 Path p;
268 DomItem fLoc = item.field(fieldName);
269 if (!fLoc) {
270 // owner or container.owner should be a file, so this works, but we could simply use the
271 // canonical path, and PathType::Canonical instead...
272 DomItem o = item.owner();
273 p = item.pathFromOwner();
274 fLoc = o.field(fieldName);
275 while (!fLoc && o) {
276 DomItem c = o.container();
277 p = c.pathFromOwner().path(o.canonicalPath().last()).path(p);
278 o = c.owner();
279 fLoc = o.field(fieldName);
280 }
281 }
282 AttachedInfoLookupResult<AttachedInfo::Ptr> res;
283 res.lookupPath = p;
284 if (AttachedInfo::Ptr fLocPtr = fLoc.ownerAs<AttachedInfo>())
285 if (AttachedInfo::Ptr foundTree =
287 res.foundTree = foundTree;
288 res.rootTreePath = fLoc.canonicalPath();
289
290 res.foundTreePath = res.rootTreePath;
291 for (const Path &pEl : res.lookupPath)
292 res.foundTreePath = res.foundTreePath.field(Fields::subItems).key(pEl.toString());
293 return res;
294}
295
297{
298 bool cont = true;
299 cont = cont && self.dvWrapField(visitor, Fields::expr, expr);
300 return cont;
301}
302
307
314
315AttachedInfoLookupResult<UpdatedScriptExpression::Tree>
321
326
328{
330 return &(t->info());
331 return nullptr;
332}
333
335 const Tree &base, function_ref<bool(const Path &, const Tree &)> visitor,
336 const Path &basePath)
337{
339}
340
341} // namespace Dom
342} // namespace QQmlJS
344
345#include "moc_qqmldomattachedinfo_p.cpp"
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
Key key(const T &value, const Key &defaultKey=Key()) const
Definition qmap.h:349
static Ptr createTree(const Path &p=Path())
static bool visitTree(const Ptr &base, function_ref< bool(const Path &, const Ptr &)> visitor, const Path &basePath=Path())
static Ptr ensure(const Ptr &self, const Path &path, PathType pType=PathType::Relative)
static AttachedInfoLookupResult< Ptr > findAttachedInfo(const DomItem &item, QStringView fieldName)
static Ptr treePtr(const DomItem &item, QStringView fieldName)
Attached info creates a tree to attach extra info to DomItems.
virtual DomItem infoItem(const DomItem &self) const =0
static Ptr find(const Ptr &self, const Path &p, PathType pType=PathType::Relative)
static Ptr ensure(const Ptr &self, const Path &path, PathType pType=PathType::Relative)
Returns that the AttachedInfo corresponding to the given path, creating it if it does not exists.
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const override
static AttachedInfoLookupResult< Ptr > findAttachedInfo(const DomItem &item, QStringView treeFieldName)
std::shared_ptr< AttachedInfo > Ptr
AttachedInfo(const Ptr &parent=nullptr, const Path &p=Path())
Represents and maintains a mapping between elements and their location in a file.
QMap< FileLocationRegion, QList< SourceLocation > > preCommentLocations
static void addRegion(const Tree &fLoc, FileLocationRegion region, SourceLocation loc)
static const FileLocations * fileLocationsOf(const DomItem &)
static AttachedInfoLookupResult< Tree > findAttachedInfo(const DomItem &item)
QMap< FileLocationRegion, SourceLocation > regions
static QQmlJS::SourceLocation region(const Tree &fLoc, FileLocationRegion region)
std::shared_ptr< AttachedInfoT< FileLocations > > Tree
static Tree createTree(const Path &basePath)
static FileLocations::Tree treeOf(const DomItem &)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const
static Tree ensure(const Tree &base, const Path &basePath, AttachedInfo::PathType pType=AttachedInfo::PathType::Relative)
QMap< FileLocationRegion, QList< SourceLocation > > postCommentLocations
static void updateFullLocation(const Tree &fLoc, SourceLocation loc)
Path mid(int offset, int length) const
static Path fromString(const QString &s, const ErrorHandler &errorHandler=nullptr)
QString toString() const
static Path Field(QStringView s=u"")
Path dropFront(int n=1) const
static AttachedInfoLookupResult< Tree > findAttachedInfo(const DomItem &item)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const
std::shared_ptr< AttachedInfoT< UpdatedScriptExpression > > Tree
static const UpdatedScriptExpression * exprPtr(const DomItem &)
static bool visitTree(const Tree &base, function_ref< bool(const Path &, const Tree &)> visitor, const Path &basePath=Path())
static Tree ensure(const Tree &base, const Path &basePath, AttachedInfo::PathType pType)
static Tree createTree(const Path &basePath)
std::shared_ptr< ScriptExpression > expr
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString & insert(qsizetype i, QChar c)
Definition qstring.cpp:3132
qsizetype length() const noexcept
Returns the number of characters in this string.
Definition qstring.h:191
QMap< QString, QString > map
[6]
QSet< QString >::iterator it
QCborValue sourceLocationToQCborValue(QQmlJS::SourceLocation loc)
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
static const uint base
Definition qurlidna.cpp:20
QGraphicsItem * item