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
4#ifndef QQMLCODEMODEL_P_H
5#define QQMLCODEMODEL_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 "qlanguageserver_p.h"
19#include "qtextdocument_p.h"
20
21#include <QObject>
22#include <QHash>
23#include <QtQmlDom/private/qqmldomitem_p.h>
24#include <QtQmlCompiler/private/qqmljsscope_p.h>
25#include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h>
26
27#include <functional>
28#include <memory>
29
30QT_BEGIN_NAMESPACE
31class TextSynchronization;
32namespace QmlLsp {
33
34class OpenDocumentSnapshot
35{
36public:
37 enum class DumpOption {
38 NoCode = 0,
39 LatestCode = 0x1,
40 ValidCode = 0x2,
41 AllCode = LatestCode | ValidCode
42 };
43 Q_DECLARE_FLAGS(DumpOptions, DumpOption)
44 QStringList searchPath;
45 QByteArray url;
46 std::optional<int> docVersion;
47 QQmlJS::Dom::DomItem doc;
48 std::optional<int> validDocVersion;
49 QQmlJS::Dom::DomItem validDoc;
50 std::optional<int> scopeVersion;
51 QDateTime scopeDependenciesLoadTime;
52 bool scopeDependenciesChanged = false;
53 QQmlJSScope::Ptr scope = {};
54 QDebug dump(QDebug dbg, DumpOptions dump = DumpOption::NoCode);
55};
56
57Q_DECLARE_OPERATORS_FOR_FLAGS(OpenDocumentSnapshot::DumpOptions)
58
59class OpenDocument
60{
61public:
62 OpenDocumentSnapshot snapshot;
63 std::shared_ptr<Utils::TextDocument> textDocument;
64};
65
66struct ToIndex
67{
68 QString path;
69 int leftDepth;
70};
71
72class QQmlCodeModel : public QObject
73{
74 Q_OBJECT
75public:
76 enum class UrlLookup { Caching, ForceLookup };
77 enum class State { Running, Stopping };
78
79 explicit QQmlCodeModel(QObject *parent = nullptr, QQmlToolingSettings *settings = nullptr);
80 ~QQmlCodeModel();
81 QQmlJS::Dom::DomItem currentEnv();
82 QQmlJS::Dom::DomItem validEnv();
83 OpenDocumentSnapshot snapshotByUrl(const QByteArray &url);
84 OpenDocument openDocumentByUrl(const QByteArray &url);
85
86 void openNeedUpdate();
87 void indexNeedsUpdate();
88 void addDirectoriesToIndex(const QStringList &paths, QLanguageServer *server);
89 void addOpenToUpdate(const QByteArray &);
90 void removeDirectory(const QString &path);
91 // void updateDocument(const OpenDocument &doc);
92 QString url2Path(const QByteArray &url, UrlLookup options = UrlLookup::Caching);
93 void newOpenFile(const QByteArray &url, int version, const QString &docText);
94 void newDocForOpenFile(const QByteArray &url, int version, const QString &docText);
95 void closeOpenFile(const QByteArray &url);
96 void setRootUrls(const QList<QByteArray> &urls);
97 QList<QByteArray> rootUrls() const;
98 void addRootUrls(const QList<QByteArray> &urls);
99 QStringList buildPathsForRootUrl(const QByteArray &url);
100 QStringList buildPathsForFileUrl(const QByteArray &url);
101 void setBuildPathsForRootUrl(QByteArray url, const QStringList &paths);
102 void removeRootUrls(const QList<QByteArray> &urls);
103 QQmlToolingSettings *settings();
104Q_SIGNALS:
105 void updatedSnapshot(const QByteArray &url);
106private:
107 void indexDirectory(const QString &path, int depthLeft);
108 int indexEvalProgress() const; // to be called in the mutex
109 void indexStart(); // to be called in the mutex
110 void indexEnd(); // to be called in the mutex
111 void indexSendProgress(int progress);
112 bool indexCancelled();
113 bool indexSome();
114 void addDirectory(const QString &path, int leftDepth);
115 bool openUpdateSome();
116 void openUpdateStart();
117 void openUpdateEnd();
118 void openUpdate(const QByteArray &);
119 mutable QMutex m_mutex;
120 State m_state = State::Running;
121 int m_lastIndexProgress = 0;
122 int m_nIndexInProgress = 0;
123 QList<ToIndex> m_toIndex;
124 int m_indexInProgressCost = 0;
125 int m_indexDoneCost = 0;
126 int m_nUpdateInProgress = 0;
127 QQmlJS::Dom::DomItem m_currentEnv;
128 QQmlJS::Dom::DomItem m_validEnv;
129 QByteArray m_lastOpenDocumentUpdated;
130 QSet<QByteArray> m_openDocumentsToUpdate;
131 QHash<QByteArray, QStringList> m_buildPathsForRootUrl;
132 QList<QByteArray> m_rootUrls;
133 QHash<QByteArray, QString> m_url2path;
134 QHash<QString, QByteArray> m_path2url;
135 QHash<QByteArray, OpenDocument> m_openDocuments;
136 QQmlToolingSettings *m_settings;
137};
138
139} // namespace QmlLsp
140QT_END_NAMESPACE
141#endif // QQMLCODEMODEL_P_H
142

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