Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmldomfilelocations.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
6
8namespace QQmlJS {
9namespace Dom {
10
11using namespace Qt::StringLiterals;
12
13/*!
14\internal
15\namespace QQmlJS::Dom::FileLocations
16\brief Provides entities to maintain mappings between elements and their location in a file
17
18The location information is associated with the element it refers to via ::Node
19There are free functions to simplify the handling of the FileLocations tree.
20*/
21namespace FileLocations {
22
23/*!
24\internal
25\namespace QQmlJS::Dom::FileLocations::Info
26\brief Contains region information about the item
27
28Attributes:
29\list
30\li fullRegion: A location guaranteed to include this element and all its sub elements
31\li regions: a map with locations of regions of this element, the empty string is the default region
32 of this element
33\endlist
34*/
35bool Info::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
36{
37 bool cont = true;
38 cont = cont && self.invokeVisitorOnLazyField(visitor, Fields::fullRegion, [this]() {
39 return sourceLocationToQCborValue(fullRegion);
40 });
41 cont = cont && visitor(Fields::regions, [this, &self]() -> DomItem {
42 const Path pathFromOwner = self.pathFromOwner().withField(Fields::regions);
43 auto map = Map::fromFileRegionMap(pathFromOwner, regions);
44 return self.subMapItem(map);
45 });
46 return cont;
47}
48
49Tree createTree(const Path &basePath)
50{
51 return Node::instantiate(nullptr, basePath);
52}
53
54Tree ensure(const Tree &base, const Path &basePath)
55{
56 Path relative = basePath;
57 Tree res = base;
58 for (const auto &p : std::as_const(relative)) {
59 res = res->insertOrReturnChildAt(p);
60 }
61 return res;
62}
63
64Tree find(const Tree &self, const Path &p)
65{
66 Tree res = self;
67 for (const Path &component : p) {
68 if (!res)
69 break;
70 res = res->subItems().value(component);
71 }
72 return res;
73}
74
75bool visitTree(const Tree &base, function_ref<bool(const Path &, const Tree &)> visitor,
76 const Path &basePath)
77{
78 if (base) {
79 Path pNow = basePath.withPath(base->path());
80 if (!visitor(pNow, base)) {
81 return false;
82 }
83 for (const auto &childNode : base->subItems()) {
84 if (!visitTree(childNode, visitor, pNow))
85 return false;
86 }
87 }
88 return true;
89}
90
92{
93 QString result;
94 for (auto *it = base.get(); it; it = it->parent().get()) {
95 result.prepend(it->path().toString());
96 }
97 return result;
98}
99
100/*!
101 \internal
102 Returns the tree corresponding to a DomItem.
103 */
104Tree treeOf(const DomItem &item)
105{
106 Path p;
107 DomItem fLoc = item.field(Fields::fileLocationsTree);
108 if (!fLoc) {
109 // owner or container.owner should be a file, so this works, but we could simply use the
110 // canonical path, and PathType::Canonical instead...
111 DomItem o = item.owner();
112 p = item.pathFromOwner();
113 fLoc = o.field(Fields::fileLocationsTree);
114 while (!fLoc && o) {
117 o = c.owner();
118 fLoc = o.field(Fields::fileLocationsTree);
119 }
120 }
121 if (Node::Ptr fLocPtr = fLoc.ownerAs<Node>())
122 return find(fLocPtr, p);
123 return {};
124}
125
126static void updateFullLocation(const Tree &fLoc, SourceLocation loc)
127{
128 Q_ASSERT(fLoc);
129 if (loc != SourceLocation()) {
130 Tree p = fLoc;
131 while (p) {
132 SourceLocation &l = p->info().fullRegion;
133 if (loc.begin() < l.begin() || loc.end() > l.end()) {
134 l = combine(l, loc);
135 p->info().regions[MainRegion] = l;
136 } else {
137 break;
138 }
139 p = p->parent();
140 }
141 }
142}
143
144// Adding a new region to file location regions might break down qmlformat because
145// comments might be linked to new region undesirably. We might need to add an
146// exception to AstRangesVisitor::shouldSkipRegion when confronted those cases.
147// beware of shrinking and reassigning regions (QTBUG-131288)
148void addRegion(const Tree &fLoc, FileLocationRegion region, SourceLocation loc)
149{
150 Q_ASSERT(fLoc);
151 fLoc->info().regions[region] = loc;
152 updateFullLocation(fLoc, loc);
153}
154
155SourceLocation region(const Tree &fLoc, FileLocationRegion region)
156{
157 Q_ASSERT(fLoc);
158 const auto &regions = fLoc->info().regions;
159 if (auto it = regions.constFind(region); it != regions.constEnd() && it->isValid()) {
160 return *it;
161 }
162
163 if (region == MainRegion)
164 return fLoc->info().fullRegion;
165
166 return SourceLocation{};
167}
168
169/*!
170\internal
171\class QQmlJS::Dom::FileLocations::Node
172\brief Represents a Node of FileLocations tree
173
174Attributes:
175\list
176\li parent: parent Node in tree (might be empty)
177\li subItems: subItems of the tree (path -> Node)
178\li infoItem: actual FileLocations::Info with regions
179\endlist
180
181\sa QQmlJs::Dom::Node
182*/
183
184Node::Ptr Node::instantiate(const Ptr &parent, const Path &p)
185{
186 return std::shared_ptr<Node>(new Node(parent, p));
187}
188
189bool Node::iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
190{
191 bool cont = true;
192 if (const Ptr p = parent()) {
193 cont = cont && visitor(Fields::parent, [&self, &p]() {
194 return self.copy(p, self.m_ownerPath.dropTail(2), p.get());
195 });
196 }
197 cont = cont && self.invokeVisitorOnLazyField(visitor, Fields::path, [this]() {
198 return path().toString();
199 });
200 cont = cont && visitor(Fields::subItems, [this, &self]() {
201 // keyed by Path::toString() to avoid reparsing the path in Map::key()
202 QMap<QString, Path> byName;
203 for (auto it = m_subItems.keyBegin(), end = m_subItems.keyEnd(); it != end; ++it)
204 byName.insert(it->toString(), *it);
205 return self.subMapItem(Map(
206 Path::fromField(Fields::subItems),
207 [this, byName](const DomItem &map, const QString &name) {
208 auto it = byName.find(name);
209 if (it == byName.end())
210 return DomItem();
211 return map.copy(m_subItems.value(*it),
212 map.canonicalPath().withKey(name));
213 },
214 [byName](const DomItem &) {
215 return QSet<QString>(byName.keyBegin(), byName.keyEnd());
216 },
217 QLatin1String("Node")));
218 });
219 cont = cont && visitor(Fields::infoItem, [&self, this]() {
220 return self.wrap(Fields::infoItem, m_info);
221 });
222 return cont;
223}
224
226{
227 if (Node::Ptr subEl = m_subItems.value(path)) {
228 return subEl;
229 }
230 return m_subItems.insert(path, Node::instantiate(shared_from_this(), path)).value();
231}
232
233std::shared_ptr<OwningItem> Node::doCopy(const DomItem &) const
234{
235 return std::shared_ptr<Node>(new Node(*this));
236}
237
238} // namespace FileLocations
239} // namespace Dom
240} // namespace QQmlJS
241QT_END_NAMESPACE
A value type that references any element of the Dom.
DomItem container() const
DomItem field(FieldName name) const
Path canonicalPath() const
DomItem subMapItem(const Map &map) const
DomItem owner() const
The owner of an element, for an qmlObject this is the containing qml file.
Path pathFromOwner() const
Represents a Node of FileLocations tree.
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const override
std::shared_ptr< OwningItem > doCopy(const DomItem &) const override
static Ptr instantiate(const Ptr &parent=nullptr, const Path &p=Path())
A DomItem that owns other DomItems and is managed through a shared pointer.
Represents an immutable JsonPath like path in the Qml code model (from a DomItem to another DomItem).
Path withField(FieldName name) const
Path last() const
Path withPath(const Path &toAdd) const
Returns a copy of this with toAdd appended to it.
Provides entities to maintain mappings between elements and their location in a file.
void addRegion(const Tree &fLoc, FileLocationRegion region, SourceLocation loc)
Tree createTree(const Path &basePath)
QString canonicalPathForTesting(const Tree &base)
QQmlJS::SourceLocation region(const Tree &fLoc, FileLocationRegion region)
bool visitTree(const Tree &base, function_ref< bool(const Path &, const Tree &)> visitor, const Path &basePath=Path())
Tree ensure(const Tree &base, const Path &basePath)
static void updateFullLocation(const Tree &fLoc, SourceLocation loc)
Tree find(const Tree &self, const Path &p)
Combined button and popup list for selecting options.
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const