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
qqmllsutils_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 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// Qt-Security score:significant reason:default
4
5#ifndef QLANGUAGESERVERUTILS_P_H
6#define QLANGUAGESERVERUTILS_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtLanguageServer/private/qlanguageserverspectypes_p.h>
20#include <QtQmlCompiler/private/qqmljsutils_p.h>
21#include <QtQmlDom/private/qqmldomexternalitems_p.h>
22#include <QtQmlDom/private/qqmldomtop_p.h>
23#include <algorithm>
24#include <optional>
25#include <tuple>
26#include <variant>
27
29
31
32namespace QQmlLSUtils {
33
39
41{
42 int line = 0;
43 int character = 0;
44};
45
47 NotAnIdentifier, // when resolving expressions like `Qt.point().x` for example, where
48 // `Qt.point()` is not an identifier
49
63 // qqmljsimportvisitor creates extra attached type scopes for `Type.property: ...` compared to
64 // other usages of attached types, like for example `p: Type.property`
69};
70
72{
73 int code = 0;
75};
76
83
85{
86public:
87 Location() = default;
88 Location(const QString &filename, const QQmlJS::SourceLocation &sourceLocation,
89 const TextPosition &end)
91 {
92 }
93
94 QString filename() const { return m_filename; }
95 QQmlJS::SourceLocation sourceLocation() const { return m_sourceLocation; }
96 TextPosition end() const { return m_end; }
97
98 bool isInSourceFolder(const QQmlJSResourceFileMapper *mapper) const
99 {
100 return QQmlJSUtils::qmlPathInMapper(mapper, m_filename);
101 }
102
103 static Location from(const QString &fileName, const QString &code, qsizetype startLine,
104 qsizetype startCharacter, qsizetype length);
105 static Location from(const QString &fileName, const QQmlJS::SourceLocation &sourceLocation,
106 const QString &code);
107 static std::optional<Location> tryFrom(const QString &fileName,
108 const QQmlJS::SourceLocation &sourceLocation,
109 const QQmlJS::Dom::DomItem &someItem);
110
111 friend bool operator<(const Location &a, const Location &b)
112 {
113 return std::make_tuple(a.m_filename, a.m_sourceLocation.begin(), a.m_sourceLocation.end())
114 < std::make_tuple(b.m_filename, b.m_sourceLocation.begin(),
115 b.m_sourceLocation.end());
116 }
117 friend bool operator==(const Location &a, const Location &b)
118 {
119 return std::make_tuple(a.m_filename, a.m_sourceLocation.begin(), a.m_sourceLocation.end())
120 == std::make_tuple(b.m_filename, b.m_sourceLocation.begin(),
121 b.m_sourceLocation.end());
122 }
123
124private:
125 QString m_filename;
126 QQmlJS::SourceLocation m_sourceLocation;
127 TextPosition m_end;
128};
129
130/*!
131Represents a rename operation where the file itself needs to be renamed.
132\internal
133*/
135{
138
139 friend bool comparesEqual(const FileRename &a, const FileRename &b) noexcept
140 {
141 return std::tie(a.oldFilename, a.newFilename) == std::tie(b.oldFilename, b.newFilename);
142 }
143 friend Qt::strong_ordering compareThreeWay(const FileRename &a, const FileRename &b) noexcept
144 {
145 if (a.oldFilename != b.oldFilename)
146 return compareThreeWay(a.oldFilename, b.oldFilename);
147 return compareThreeWay(a.newFilename, b.newFilename);
148 }
150};
151
152struct Edit
153{
156
157 static Edit from(const QString &fileName, const QString &code, qsizetype startLine,
158 qsizetype startCharacter, qsizetype length, const QString &newName);
159
160 friend bool operator<(const Edit &a, const Edit &b)
161 {
162 return std::make_tuple(a.location, a.replacement)
163 < std::make_tuple(b.location, b.replacement);
164 }
165 friend bool operator==(const Edit &a, const Edit &b)
166 {
167 return std::make_tuple(a.location, a.replacement)
168 == std::make_tuple(b.location, b.replacement);
169 }
170};
171
172/*!
173Represents the locations where some highlighting should take place, like in the "find all
174references" feature of the LSP. Those locations are pointing to parts of a Qml file or to a Qml
175file name.
176
177The file names are not reported as usage to the LSP and are currently only needed for the renaming
178operation to be able to rename files.
179
180\internal
181*/
183{
184public:
185 void sort();
186 bool isEmpty() const;
187
188 friend bool comparesEqual(const Usages &a, const Usages &b) noexcept
189 {
190 return a.m_usagesInFile == b.m_usagesInFile && a.m_usagesInFilename == b.m_usagesInFilename;
191 }
193
194 Usages() = default;
195 Usages(const QList<Location> &usageInFile, const QList<QString> &usageInFilename);
196
197 QList<Location> usagesInFile() const { return m_usagesInFile; };
198 QList<QString> usagesInFilename() const { return m_usagesInFilename; };
199
200 void appendUsage(const Location &edit)
201 {
202 if (!m_usagesInFile.contains(edit))
203 m_usagesInFile.append(edit);
204 };
205 void appendFilenameUsage(const QString &edit)
206 {
207
208 if (!m_usagesInFilename.contains(edit))
209 m_usagesInFilename.append(edit);
210 };
211
212private:
213 QList<Location> m_usagesInFile;
214 QList<QString> m_usagesInFilename;
215};
216
217/*!
218Represents the locations where a renaming should take place. Parts of text inside a file can be
219renamed and also filename themselves can be renamed.
220
221\internal
222*/
224{
225public:
226 friend bool comparesEqual(const RenameUsages &a, const RenameUsages &b) noexcept
227 {
228 return std::tie(a.m_renamesInFile, a.m_renamesInFilename)
229 == std::tie(b.m_renamesInFile, b.m_renamesInFilename);
230 }
232
233 RenameUsages() = default;
234 RenameUsages(const QList<Edit> &renamesInFile, const QList<FileRename> &renamesInFilename);
235
236 QList<Edit> renameInFile() const { return m_renamesInFile; };
237 QList<FileRename> renameInFilename() const { return m_renamesInFilename; };
238
239 void appendRename(const Edit &edit) { m_renamesInFile.append(edit); };
240 void appendRename(const FileRename &edit) { m_renamesInFilename.append(edit); };
241
242private:
243 QList<Edit> m_renamesInFile;
244 QList<FileRename> m_renamesInFilename;
245};
246
247/*!
248 \internal
249 Choose whether to resolve the owner type or the entire type (the latter is only required to
250 resolve the types of qualified names and property accesses).
251
252 For properties, methods, enums and co:
253 * ResolveOwnerType returns the base type of the owner that owns the property, method, enum
254 and co. For example, resolving "x" in "myRectangle.x" will return the Item as the owner, as
255 Item is the base type of Rectangle that defines the "x" property.
256 * ResolveActualTypeForFieldMemberExpression is used to resolve field member expressions, and
257 might lose some information about the owner. For example, resolving "x" in "myRectangle.x"
258 will return the JS type for float that was used to define the "x" property.
259 */
264
266
267qsizetype textOffsetFrom(const QString &code, int row, int character);
268TextPosition textRowAndColumnFrom(const QString &code, qsizetype offset);
269QList<ItemLocation> itemsFromTextLocation(const DomItem &file, int line, int character);
270DomItem sourceLocationToDomItem(const DomItem &file, const QQmlJS::SourceLocation &location);
271QByteArray lspUriToQmlUrl(const QByteArray &uri);
272QByteArray qmlUrlToLspUri(const QByteArray &url);
274std::optional<Location> findTypeDefinitionOf(const DomItem &item);
275QList<Location> findDefinitionOf(const DomItem &item, const QStringList &headerDirectories);
276Usages findUsagesOf(const DomItem &item);
277
278std::optional<ErrorMessage>
279checkNameForRename(const DomItem &item, const QString &newName,
280 const std::optional<ExpressionType> &targetType = std::nullopt);
281RenameUsages renameUsagesOf(const DomItem &item, const QString &newName,
282 const std::optional<ExpressionType> &targetType = std::nullopt);
283std::optional<ExpressionType> resolveExpressionType(const DomItem &item, ResolveOptions);
284bool isValidEcmaScriptIdentifier(QStringView view);
285
286std::pair<QString, QStringList> cmakeBuildCommand(const QString &path, int cmakeJobs = 0);
287
288bool isFieldMemberExpression(const DomItem &item);
289bool isFieldMemberAccess(const DomItem &item);
290bool isFieldMemberBase(const DomItem &item);
291QStringList fieldMemberExpressionBits(const DomItem &item, const DomItem &stopAtChild = {});
292
293QString qualifiersFrom(const DomItem &el);
294
295QQmlJSScope::ConstPtr findDefiningScopeForProperty(const QQmlJSScope::ConstPtr &referrerScope,
296 const QString &nameToCheck);
297QQmlJSScope::ConstPtr findDefiningScopeForBinding(const QQmlJSScope::ConstPtr &referrerScope,
298 const QString &nameToCheck);
299QQmlJSScope::ConstPtr findDefiningScopeForMethod(const QQmlJSScope::ConstPtr &referrerScope,
300 const QString &nameToCheck);
301QQmlJSScope::ConstPtr findDefiningScopeForEnumeration(const QQmlJSScope::ConstPtr &referrerScope,
302 const QString &nameToCheck);
303QQmlJSScope::ConstPtr findDefiningScopeForEnumerationKey(const QQmlJSScope::ConstPtr &referrerScope,
304 const QString &nameToCheck);
305QStringList findFilePathsFromFileNames(const QString &rootDir, const QStringList &fileNamesToSearch,
306 const QSet<QString> &ignoredPaths);
307QString findFilePathFromFileName(const QStringList &rootDirs, const QString &fileNameToSearch,
308 const QSet<QString> &ignoredPaths);
309} // namespace QQmlLSUtils
310
311QT_END_NAMESPACE
312
313#endif // QLANGUAGESERVERUTILS_P_H
DocumentSymbolVisitor(const DomItem &item, const AssemblingFunction af)
friend bool operator<(const Location &a, const Location &b)
bool isInSourceFolder(const QQmlJSResourceFileMapper *mapper) const
static std::optional< Location > tryFrom(const QString &fileName, const QQmlJS::SourceLocation &sourceLocation, const QQmlJS::Dom::DomItem &someItem)
static Location from(const QString &fileName, const QQmlJS::SourceLocation &sourceLocation, const QString &code)
QString filename() const
TextPosition end() const
QQmlJS::SourceLocation sourceLocation() const
friend bool operator==(const Location &a, const Location &b)
Location(const QString &filename, const QQmlJS::SourceLocation &sourceLocation, const TextPosition &end)
static Location from(const QString &fileName, const QString &code, qsizetype startLine, qsizetype startCharacter, qsizetype length)
Represents the locations where a renaming should take place.
friend bool comparesEqual(const RenameUsages &a, const RenameUsages &b) noexcept
void appendRename(const Edit &edit)
RenameUsages(const QList< Edit > &renamesInFile, const QList< FileRename > &renamesInFilename)
QList< Edit > renameInFile() const
void appendRename(const FileRename &edit)
QList< FileRename > renameInFilename() const
Represents the locations where some highlighting should take place, like in the "find allreferences" ...
void appendFilenameUsage(const QString &edit)
QList< Location > usagesInFile() const
QList< QString > usagesInFilename() const
friend bool comparesEqual(const Usages &a, const Usages &b) noexcept
void appendUsage(const Location &edit)
Usages(const QList< Location > &usageInFile, const QList< QString > &usageInFilename)
static constexpr std::array< TypeSymbolRelation, 9 > s_TypeSymbolRelations
std::pair< QLspSpecification::Range, QLspSpecification::Range > symbolRangesOf(const DomItem &item)
static bool shouldFilterOut(const DomItem &item)
QLspSpecification::DocumentSymbol & MutableRefToDocumentSymbol
SymbolsList assembleSymbolsForQmlFile(const DomItem &item, const AssemblingFunction af)
static void adopt(QLspSpecification::DocumentSymbol &&child, MutableRefToDocumentSymbol parent)
static std::optional< QByteArray > tryGetQmlObjectDetail(const DomItem &qmlObj)
static void readoptChildrenIf(const DocumentSymbolPredicate unaryPred, MutableRefToDocumentSymbol currentParent)
std::optional< QByteArray > tryGetDetailOf(const DomItem &item)
static SymbolsList extractChildrenIf(const DocumentSymbolPredicate shouldBeReadopted, MutableRefToDocumentSymbol currentParent)
static bool propertyBoundAtDefinitionLine(const DomItem &propertyDefinition)
static constexpr bool documentSymbolNotSupportedFor(const DomType &type)
static QByteArray getMethodDetail(const DomItem &mItem)
static MutableRefToDocumentSymbol findDirectParentFor(const QLspSpecification::DocumentSymbol &child, MutableRefToDocumentSymbol currentParent)
void reorganizeForOutlineView(SymbolsList &qmlFileSymbols)
SymbolsList buildSymbolOrReturnChildren(const DomItem &item, SymbolsList &&children)
qxp::function_ref< bool(const QLspSpecification::DocumentSymbol &) const > DocumentSymbolPredicate
static constexpr SymbolKind symbolKindFor(const DomType &type)
static bool isSubRange(const QLspSpecification::Range &potentialSubRange, const QLspSpecification::Range &range)
static std::optional< QByteArray > tryGetBindingDetail(const DomItem &bItem)
static void reorganizeQmlComponentSymbol(MutableRefToDocumentSymbol qmlCompSymbol)
QLspSpecification::SymbolKind symbolKindOf(const DomItem &item)
QByteArray symbolNameOf(const DomItem &item)
RenameUsages renameUsagesOf(const DomItem &item, const QString &newName, const std::optional< ExpressionType > &targetType=std::nullopt)
Rename the appearance of item to newName.
QQmlJSScope::ConstPtr findDefiningScopeForProperty(const QQmlJSScope::ConstPtr &referrerScope, const QString &nameToCheck)
Finds the scope where a property is first defined.
bool isFieldMemberBase(const DomItem &item)
QStringList findFilePathsFromFileNames(const QString &rootDir, const QStringList &fileNamesToSearch, const QSet< QString > &ignoredPaths)
@ EnumeratorValueIdentifier
@ GroupedPropertyIdentifier
@ AttachedTypeIdentifierInBindingTarget
@ QualifiedModuleIdentifier
@ PropertyChangedHandlerIdentifier
@ PropertyChangedSignalIdentifier
@ SignalHandlerIdentifier
bool isValidEcmaScriptIdentifier(QStringView view)
QLspSpecification::Range qmlLocationToLspLocation(Location qmlLocation)
Converts a QQmlJS::SourceLocation to a LSP Range.
QQmlJSScope::ConstPtr findDefiningScopeForMethod(const QQmlJSScope::ConstPtr &referrerScope, const QString &nameToCheck)
QQmlJSScope::ConstPtr findDefiningScopeForBinding(const QQmlJSScope::ConstPtr &referrerScope, const QString &nameToCheck)
qsizetype textOffsetFrom(const QString &code, int row, int character)
Convert a text position from (line, column) into an offset.
QByteArray lspUriToQmlUrl(const QByteArray &uri)
DomItem sourceLocationToDomItem(const DomItem &file, const QQmlJS::SourceLocation &location)
QStringList fieldMemberExpressionBits(const DomItem &item, const DomItem &stopAtChild={})
QByteArray qmlUrlToLspUri(const QByteArray &url)
@ ResolveActualTypeForFieldMemberExpression
TextPosition textRowAndColumnFrom(const QString &code, qsizetype offset)
Convert a text position from an offset into (line, column).
QList< Location > findDefinitionOf(const DomItem &item, const QStringList &headerDirectories)
std::optional< ExpressionType > resolveExpressionType(const DomItem &item, ResolveOptions)
Resolves the type of the given DomItem, when possible (e.g., when there are enough type annotations).
bool isFieldMemberExpression(const DomItem &item)
bool isFieldMemberAccess(const DomItem &item)
QQmlJSScope::ConstPtr findDefiningScopeForEnumeration(const QQmlJSScope::ConstPtr &referrerScope, const QString &nameToCheck)
QList< ItemLocation > itemsFromTextLocation(const DomItem &file, int line, int character)
Find the DomItem representing the object situated in file at given line and character/column.
QString qualifiersFrom(const DomItem &el)
QQmlJSScope::ConstPtr findDefiningScopeForEnumerationKey(const QQmlJSScope::ConstPtr &referrerScope, const QString &nameToCheck)
Usages findUsagesOf(const DomItem &item)
std::optional< Location > findTypeDefinitionOf(const DomItem &item)
Returns the location of the type definition pointed by object.
QString findFilePathFromFileName(const QStringList &rootDirs, const QString &fileNameToSearch, const QSet< QString > &ignoredPaths)
std::optional< ErrorMessage > checkNameForRename(const DomItem &item, const QString &newName, const std::optional< ExpressionType > &targetType=std::nullopt)
std::pair< QString, QStringList > cmakeBuildCommand(const QString &path, int cmakeJobs=0)
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcQIORing)
static Edit from(const QString &fileName, const QString &code, qsizetype startLine, qsizetype startCharacter, qsizetype length, const QString &newName)
friend bool operator<(const Edit &a, const Edit &b)
friend bool operator==(const Edit &a, const Edit &b)
QQmlJSScope::ConstPtr semanticScope
std::optional< QString > name
Represents a rename operation where the file itself needs to be renamed.
friend bool comparesEqual(const FileRename &a, const FileRename &b) noexcept
friend Qt::strong_ordering compareThreeWay(const FileRename &a, const FileRename &b) noexcept
QQmlJS::Dom::DomItem domItem
QQmlJS::Dom::FileLocations::Tree fileLocation