[comments] Import mpark/variant and make MarkedString a variant (#200)

This commit is contained in:
Fangrui Song 2017-12-28 15:21:40 -08:00 committed by GitHub
parent 34052fbf27
commit 6636617b4d
3 changed files with 21 additions and 12 deletions

View File

@ -5,7 +5,8 @@
#include "serializer.h"
#include "utils.h"
#include <optional.h>
#include "optional.h"
#include "variant.h"
#include <rapidjson/writer.h>
#include <algorithm>
@ -982,11 +983,12 @@ MAKE_REFLECT_STRUCT(Out_TextDocumentPublishDiagnostics::Params,
//
// Note that markdown strings will be sanitized - that means html will be
// escaped.
struct lsMarkedString {
struct lsMarkedString1 {
std::string language;
std::string value;
};
MAKE_REFLECT_STRUCT(lsMarkedString, language, value);
using lsMarkedString = std::variant<std::string, lsMarkedString1>;
MAKE_REFLECT_STRUCT(lsMarkedString1, language, value);
struct lsTextDocumentContentChangeEvent {
// The range of the document that changed.

View File

@ -98,12 +98,11 @@ struct TextDocumentHoverHandler : BaseMessageHandler<Ipc_TextDocumentHover> {
if (comments_hover.first || comments_hover.second.size()) {
out.result = Out_TextDocumentHover::Result();
if (comments_hover.first) {
out.result->contents.emplace_back(
lsMarkedString{"text", *comments_hover.first});
out.result->contents.emplace_back(*comments_hover.first);
}
if (comments_hover.second.size()) {
out.result->contents.emplace_back(
lsMarkedString{file->def->language, comments_hover.second});
lsMarkedString1{file->def->language, comments_hover.second});
}
out.result->range = *ls_range;
break;

View File

@ -1,14 +1,15 @@
#pragma once
#include <macro_map.h>
#include <optional.h>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <memory>
#include <string>
#include <vector>
#include "macro_map.h"
#include "optional.h"
#include "variant.h"
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
using std::experimental::nullopt;
using std::experimental::optional;
@ -112,10 +113,17 @@ void Reflect(Writer& visitor, std::vector<T>& values) {
visitor.EndArray();
}
template <typename T>
void Reflect(Writer& visitor, optional<T> value) {
void Reflect(Writer& visitor, optional<T>& value) {
if (value)
Reflect(visitor, value.value());
}
template <typename T0, typename T1>
void Reflect(Writer& visitor, std::variant<T0, T1>& value) {
if (value.index() == 0)
Reflect(visitor, std::get<0>(value));
else
Reflect(visitor, std::get<1>(value));
}
inline void DefaultReflectMemberStart(Writer& visitor) {
visitor.StartObject();
}