aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/languageserverprotocol/jsonrpcmessages.h
blob: 680bff95782fb0151f321f065b26a62d68a5f4ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "basemessage.h"
#include "jsonkeys.h"
#include "lsptypes.h"

#include <utils/qtcassert.h>

#include <QDebug>
#include <QElapsedTimer>
#include <QHash>
#include <QJsonObject>
#include <QJsonValue>
#include <QCoreApplication>
#include <QUuid>

#include <optional>
#include <variant>

namespace LanguageServerProtocol {

class JsonRpcMessage;

class LANGUAGESERVERPROTOCOL_EXPORT MessageId : public std::variant<int, QString>
{
public:
    MessageId() : variant(QString()) {}
    explicit MessageId(int id) : variant(id) {}
    explicit MessageId(const QString &id) : variant(id) {}
    explicit MessageId(const QJsonValue &value)
    {
        if (value.isDouble())
            emplace<int>(value.toInt());
        else
            emplace<QString>(value.toString());
    }

    operator QJsonValue() const
    {
        if (auto id = std::get_if<int>(this))
            return *id;
        if (auto id = std::get_if<QString>(this))
            return *id;
        return QJsonValue();
    }

    bool isValid() const
    {
        if (std::holds_alternative<int>(*this))
            return true;
        const QString *id = std::get_if<QString>(this);
        QTC_ASSERT(id, return false);
        return !id->isEmpty();
    }

    QString toString() const
    {
        if (auto id = std::get_if<QString>(this))
            return *id;
        if (auto id = std::get_if<int>(this))
            return QString::number(*id);
        return {};
    }

private:
    friend size_t qHash(const MessageId &id)
    {
        if (std::holds_alternative<int>(id))
            return QT_PREPEND_NAMESPACE(qHash(std::get<int>(id)));
        if (std::holds_alternative<QString>(id))
            return QT_PREPEND_NAMESPACE(qHash(std::get<QString>(id)));
        return QT_PREPEND_NAMESPACE(qHash(0));
    }

    friend QDebug operator<<(QDebug stream, const MessageId &id)
    {
        if (std::holds_alternative<int>(id))
            stream << std::get<int>(id);
        else
            stream << std::get<QString>(id);
        return stream;
    }
};

struct ResponseHandler
{
    MessageId id;
    using Callback = std::function<void(const JsonRpcMessage &)>;
    Callback callback;
};

class LANGUAGESERVERPROTOCOL_EXPORT JsonRpcMessage
{
public:
    JsonRpcMessage();
    explicit JsonRpcMessage(const BaseMessage &message);
    explicit JsonRpcMessage(const QJsonObject &jsonObject);
    explicit JsonRpcMessage(QJsonObject &&jsonObject);

    virtual ~JsonRpcMessage() = default;

    static QByteArray jsonRpcMimeType();

    QByteArray toRawData() const;
    virtual bool isValid(QString *errorMessage) const;

    const QJsonObject &toJsonObject() const;

    const QString parseError() { return m_parseError; }

    virtual std::optional<ResponseHandler> responseHandler() const { return std::nullopt; }

    BaseMessage toBaseMessage() const
    { return BaseMessage(jsonRpcMimeType(), toRawData()); }

protected:
    QJsonObject m_jsonObject;

private:
    QString m_parseError;
};

template <typename Params>
class Notification : public JsonRpcMessage
{
public:
    Notification(const QString &methodName, const Params &params)
    {
        setMethod(methodName);
        setParams(params);
    }

    explicit Notification(const QJsonObject &jsonObject) : JsonRpcMessage(jsonObject) {}
    explicit Notification(QJsonObject &&jsonObject) : JsonRpcMessage(std::move(jsonObject)) {}

    QString method() const
    { return fromJsonValue<QString>(m_jsonObject.value(methodKey)); }
    void setMethod(const QString &method)
    { m_jsonObject.insert(methodKey, method); }

    using Parameters = Params;
    std::optional<Params> params() const
    {
        const QJsonValue &params = m_jsonObject.value(paramsKey);
        return params.isUndefined() ? std::nullopt : std::make_optional(Params(params));
    }
    void setParams(const Params &params)
    { m_jsonObject.insert(paramsKey, QJsonValue(params)); }
    void clearParams() { m_jsonObject.remove(paramsKey); }

    bool isValid(QString *errorMessage) const override
    {
        return JsonRpcMessage::isValid(errorMessage)
                && m_jsonObject.value(methodKey).isString()
                && parametersAreValid(errorMessage);
    }

