| 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 QLANGUAGESERVER_P_H |
| 5 | #define QLANGUAGESERVER_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/qlanguageserverspec_p.h> |
| 19 | #include <QtLanguageServer/private/qlanguageserverprotocol_p.h> |
| 20 | #include <QtLanguageServer/private/qlspnotifysignals_p.h> |
| 21 | #include <QtCore/qloggingcategory.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QLanguageServer; |
| 26 | class QLanguageServerPrivate; |
| 27 | Q_DECLARE_LOGGING_CATEGORY(lspServerLog) |
| 28 | |
| 29 | class QLanguageServerModule : public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | public: |
| 33 | QLanguageServerModule(QObject *parent = nullptr) : QObject(parent) { } |
| 34 | virtual QString name() const = 0; |
| 35 | virtual void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) = 0; |
| 36 | virtual void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, |
| 37 | QLspSpecification::InitializeResult &) = 0; |
| 38 | }; |
| 39 | |
| 40 | class QLanguageServer : public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | Q_PROPERTY(RunStatus runStatus READ runStatus NOTIFY runStatusChanged) |
| 44 | Q_PROPERTY(bool isInitialized READ isInitialized) |
| 45 | public: |
| 46 | QLanguageServer(const QJsonRpcTransport::DataHandler &h, QObject *parent = nullptr); |
| 47 | enum class RunStatus { |
| 48 | NotSetup, |
| 49 | SettingUp, |
| 50 | DidSetup, |
| 51 | Initializing, |
| 52 | DidInitialize, // normal state of execution |
| 53 | WaitPending, |
| 54 | Stopping, |
| 55 | WaitingForExit, |
| 56 | Stopped |
| 57 | }; |
| 58 | Q_ENUM(RunStatus) |
| 59 | |
| 60 | QLanguageServerProtocol *protocol(); |
| 61 | void finishSetup(); |
| 62 | void registerHandlers(QLanguageServerProtocol *protocol); |
| 63 | void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, |
| 64 | QLspSpecification::InitializeResult &serverInfo); |
| 65 | void addServerModule(QLanguageServerModule *serverModule); |
| 66 | QLanguageServerModule *moduleByName(const QString &n) const; |
| 67 | QLspNotifySignals *notifySignals(); |
| 68 | |
| 69 | // API |
| 70 | RunStatus runStatus() const; |
| 71 | bool isInitialized() const; |
| 72 | bool isRequestCanceled(const QJsonRpc::IdType &id) const; |
| 73 | const QLspSpecification::InitializeParams &clientInfo() const; |
| 74 | const QLspSpecification::InitializeResult &serverInfo() const; |
| 75 | |
| 76 | public Q_SLOTS: |
| 77 | void receiveData(const QByteArray &d, bool isEndOfMessage); |
| 78 | Q_SIGNALS: |
| 79 | void runStatusChanged(RunStatus); |
| 80 | void clientInitialized(QLanguageServer *server); |
| 81 | void shutdown(); |
| 82 | void exit(); |
| 83 | void lifecycleError(); |
| 84 | void readNextMessage(); |
| 85 | |
| 86 | private: |
| 87 | void registerMethods(QJsonRpc::TypedRpc &typedRpc); |
| 88 | void executeShutdown(); |
| 89 | Q_DECLARE_PRIVATE(QLanguageServer) |
| 90 | }; |
| 91 | |
| 92 | QT_END_NAMESPACE |
| 93 | |
| 94 | #endif // QLANGUAGESERVER_P_H |
| 95 | |