/**************************************************************************** ** ** 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 #include #include #include #include #include #include #include namespace LanguageServerProtocol { LANGUAGESERVERPROTOCOL_EXPORT Q_DECLARE_LOGGING_CATEGORY(conversionLog) template 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(const QJsonValue &value); template<> LANGUAGESERVERPROTOCOL_EXPORT int fromJsonValue(const QJsonValue &value); template<> LANGUAGESERVERPROTOCOL_EXPORT double fromJsonValue(const QJsonValue &value); template<> LANGUAGESERVERPROTOCOL_EXPORT bool fromJsonValue(const QJsonValue &value); template<> LANGUAGESERVERPROTOCOL_EXPORT QJsonArray fromJsonValue(const QJsonValue &value); template class LanguageClientArray : public Utils::variant, std::nullptr_t> { public: using Utils::variant, std::nullptr_t>::variant; using Utils::variant, std::nullptr_t>::operator=; LanguageClientArray() {} LanguageClientArray(const QList &list) { *this = list; } LanguageClientArray(const QJsonValue &value) { if (value.isArray()) { QList values; values.reserve(value.toArray().count()); for (auto arrayValue : value.toArray()) values << fromJsonValue(arrayValue); *this = values; } else { *this = nullptr; } } QJsonValue toJson() const { if (const auto list = Utils::get_if>(this)) { QJsonArray array; for (const T &value : *list) array.append(QJsonValue(value)); return array; } return QJsonValue(); } QList toList() const { QTC_ASSERT(Utils::holds_alternative>(*this), return {}); return Utils::get>(*this); } bool isNull() const { return Utils::holds_alternative(*this); } }; template class LanguageClientValue : public Utils::variant { public: using Utils::variant::operator=; LanguageClientValue() : Utils::variant(nullptr) { } LanguageClientValue(const T &value) : Utils::variant(value) { } LanguageClientValue(const QJsonValue &value) { if (QTC_GUARD(value.isUndefined()) || value.isNull()) *this = nullptr; else *this = fromJsonValue(value); } operator const QJsonValue() const { if (auto val = Utils::get_if(this)) return QJsonValue(*val); return QJsonValue(); } T value(const T &defaultValue = T()) const { QTC_ASSERT(Utils::holds_alternative(*this), return defaultValue); return Utils::get(*this); } template LanguageClientValue transform() { QTC_ASSERT(!Utils::holds_alternative(*this), return LanguageClientValue()); return Type(Utils::get(*this)); } bool isNull() const { return Utils::holds_alternative(*this); } }; template QJsonArray enumArrayToJsonArray(const QList &values) { QJsonArray array; for (T value : values) array.append(static_cast(value)); return array; } template QList jsonArrayToList(const QJsonArray &array) { QList list; for (const QJsonValue &val : array) list << fromJsonValue(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 m_children; QString m_error; }; } // namespace LanguageClient