    virtual bool parametersAreValid(QString *errorMessage) const
    {
        if (auto parameter = params())
            return parameter->isValid();
        if (errorMessage)
            *errorMessage = QCoreApplication::translate("QtC::LanguageServerProtocol",
                                                        "No parameters in \"%1\".").arg(method());
        return false;
    }
};

template <>
class Notification<std::nullptr_t> : public JsonRpcMessage
{
public:
    Notification() : Notification(QString()) {}
    explicit Notification(const QString &methodName, const std::nullptr_t &/*params*/ = nullptr)
    {
        setMethod(methodName);
        setParams(nullptr);
    }
    using JsonRpcMessage::JsonRpcMessage;

    QString method() const
    { return fromJsonValue<QString>(m_jsonObject.value(methodKey)); }
    void setMethod(const QString &method)
    { m_jsonObject.insert(methodKey, method); }

    std::optional<std::nullptr_t> params() const
    { return nullptr; }
    void setParams(const std::nullptr_t &/*params*/)
    { m_jsonObject.insert(paramsKey, QJsonValue::Null); }
    void clearParams() { m_jsonObject.remove(paramsKey); }

    bool isValid(QString *errorMessage) const override
    {
        return JsonRpcMessage::isValid(errorMessage)
               && m_jsonObject.value(methodKey).isString()
               && parametersAreValid(errorMessage);
    }

    virtual bool parametersAreValid(QString * /*errorMessage*/) const
    { return true; }
};

template <typename Error>
class ResponseError : public JsonObject
{
public:
    using JsonObject::JsonObject;

    int code() const { return typedValue<int>(codeKey); }
    void setCode(int code) { insert(codeKey, code); }

    QString message() const { return typedValue<QString>(messageKey); }
    void setMessage(const QString &message) { insert(messageKey, message); }

    std::optional<Error> data() const { return optionalValue<Error>(dataKey); }
    void setData(const Error &data) { insert(dataKey, data); }
    void clearData() { remove(dataKey); }

    bool isValid() const override { return contains(codeKey) && contains(messageKey); }

    QString toString() const { return errorCodesToString(code()) + ": " + message(); }

    // predefined error codes
    enum ErrorCodes {
        // Defined by JSON RPC
        ParseError = -32700,
        InvalidRequest = -32600,
        MethodNotFound = -32601,
        InvalidParams = -32602,
        InternalError = -32603,
        serverErrorStart = -32099,
        serverErrorEnd = -32000,
        ServerNotInitialized = -32002,
        UnknownErrorCode = -32001,

        // Defined by the protocol.
        RequestCancelled = -32800
    };

#define CASE_ERRORCODES(x) case ErrorCodes::x: return QLatin1String(#x)
    static QString errorCodesToString(int code)
    {
        switch (code) {
        CASE_ERRORCODES(ParseError);
        CASE_ERRORCODES(InvalidRequest);
        CASE_ERRORCODES(MethodNotFound);
        CASE_ERRORCODES(InvalidParams);
        CASE_ERRORCODES(InternalError);
        CASE_ERRORCODES(serverErrorStart);
        CASE_ERRORCODES(serverErrorEnd);
        CASE_ERRORCODES(ServerNotInitialized);
        CASE_ERRORCODES(RequestCancelled);
        default:
            return QCoreApplication::translate("QtC::LanguageClient", "Error %1").arg(code);
        }
    }
#undef CASE_ERRORCODES
};

template <typename Result, typename ErrorDataType>
class Response : public JsonRpcMessage
{
public:
    explicit Response(const MessageId &id) { setId(id); }
    using JsonRpcMessage::JsonRpcMessage;

    MessageId id() const
    { return MessageId(m_jsonObject.value(idKey)); }
    void setId(MessageId id)
    { this->m_jsonObject.insert(idKey, id); }

    std::optional<Result> result() const
    {
        const QJsonValue &result = m_jsonObject.value(resultKey);
        if (result.isUndefined())
            return std::nullopt;
        return std::make_optional(Result(result));
    }
    void setResult(const Result &result) { m_jsonObject.insert(resultKey, QJsonValue(result)); }
    void clearResult() { m_jsonObject.remove(resultKey); }

    using Error = ResponseError<ErrorDataType>;
    std::optional<Error> error() const
    {
        const QJsonValue &val = m_jsonObject.value(errorKey);
        return val.isUndefined() ? std::nullopt : std::make_optional(fromJsonValue<Error>(val));
    }
    void setError(const Error &error)
    {
        QTC_CHECK(error.isValid());
        m_jsonObject.insert(errorKey, QJsonValue(error));
    }
    void clearError() { m_jsonObject.remove(errorKey); }

