This commit is contained in:
Jacob Dufault 2017-03-08 22:27:42 -08:00
parent 9b89bfc905
commit afc6db80dd

View File

@ -1,7 +1,5 @@
#pragma once #pragma once
#if false
#include <iostream> #include <iostream>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
@ -27,27 +25,28 @@ namespace language_server_api {
optional<int> id0; optional<int> id0;
optional<std::string> id1; optional<std::string> id1;
}; };
}
void Serialize(Writer& writer, const RequestId& value) { void Reflect(Writer& visitor, language_server_api::RequestId& value) {
if (value.id0) { if (value.id0) {
::Serialize(writer, value.id0.value()); Reflect(visitor, value.id0.value());
}
else {
assert(value.id1.has_value());
::Serialize(writer, value.id1.value());
}
} }
else {
void Deserialize(const Reader& reader, RequestId& id) { assert(value.id1.has_value());
if (reader.IsInt()) Reflect(visitor, value.id1.value());
id.id0 = reader.GetInt();
else if (reader.IsString())
id.id1 = std::string(reader.GetString());
else
std::cerr << "Unable to deserialize id" << std::endl;
} }
}
void Reflect(Reader& visitor, language_server_api::RequestId& id) {
if (visitor.IsInt())
Reflect(visitor, id.id0);
else if (visitor.IsString())
Reflect(visitor, id.id1);
else
std::cerr << "Unable to deserialize id" << std::endl;
}
namespace language_server_api {
struct OutMessage { struct OutMessage {
// Write out the body of the message. The writer expects object key/value // Write out the body of the message. The writer expects object key/value
@ -75,17 +74,18 @@ namespace language_server_api {
RequestId id; RequestId id;
virtual std::string Method() = 0; virtual std::string Method() = 0;
virtual void SerializeParams(Writer& writer) = 0; virtual void SerializeParams(Writer& visitor) = 0;
// Message: // Message:
void WriteMessageBody(Writer& writer) override { void WriteMessageBody(Writer& visitor) override {
auto& value = *this; auto& value = *this;
auto method = Method();
SERIALIZE_MEMBER(id); REFLECT_MEMBER(id);
SERIALIZE_MEMBER2("method", Method()); REFLECT_MEMBER2("method", method);
writer.Key("params"); visitor.Key("params");
SerializeParams(writer); SerializeParams(visitor);
} }
}; };
@ -112,17 +112,18 @@ namespace language_server_api {
std::string message; std::string message;
std::unique_ptr<Data> data; std::unique_ptr<Data> data;
void Write(Writer& writer) { void Write(Writer& visitor) {
auto& value = *this; auto& value = *this;
int code = static_cast<int>(this->code);
writer.StartObject(); visitor.StartObject();
SERIALIZE_MEMBER2("code", static_cast<int>(code)); REFLECT_MEMBER2("code", code);
SERIALIZE_MEMBER(message); REFLECT_MEMBER(message);
if (data) { if (data) {
writer.Key("data"); visitor.Key("data");
data->Write(writer); data->Write(visitor);
} }
writer.EndObject(); visitor.EndObject();
} }
}; };
@ -132,22 +133,22 @@ namespace language_server_api {
virtual optional<ResponseError> Error() { virtual optional<ResponseError> Error() {
return nullopt; return nullopt;
} }
virtual void WriteResult(Writer& writer) = 0; virtual void WriteResult(Writer& visitor) = 0;
// Message: // Message:
void WriteMessageBody(Writer& writer) override { void WriteMessageBody(Writer& visitor) override {
auto& value = *this; auto& value = *this;
SERIALIZE_MEMBER(id); REFLECT_MEMBER(id);
optional<ResponseError> error = Error(); optional<ResponseError> error = Error();
if (error) { if (error) {
writer.Key("error"); visitor.Key("error");
error->Write(writer); error->Write(visitor);
} }
else { else {
writer.Key("result"); visitor.Key("result");
WriteResult(writer); WriteResult(visitor);
} }
} }
}; };
@ -157,13 +158,13 @@ namespace language_server_api {
virtual void SerializeParams(Writer& writer) = 0; virtual void SerializeParams(Writer& writer) = 0;
// Message: // Message:
void WriteMessageBody(Writer& writer) override { void WriteMessageBody(Writer& visitor) override {
writer.Key("method"); visitor.Key("method");
std::string method = Method(); std::string method = Method();
writer.Key(method.c_str(), method.size()); Reflect(visitor, method);
writer.Key("params"); visitor.Key("params");
SerializeParams(writer); SerializeParams(visitor);
} }
}; };
@ -260,17 +261,17 @@ namespace language_server_api {
}; };
} }
std::unique_ptr<InMessage> Parse(const Reader& reader) { std::unique_ptr<InMessage> Parse(const Reader& visitor) {
std::string jsonrpc = reader["jsonrpc"].GetString(); std::string jsonrpc = visitor["jsonrpc"].GetString();
if (jsonrpc != "2.0") if (jsonrpc != "2.0")
exit(1); exit(1);
optional<RequestId> id; optional<RequestId> id;
if (reader.FindMember("id") != reader.MemberEnd()) if (visitor.FindMember("id") != visitor.MemberEnd())
::Deserialize(reader["id"], id); ::Deserialize(visitor["id"], id);
std::string method; std::string method;
::Deserialize(reader["method"], method); ::Deserialize(visitor["method"], method);
if (allocators.find(method) == allocators.end()) { if (allocators.find(method) == allocators.end()) {
std::cerr << "Unable to find registered handler for method \"" << method << "\"" << std::endl; std::cerr << "Unable to find registered handler for method \"" << method << "\"" << std::endl;
@ -283,8 +284,8 @@ namespace language_server_api {
// We run the allocator with actual params object or a null // We run the allocator with actual params object or a null
// params object if there are no params. Unifying the two ifs is // params object if there are no params. Unifying the two ifs is
// tricky because the second allocator param is a reference. // tricky because the second allocator param is a reference.
if (reader.FindMember("params") != reader.MemberEnd()) { if (visitor.FindMember("params") != visitor.MemberEnd()) {
const Reader& params = reader["params"]; const Reader& params = visitor["params"];
return allocator(id, params); return allocator(id, params);
} }
else { else {
@ -410,12 +411,9 @@ namespace language_server_api {
} }
}; };
void Serialize(Writer& writer, const DocumentUri& value) { template<typename TVisitor>
::Serialize(writer, value.raw_uri); void Reflect(TVisitor& visitor, DocumentUri& value) {
} Reflect(visitor, value.raw_uri);
void Deserialize(const Reader& reader, DocumentUri& value) {
::Deserialize(reader, value.raw_uri);
} }
@ -425,16 +423,12 @@ namespace language_server_api {
int character = 0; int character = 0;
}; };
void Serialize(Writer& writer, const Position& value) { template<typename TVisitor>
writer.StartObject(); void Reflect(TVisitor& visitor, Position& value) {
SERIALIZE_MEMBER(line); REFLECT_MEMBER_START();
SERIALIZE_MEMBER(character); REFLECT_MEMBER(line);
writer.EndObject(); REFLECT_MEMBER(character);
} REFLECT_MEMBER_END();
void Deserialize(const Reader& reader, Position& value) {
DESERIALIZE_MEMBER(line);
DESERIALIZE_MEMBER(character);
} }
@ -443,34 +437,28 @@ namespace language_server_api {
Position end; Position end;
}; };
void Serialize(Writer& writer, const Range& value) { template<typename TVisitor>
writer.StartObject(); void Reflect(TVisitor& visitor, Range& value) {
SERIALIZE_MEMBER(start); REFLECT_MEMBER_START();
SERIALIZE_MEMBER(end); REFLECT_MEMBER(start);
writer.EndObject(); REFLECT_MEMBER(end);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, Range& value) {
DESERIALIZE_MEMBER(start);
DESERIALIZE_MEMBER(end);
}
struct Location { struct Location {
DocumentUri uri; DocumentUri uri;
Range range; Range range;
}; };
void Serialize(Writer& writer, const Location& value) { template<typename TVisitor>
writer.StartObject(); void Reflect(TVisitor& visitor, Location& value) {
SERIALIZE_MEMBER(uri); REFLECT_MEMBER_START();
SERIALIZE_MEMBER(range); REFLECT_MEMBER(uri);
writer.EndObject(); REFLECT_MEMBER(range);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, Location& value) {
DESERIALIZE_MEMBER(uri);
DESERIALIZE_MEMBER(range);
}
enum class SymbolKind : int { enum class SymbolKind : int {
File = 1, File = 1,
@ -493,11 +481,11 @@ namespace language_server_api {
Array = 18 Array = 18
}; };
void Serialize(Writer& writer, const SymbolKind& value) { void Reflect(Writer& writer, SymbolKind& value) {
writer.Int(static_cast<int>(value)); writer.Int(static_cast<int>(value));
} }
void Deserialize(const Reader& reader, SymbolKind& value) { void Reflect(Reader& reader, SymbolKind& value) {
value = static_cast<SymbolKind>(reader.GetInt()); value = static_cast<SymbolKind>(reader.GetInt());
} }
@ -509,20 +497,14 @@ namespace language_server_api {
std::string containerName; std::string containerName;
}; };
void Serialize(Writer& writer, const SymbolInformation& value) { template<typename TVisitor>
writer.StartObject(); void Reflect(TVisitor& visitor, SymbolInformation& value) {
SERIALIZE_MEMBER(name); REFLECT_MEMBER_START();
SERIALIZE_MEMBER(kind); REFLECT_MEMBER(name);
SERIALIZE_MEMBER(location); REFLECT_MEMBER(kind);
SERIALIZE_MEMBER(containerName); REFLECT_MEMBER(location);
writer.EndObject(); REFLECT_MEMBER(containerName);
} REFLECT_MEMBER_END();
void Deserialize(const Reader& reader, SymbolInformation& value) {
DESERIALIZE_MEMBER(name);
DESERIALIZE_MEMBER(kind);
DESERIALIZE_MEMBER(location);
DESERIALIZE_MEMBER(containerName);
} }
@ -535,19 +517,15 @@ namespace language_server_api {
std::vector<std::string> arguments; std::vector<std::string> arguments;
}; };
void Serialize(Writer& writer, const Command& value) { template<typename TVisitor>
writer.StartObject(); void Reflect(TVisitor& visitor, Command& value) {
SERIALIZE_MEMBER(title); REFLECT_MEMBER_START();
SERIALIZE_MEMBER(command); REFLECT_MEMBER(title);
SERIALIZE_MEMBER(arguments); REFLECT_MEMBER(command);
writer.EndObject(); REFLECT_MEMBER(arguments);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, Command& value) {
DESERIALIZE_MEMBER(title);
DESERIALIZE_MEMBER(command);
DESERIALIZE_MEMBER(arguments);
}
// TODO: TextDocumentEdit // TODO: TextDocumentEdit
// TODO: WorkspaceEdit // TODO: WorkspaceEdit
@ -709,23 +687,33 @@ namespace language_server_api {
optional<GenericDynamicReg> executeCommand; optional<GenericDynamicReg> executeCommand;
}; };
void Deserialize(const Reader& reader, WorkspaceClientCapabilites::WorkspaceEdit& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(documentChanges); void Reflect(TVisitor& visitor, WorkspaceClientCapabilites::WorkspaceEdit& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(documentChanges);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, WorkspaceClientCapabilites::GenericDynamicReg& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(dynamicRegistration); void Reflect(TVisitor& visitor, WorkspaceClientCapabilites::GenericDynamicReg& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(dynamicRegistration);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, WorkspaceClientCapabilites& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(applyEdit); void Reflect(TVisitor& visitor, WorkspaceClientCapabilites& value) {
DESERIALIZE_MEMBER(workspaceEdit); REFLECT_MEMBER_START();
DESERIALIZE_MEMBER(didChangeConfiguration); REFLECT_MEMBER(applyEdit);
DESERIALIZE_MEMBER(didChangeWatchedFiles); REFLECT_MEMBER(workspaceEdit);
DESERIALIZE_MEMBER(symbol); REFLECT_MEMBER(didChangeConfiguration);
DESERIALIZE_MEMBER(executeCommand); REFLECT_MEMBER(didChangeWatchedFiles);
REFLECT_MEMBER(symbol);
REFLECT_MEMBER(executeCommand);
REFLECT_MEMBER_END();
} }
// Text document specific client capabilities. // Text document specific client capabilities.
struct TextDocumentClientCapabilities { struct TextDocumentClientCapabilities {
struct Synchronization { struct Synchronization {
@ -812,42 +800,57 @@ namespace language_server_api {
optional<GenericDynamicReg> rename; optional<GenericDynamicReg> rename;
}; };
void Deserialize(const Reader& reader, TextDocumentClientCapabilities::Synchronization& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(dynamicRegistration); void Reflect(TVisitor& visitor, TextDocumentClientCapabilities::Synchronization& value) {
DESERIALIZE_MEMBER(willSave); REFLECT_MEMBER_START();
DESERIALIZE_MEMBER(willSaveWaitUntil); REFLECT_MEMBER(dynamicRegistration);
DESERIALIZE_MEMBER(didSave); REFLECT_MEMBER(willSave);
REFLECT_MEMBER(willSaveWaitUntil);
REFLECT_MEMBER(didSave);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, TextDocumentClientCapabilities::Completion& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(dynamicRegistration); void Reflect(TVisitor& visitor, TextDocumentClientCapabilities::Completion& value) {
DESERIALIZE_MEMBER(completionItem); REFLECT_MEMBER_START();
REFLECT_MEMBER(dynamicRegistration);
REFLECT_MEMBER(completionItem);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, TextDocumentClientCapabilities::Completion::CompletionItem& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(snippetSupport); void Reflect(TVisitor& visitor, TextDocumentClientCapabilities::Completion::CompletionItem& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(snippetSupport);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, TextDocumentClientCapabilities::GenericDynamicReg& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(dynamicRegistration); void Reflect(TVisitor& visitor, TextDocumentClientCapabilities::GenericDynamicReg& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(dynamicRegistration);
REFLECT_MEMBER_END();
} }
void Deserialize(const Reader& reader, TextDocumentClientCapabilities& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(synchronization); void Reflect(TVisitor& visitor, TextDocumentClientCapabilities& value) {
DESERIALIZE_MEMBER(completion); REFLECT_MEMBER_START();
DESERIALIZE_MEMBER(hover); REFLECT_MEMBER(synchronization);
DESERIALIZE_MEMBER(signatureHelp); REFLECT_MEMBER(completion);
DESERIALIZE_MEMBER(references); REFLECT_MEMBER(hover);
DESERIALIZE_MEMBER(documentHighlight); REFLECT_MEMBER(signatureHelp);
DESERIALIZE_MEMBER(documentSymbol); REFLECT_MEMBER(references);
DESERIALIZE_MEMBER(formatting); REFLECT_MEMBER(documentHighlight);
DESERIALIZE_MEMBER(rangeFormatting); REFLECT_MEMBER(documentSymbol);
DESERIALIZE_MEMBER(onTypeFormatting); REFLECT_MEMBER(formatting);
DESERIALIZE_MEMBER(definition); REFLECT_MEMBER(rangeFormatting);
DESERIALIZE_MEMBER(codeAction); REFLECT_MEMBER(onTypeFormatting);
DESERIALIZE_MEMBER(codeLens); REFLECT_MEMBER(definition);
DESERIALIZE_MEMBER(documentLink); REFLECT_MEMBER(codeAction);
DESERIALIZE_MEMBER(rename); REFLECT_MEMBER(codeLens);
REFLECT_MEMBER(documentLink);
REFLECT_MEMBER(rename);
REFLECT_MEMBER_END();
} }
struct ClientCapabilities { struct ClientCapabilities {
@ -863,9 +866,12 @@ namespace language_server_api {
// experimental?: any; // TODO // experimental?: any; // TODO
}; };
void Deserialize(const Reader& reader, ClientCapabilities& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(workspace); void Deserialize(TVisitor& visitor, ClientCapabilities& value) {
DESERIALIZE_MEMBER(textDocument); REFLECT_MEMBER_START();
REFLECT_MEMBER(workspace);
REFLECT_MEMBER(textDocument);
REFLECT_MEMBER_END();
} }
struct InitializeParams { struct InitializeParams {
@ -912,12 +918,15 @@ namespace language_server_api {
value = InitializeParams::Trace::Verbose; value = InitializeParams::Trace::Verbose;
} }
void Deserialize(const Reader& reader, InitializeParams& value) { template<typename TVisitor>
DESERIALIZE_MEMBER(processId); void Reflect(TVisitor& visitor, InitializeParams& value) {
DESERIALIZE_MEMBER(rootPath); REFLECT_MEMBER_START();
DESERIALIZE_MEMBER(rootUri); REFLECT_MEMBER(processId);
DESERIALIZE_MEMBER(capabilities); REFLECT_MEMBER(rootPath);
DESERIALIZE_MEMBER(trace); REFLECT_MEMBER(rootUri);
REFLECT_MEMBER(capabilities);
REFLECT_MEMBER(trace);
REFLECT_MEMBER_END();
} }
@ -942,10 +951,11 @@ namespace language_server_api {
bool retry; bool retry;
}; };
void Serialize(Writer& writer, const InitializeError& value) { template<typename TVisitor>
writer.StartObject(); void Reflect(TVisitor& visitor, InitializeError& value) {
SERIALIZE_MEMBER(retry); REFLECT_MEMBER_START();
writer.EndObject(); REFLECT_MEMBER(retry);
REFLECT_MEMBER_END();
} }
// Defines how the host (editor) should sync document changes to the language server. // Defines how the host (editor) should sync document changes to the language server.
@ -963,7 +973,7 @@ namespace language_server_api {
Incremental = 2 Incremental = 2
}; };
void Serialize(Writer& writer, const TextDocumentSyncKind& value) { void Reflect(Writer& writer, TextDocumentSyncKind& value) {
writer.Int(static_cast<int>(value)); writer.Int(static_cast<int>(value));
} }
@ -977,11 +987,12 @@ namespace language_server_api {
std::vector<std::string> triggerCharacters; std::vector<std::string> triggerCharacters;
}; };
void Serialize(Writer& writer, const CompletionOptions& value) { template<typename TVisitor>
writer.StartObject(); void Reflect(TVisitor& visitor, CompletionOptions& value) {
SERIALIZE_MEMBER(resolveProvider); REFLECT_MEMBER_START();
SERIALIZE_MEMBER(triggerCharacters); REFLECT_MEMBER(resolveProvider);
writer.EndObject(); REFLECT_MEMBER(triggerCharacters);
REFLECT_MEMBER_END();
} }
// Signature help options. // Signature help options.
@ -990,7 +1001,9 @@ namespace language_server_api {
std::vector<std::string> triggerCharacters; std::vector<std::string> triggerCharacters;
}; };
void Serialize(Writer& writer, const SignatureHelpOptions& value) { template<typename TVisitor>
void Serialize(TVisitor& visitor, SignatureHelpOptions& value) {
REFLECT_MEMBER_START();
writer.StartObject(); writer.StartObject();
SERIALIZE_MEMBER(triggerCharacters); SERIALIZE_MEMBER(triggerCharacters);
writer.EndObject(); writer.EndObject();
@ -1382,4 +1395,3 @@ namespace language_server_api {
} }
#endif