aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/languageserverprotocol/lsputils.h
blob: 8da5fdd93af92487f5009ee0ab87965302ecdacc (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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "languageserverprotocol_global.h"

#include <utils/algorithm.h>
#include <utils/mimetypes/mimetype.h>
#include <utils/optional.h>
#include <utils/qtcassert.h>
#include <utils/variant.h>

#include <QJsonArray>
#include <QJsonObject>
#include <QLoggingCategory>

namespace LanguageServerProtocol {

LANGUAGESERVERPROTOCOL_EXPORT Q_DECLARE_LOGGING_CATEGORY(conversionLog)

template <typename T>
T fromJsonValue(const QJsonValue &value)
{
    if (conversionLog().isDebugEnabled() && !value.isObject())
        qCDebug(conversionLog) << "Expected Object in json value but got: " << value;
    return T(value.toObject());
}

template<>
LANGUAGESERVERPROTOCOL_EXPORT QString fromJsonValue<QString>(const QJsonValue &value);

template<>
LANGUAGESERVERPROTOCOL_EXPORT int fromJsonValue<int>(const QJsonValue &value);

template<>
LANGUAGESERVERPROTOCOL_EXPORT double fromJsonValue<double>(const QJsonValue &value);

template<>
LANGUAGESERVERPROTOCOL_EXPORT bool fromJsonValue<bool>(const QJsonValue &value);

template<>
LANGUAGESERVERPROTOCOL_EXPORT QJsonArray fromJsonValue<QJsonArray>(const QJsonValue &value);

template <typename T>
class LanguageClientArray : public Utils::variant<QList<T>, std::nullptr_t>
{
public:
    using Utils::variant<QList<T>, std::nullptr_t>::variant;
    using Utils::variant<QList<T>, std::nullptr_t>::operator=;

    LanguageClientArray() {}

    LanguageClientArray(const QList<T> &list)
    { *this = list; }

    LanguageClientArray(const QJsonValue &value)
    {
        if (value.isArray()) {
            QList<T> values;
            values.reserve(value.toArray().count());
            for (auto arrayValue : value.toArray())
                values << fromJsonValue<T>(arrayValue);
            *this = values;
        } else {
            *this = nullptr;
        }
    }

    QJsonValue toJson() const
    {
        if (const auto list = Utils::get_if<QList<T>>(this)) {
            QJsonArray array;
            for (const T &value : *list)
                array.append(QJsonValue(value));
            return array;
        }
        return QJsonValue();
    }

    QList<T> toList() const
    {
        QTC_ASSERT(Utils::holds_alternative<QList<T>>(*this), return {});
        return Utils::get<QList<T>>(*this);
    }
    bool isNull() const { return Utils::holds_alternative<std::nullptr_t>(*this); }
};

template <typename T>
class LanguageClientValue : public Utils::variant<T, std::nullptr_t>
{
public:
    using Utils::variant<T, std::nullptr_t>::operator=;

    LanguageClientValue() : Utils::variant<T, std::nullptr_t>(nullptr) { }
    LanguageClientValue(const T &value) : Utils::variant<T, std::nullptr_t>(value) { }
    LanguageClientValue(const QJsonValue &value)
    {
        if (QTC_GUARD(value.isUndefined()) || value.isNull())
            *this = nullptr;
        else
            *this = fromJsonValue<T>(value);
    }

    operator const QJsonValue() const
    {
        if (auto val = Utils::get_if<T>(this))
            return QJsonValue(*val);
        return QJsonValue();
    }

    T value(const T &defaultValue = T()) const
    {
        QTC_ASSERT(Utils::holds_alternative<T>(*this), return defaultValue);
        return Utils::get<T>(*this);
    }

    template<typename Type>
    LanguageClientValue<Type> transform()
    {
        QTC_ASSERT(!Utils::holds_alternative<T>(*this), return LanguageClientValue<Type>());
        return Type(Utils::get<T>(*this));
    }

    bool isNull() const { return Utils::holds_alternative<std::nullptr_t>(*this); }
};

template <typename T>
QJsonArray enumArrayToJsonArray(const QList<T> &values)
{
    QJsonArray array;
    for (T value : values)
        array.append(static_cast<int>(value));
    return array;
}

template <typename T>
QList<T> jsonArrayToList(const QJsonArray &array)
{
    QList<T> list;
    for (const QJsonValue &val : array)
        list << fromJsonValue<T>(val);
    return list;
}

class LANGUAGESERVERPROTOCOL_EXPORT ErrorHierarchy
{
public:
    ErrorHierarchy() = default;

    void setError(const QString &error) { m_error = error; }
    void prependMember(const QString &member) { m_hierarchy.prepend(member); }
    void addVariantHierachy(const ErrorHierarchy &subError) { m_children.append(subError); }
    void clear();

    bool isEmpty() const;
    QString toString() const;

    bool operator==(const ErrorHierarchy &other) const;
private:
    QStringList m_hierarchy;
    QList<ErrorHierarchy> m_children;
    QString m_error;
};

} // namespace LanguageClient