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#ifndef QLANGUAGESERVERPRESPECTYPES_P_H
4#define QLANGUAGESERVERPRESPECTYPES_P_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include <QtJsonRpc/private/qtypedjson_p.h>
18#include <QtJsonRpc/private/qjsontypedrpc_p.h>
19#include <QtLanguageServer/qtlanguageserverglobal.h>
20#include <QtCore/QByteArray>
21#include <QtCore/QMetaEnum>
22#include <QtCore/QString>
23
24#include <variant>
25#include <type_traits>
26
27QT_BEGIN_NAMESPACE
28namespace QLspSpecification {
29
30using ProgressToken = std::variant<int, QByteArray>;
31
32class Q_LANGUAGESERVER_EXPORT StringAndLanguage
33{
34public:
35 QString language;
36 QString value;
37
38 template<typename W>
39 void walk(W &w)
40 {
41 field(w, "language", language);
42 field(w, "value", value);
43 }
44};
45
46using MarkedString = std::variant<QByteArray, StringAndLanguage>;
47
48template<typename RType>
49class LSPResponse : public QJsonRpc::TypedResponse
50{
51 Q_DISABLE_COPY(LSPResponse)
52public:
53 LSPResponse() = default;
54 LSPResponse(LSPResponse &&o) noexcept = default;
55 LSPResponse &operator=(LSPResponse &&o) noexcept = default;
56 using ResponseType = RType;
57
58 LSPResponse(QJsonRpc::TypedResponse &&r) : QJsonRpc::TypedResponse(std::move(r)) { }
59
60 void sendResponse(const RType &r) { sendSuccessfullResponse(r); }
61 auto sendResponse()
62 {
63 if constexpr (std::is_same_v<std::decay_t<RType>, std::nullptr_t>)
64 sendSuccessfullResponse(nullptr);
65 else
66 Q_ASSERT(false);
67 }
68};
69
70template<typename RType, typename PRType>
71class LSPPartialResponse : public LSPResponse<RType>
72{
73 Q_DISABLE_COPY(LSPPartialResponse)
74public:
75 using PartialResponseType = PRType;
76
77 LSPPartialResponse() = default;
78 LSPPartialResponse(LSPPartialResponse &&o) noexcept = default;
79 LSPPartialResponse &operator=(LSPPartialResponse &&o) noexcept = default;
80
81 LSPPartialResponse(QJsonRpc::TypedResponse &&r) : LSPResponse<RType>(std::move(r)) { }
82
83 void sendPartialResponse(const PRType &r)
84 {
85 // using Notifications::Progress here would require to split out the *RequestType aliases
86 sendNotification("$/progress", r);
87 }
88};
89
90} // namespace QLspSpecification
91QT_END_NAMESPACE
92#endif // QLANGUAGESERVERPRESPECTYPES_P_H
93

source code of qtlanguageserver/src/languageserver/qlanguageserverprespectypes_p.h