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
4#ifndef QLANGUAGESERVERUTILS_P_H
5#define QLANGUAGESERVERUTILS_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 <QtLanguageServer/private/qlanguageserverspectypes_p.h>
19#include <QtQmlDom/private/qqmldomexternalitems_p.h>
20#include <QtQmlDom/private/qqmldomtop_p.h>
21#include <algorithm>
22#include <tuple>
23#include <variant>
24
25QT_BEGIN_NAMESPACE
26
27struct QQmlLSUtilsItemLocation
28{
29 QQmlJS::Dom::DomItem domItem;
30 QQmlJS::Dom::FileLocations::Tree fileLocation;
31};
32
33struct QQmlLSUtilsTextPosition
34{
35 int line;
36 int character;
37};
38
39struct QQmlLSUtilsLocation
40{
41 QString filename;
42 QQmlJS::SourceLocation location;
43
44 friend bool operator<(const QQmlLSUtilsLocation &a, const QQmlLSUtilsLocation &b)
45 {
46 return std::make_tuple(args: a.filename, args: a.location.begin(), args: a.location.end())
47 < std::make_tuple(args: b.filename, args: b.location.begin(), args: b.location.end());
48 }
49 friend bool operator==(const QQmlLSUtilsLocation &a, const QQmlLSUtilsLocation &b)
50 {
51 return std::make_tuple(args: a.filename, args: a.location.begin(), args: a.location.end())
52 == std::make_tuple(args: b.filename, args: b.location.begin(), args: b.location.end());
53 }
54};
55
56/*!
57 \internal
58 Choose whether to resolve the entire type (useful for QmlObjects, Inline Components) or just
59 the owner type (useful for properties, which are only unique given an ownerType and their
60 property name).
61 */
62enum QQmlLSUtilsResolveOptions {
63 JustOwner,
64 Everything,
65};
66
67class QQmlLSUtils
68{
69public:
70 static qsizetype textOffsetFrom(const QString &code, int row, int character);
71 static QQmlLSUtilsTextPosition textRowAndColumnFrom(const QString &code, qsizetype offset);
72 static QList<QQmlLSUtilsItemLocation> itemsFromTextLocation(QQmlJS::Dom::DomItem file, int line,
73 int character);
74 static QQmlJS::Dom::DomItem sourceLocationToDomItem(QQmlJS::Dom::DomItem file,
75 const QQmlJS::SourceLocation &location);
76 static QByteArray lspUriToQmlUrl(const QByteArray &uri);
77 static QByteArray qmlUrlToLspUri(const QByteArray &url);
78 static QLspSpecification::Range qmlLocationToLspLocation(const QString &code,
79 QQmlJS::SourceLocation qmlLocation);
80 static QQmlJS::Dom::DomItem baseObject(QQmlJS::Dom::DomItem qmlObject);
81 static QQmlJS::Dom::DomItem findTypeDefinitionOf(QQmlJS::Dom::DomItem item);
82 static std::optional<QQmlLSUtilsLocation> findDefinitionOf(QQmlJS::Dom::DomItem item);
83 static QList<QQmlLSUtilsLocation> findUsagesOf(QQmlJS::Dom::DomItem item);
84
85 static QQmlJSScope::ConstPtr resolveExpressionType(QQmlJS::Dom::DomItem item,
86 QQmlLSUtilsResolveOptions);
87};
88QT_END_NAMESPACE
89
90#endif // QLANGUAGESERVERUTILS_P_H
91

source code of qtdeclarative/src/qmlls/qqmllsutils_p.h