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 <QtCore/qfilesystemwatcher.h> |
24 | #include <QtCore/private/qfactoryloader_p.h> |
25 | #include <QtQmlDom/private/qqmldomitem_p.h> |
26 | #include <QtQmlCompiler/private/qqmljsscope_p.h> |
27 | #include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h> |
28 | |
29 | #include <functional> |
30 | #include <memory> |
31 | |
32 | QT_BEGIN_NAMESPACE |
33 | class TextSynchronization; |
34 | namespace QmlLsp { |
35 | |
36 | class OpenDocumentSnapshot |
37 | { |
38 | public: |
39 | enum class DumpOption { |
40 | NoCode = 0, |
41 | LatestCode = 0x1, |
42 | ValidCode = 0x2, |
43 | AllCode = LatestCode | ValidCode |
44 | }; |
45 | Q_DECLARE_FLAGS(DumpOptions, DumpOption) |
46 | QStringList searchPath; |
47 | QByteArray url; |
48 | std::optional<int> docVersion; |
49 | QQmlJS::Dom::DomItem doc; |
50 | std::optional<int> validDocVersion; |
51 | QQmlJS::Dom::DomItem validDoc; |
52 | std::optional<int> scopeVersion; |
53 | QDateTime scopeDependenciesLoadTime; |
54 | bool scopeDependenciesChanged = false; |
55 | QQmlJSScope::ConstPtr scope; |
56 | QDebug dump(QDebug dbg, DumpOptions dump = DumpOption::NoCode); |
57 | }; |
58 | |
59 | Q_DECLARE_OPERATORS_FOR_FLAGS(OpenDocumentSnapshot::DumpOptions) |
60 | |
61 | class OpenDocument |
62 | { |
63 | public: |
64 | OpenDocumentSnapshot snapshot; |
65 | std::shared_ptr<Utils::TextDocument> textDocument; |
66 | }; |
67 | |
68 | struct ToIndex |
69 | { |
70 | QString path; |
71 | int leftDepth; |
72 | }; |
73 | |
74 | struct RegisteredSemanticTokens |
75 | { |
76 | QByteArray resultId = "0" ; |
77 | QList<int> lastTokens; |
78 | }; |
79 | |
80 | class QQmlCodeModel : public QObject |
81 | { |
82 | Q_OBJECT |
83 | public: |
84 | enum class UrlLookup { Caching, ForceLookup }; |
85 | enum class State { Running, Stopping }; |
86 | |
87 | explicit QQmlCodeModel(QObject *parent = nullptr, QQmlToolingSettings *settings = nullptr); |
88 | ~QQmlCodeModel(); |
89 | QQmlJS::Dom::DomItem currentEnv() const { return m_currentEnv; }; |
90 | QQmlJS::Dom::DomItem validEnv() const { return m_validEnv; }; |
91 | OpenDocumentSnapshot snapshotByUrl(const QByteArray &url); |
92 | OpenDocument openDocumentByUrl(const QByteArray &url); |
93 | |
94 | void openNeedUpdate(); |
95 | void indexNeedsUpdate(); |
96 | void addDirectoriesToIndex(const QStringList &paths, QLanguageServer *server); |
97 | void addOpenToUpdate(const QByteArray &); |
98 | void removeDirectory(const QString &path); |
99 | // void updateDocument(const OpenDocument &doc); |
100 | QString url2Path(const QByteArray &url, UrlLookup options = UrlLookup::Caching); |
101 | void newOpenFile(const QByteArray &url, int version, const QString &docText); |
102 | void newDocForOpenFile(const QByteArray &url, int version, const QString &docText); |
103 | void closeOpenFile(const QByteArray &url); |
104 | void setRootUrls(const QList<QByteArray> &urls); |
105 | QList<QByteArray> rootUrls() const; |
106 | void addRootUrls(const QList<QByteArray> &urls); |
107 | QStringList buildPathsForRootUrl(const QByteArray &url); |
108 | QStringList buildPathsForFileUrl(const QByteArray &url); |
109 | void setBuildPathsForRootUrl(QByteArray url, const QStringList &paths); |
110 | QStringList importPaths() const { return m_importPaths; }; |
111 | void setImportPaths(const QStringList &paths) { m_importPaths = paths; }; |
112 | void removeRootUrls(const QList<QByteArray> &urls); |
113 | QQmlToolingSettings *settings() const { return m_settings; } |
114 | QStringList findFilePathsFromFileNames(const QStringList &fileNames); |
115 | static QStringList fileNamesToWatch(const QQmlJS::Dom::DomItem &qmlFile); |
116 | void disableCMakeCalls(); |
117 | const QFactoryLoader &pluginLoader() const { return m_pluginLoader; } |
118 | |
119 | RegisteredSemanticTokens ®isteredTokens(); |
120 | const RegisteredSemanticTokens ®isteredTokens() const; |
121 | QString documentationRootPath() const { return m_documentationRootPath; } |
122 | void setDocumentationRootPath(const QString &path); |
123 | |
124 | QSet<QString> ignoreForWatching() const { return m_ignoreForWatching; } |
125 | |
126 | Q_SIGNALS: |
127 | void updatedSnapshot(const QByteArray &url); |
128 | void documentationRootPathChanged(const QString &path); |
129 | |
130 | private: |
131 | void indexDirectory(const QString &path, int depthLeft); |
132 | int indexEvalProgress() const; // to be called in the mutex |
133 | void indexStart(); // to be called in the mutex |
134 | void indexEnd(); // to be called in the mutex |
135 | void indexSendProgress(int progress); |
136 | bool indexCancelled(); |
137 | bool indexSome(); |
138 | void addDirectory(const QString &path, int leftDepth); |
139 | bool openUpdateSome(); |
140 | void openUpdateStart(); |
141 | void openUpdateEnd(); |
142 | void openUpdate(const QByteArray &); |
143 | |
144 | static bool callCMakeBuild(const QStringList &buildPaths); |
145 | void addFileWatches(const QQmlJS::Dom::DomItem &qmlFile); |
146 | enum CMakeStatus { RequiresInitialization, HasCMake, DoesNotHaveCMake }; |
147 | void initializeCMakeStatus(const QString &); |
148 | |
149 | mutable QMutex m_mutex; |
150 | State m_state = State::Running; |
151 | int m_lastIndexProgress = 0; |
152 | int m_nIndexInProgress = 0; |
153 | QList<ToIndex> m_toIndex; |
154 | int m_indexInProgressCost = 0; |
155 | int m_indexDoneCost = 0; |
156 | int m_nUpdateInProgress = 0; |
157 | QStringList m_importPaths; |
158 | QQmlJS::Dom::DomItem m_currentEnv; |
159 | QQmlJS::Dom::DomItem m_validEnv; |
160 | QByteArray m_lastOpenDocumentUpdated; |
161 | QSet<QByteArray> m_openDocumentsToUpdate; |
162 | QHash<QByteArray, QStringList> m_buildPathsForRootUrl; |
163 | QList<QByteArray> m_rootUrls; |
164 | QHash<QByteArray, QString> m_url2path; |
165 | QHash<QString, QByteArray> m_path2url; |
166 | QHash<QByteArray, OpenDocument> m_openDocuments; |
167 | QQmlToolingSettings *m_settings; |
168 | QFileSystemWatcher m_cppFileWatcher; |
169 | QFactoryLoader m_pluginLoader; |
170 | bool m_rebuildRequired = true; // always trigger a rebuild on start |
171 | CMakeStatus m_cmakeStatus = RequiresInitialization; |
172 | RegisteredSemanticTokens m_tokens; |
173 | QString m_documentationRootPath; |
174 | QSet<QString> m_ignoreForWatching; |
175 | private slots: |
176 | void onCppFileChanged(const QString &); |
177 | }; |
178 | |
179 | } // namespace QmlLsp |
180 | QT_END_NAMESPACE |
181 | #endif // QQMLCODEMODEL_P_H |
182 | |