    bool isValid(QString *errorMessage) const override
    { return JsonRpcMessage::isValid(errorMessage) && id().isValid(); }
};

template<typename ErrorDataType>
class Response<std::nullptr_t, ErrorDataType> : public JsonRpcMessage
{
public:
    explicit Response(const MessageId &id) { setId(id); }
    using JsonRpcMessage::JsonRpcMessage;

    MessageId id() const
    { return MessageId(m_jsonObject.value(idKey)); }
    void setId(MessageId id)
    { this->m_jsonObject.insert(idKey, id); }

    std::optional<std::nullptr_t> result() const
    {
        return m_jsonObject.value(resultKey).isNull() ? std::make_optional(nullptr) : std::nullopt;
    }
    void setResult(const std::nullptr_t &) { m_jsonObject.insert(resultKey, QJsonValue::Null); }
    void clearResult() { m_jsonObject.remove(resultKey); }

    using Error = ResponseError<ErrorDataType>;
    std::optional<Error> error() const
    {
        const QJsonValue &val = m_jsonObject.value(errorKey);
        return val.isUndefined() ? std::nullopt : std::make_optional(fromJsonValue<Error>(val));
    }
    void setError(const Error &error)
    { m_jsonObject.insert(errorKey, QJsonValue(error)); }
    void clearError() { m_jsonObject.remove(errorKey); }

    bool isValid(QString *errorMessage) const override
    { return JsonRpcMessage::isValid(errorMessage) && id().isValid(); }
};

void LANGUAGESERVERPROTOCOL_EXPORT logElapsedTime(const QString &method, const QElapsedTimer &t);

template <typename Result, typename ErrorDataType, typename Params>
class Request : public Notification<Params>
{
public:
    Request(const QString &methodName, const Params &params)
        : Notification<Params>(methodName, params)
    { setId(MessageId(QUuid::createUuid().toString())); }
    explicit Request(const QJsonObject &jsonObject) : Notification<Params>(jsonObject) { }
    explicit Request(QJsonObject &&jsonObject) : Notification<Params>(std::move(jsonObject)) { }

    MessageId id() const
    { return MessageId(JsonRpcMessage::m_jsonObject.value(idKey)); }
    void setId(const MessageId &id)
    { JsonRpcMessage::m_jsonObject.insert(idKey, id); }

    using Response = LanguageServerProtocol::Response<Result, ErrorDataType>;
    using ResponseCallback = std::function<void(Response)>;
    void setResponseCallback(const ResponseCallback &callback)
    { m_callBack = callback; }

    std::optional<ResponseHandler> responseHandler() const final
    {
        QElapsedTimer timer;
        timer.start();
        auto callback = [callback = m_callBack, method = this->method(), t = std::move(timer)]
                (const JsonRpcMessage &message) {
            if (!callback)
                return;
            logElapsedTime(method, t);

            callback(Response(message.toJsonObject()));
        };
        return std::make_optional(ResponseHandler{id(), callback});
    }

    bool isValid(QString *errorMessage) const override
    {
        if (!Notification<Params>::isValid(errorMessage))
            return false;
        if (id().isValid())
            return true;
        if (errorMessage)
            *errorMessage = QCoreApplication::translate("QtC::LanguageServerProtocol",
                                                        "No ID set in \"%1\".").arg(this->method());
        return false;
    }

private:
    ResponseCallback m_callBack;
};

class LANGUAGESERVERPROTOCOL_EXPORT CancelParameter : public JsonObject
{
public:
    explicit CancelParameter(const MessageId &id) { setId(id); }
    CancelParameter() = default;
    using JsonObject::JsonObject;

    MessageId id() const { return typedValue<MessageId>(idKey); }
    void setId(const MessageId &id) { insert(idKey, id); }

    bool isValid() const override { return contains(idKey); }
};

class LANGUAGESERVERPROTOCOL_EXPORT CancelRequest : public Notification<CancelParameter>
{
public:
    explicit CancelRequest(const CancelParameter &params);
    using Notification::Notification;
    constexpr static const char methodName[] = "$/cancelRequest";
};

} // namespace LanguageServerProtocol

template <typename Error>
inline QDebug operator<<(QDebug stream,
                const LanguageServerProtocol::ResponseError<Error> &error)
{
    stream.nospace() << error.toString();
    return stream;
}

Q_DECLARE_METATYPE(LanguageServerProtocol::JsonRpcMessage)