further messaging cleanups

This commit is contained in:
Jacob Dufault 2017-03-25 14:45:49 -07:00
parent 5a2acf17ab
commit a31ebb131c
2 changed files with 104 additions and 203 deletions

View File

@ -158,21 +158,19 @@ void Reflect(TVisitor& visitor, Ipc_OpenProject& value) {
struct Ipc_Cout : public IpcMessage<Ipc_Cout> { struct Ipc_Cout : public IpcMessage<Ipc_Cout> {
static constexpr lsMethodId kMethod = lsMethodId::Cout; static constexpr lsMethodId kMethod = lsMethodId::Cout;
std::string content; std::string content;
Ipc_Cout() {}
Ipc_Cout(lsOutMessage& message) {
std::ostringstream out;
message.Send(out);
content = out.str();
}
}; };
template <typename TVisitor> template <typename TVisitor>
void Reflect(TVisitor& visitor, Ipc_Cout& value) { void Reflect(TVisitor& visitor, Ipc_Cout& value) {
Reflect(visitor, value.content); Reflect(visitor, value.content);
} }
void SendOutMessageToClient(IpcMessageQueue* queue, lsOutMessage& response) { template<typename T>
Ipc_Cout out(response); void SendOutMessageToClient(IpcMessageQueue* queue, T& response) {
std::ostringstream sstream;
response.Write(sstream);
Ipc_Cout out;
out.content = sstream.str();
queue->SendMessage(&queue->for_client, Ipc_Cout::kMethod, out); queue->SendMessage(&queue->for_client, Ipc_Cout::kMethod, out);
} }
@ -707,7 +705,7 @@ void LanguageServerStdinLoop(IpcMessageQueue* ipc) {
response.result.capabilities.codeLensProvider = lsCodeLensOptions(); response.result.capabilities.codeLensProvider = lsCodeLensOptions();
response.result.capabilities.codeLensProvider->resolveProvider = false; response.result.capabilities.codeLensProvider->resolveProvider = false;
response.result.capabilities.workspaceSymbolProvider = true; response.result.capabilities.workspaceSymbolProvider = true;
response.Send(std::cout); response.Write(std::cout);
break; break;
} }

View File

@ -197,46 +197,25 @@ struct IpcMessage : public BaseIpcMessage {
IpcMessage() : BaseIpcMessage(T::kMethod) {} IpcMessage() : BaseIpcMessage(T::kMethod) {}
}; };
template<typename TDerived>
struct lsOutMessage { struct lsOutMessage {
// Write out the body of the message. The writer expects object key/value // All derived types need to reflect on the |jsonrpc| member.
// pairs. std::string jsonrpc = "2.0";
virtual void WriteMessageBody(Writer& writer) = 0;
// Send the message to the language client by writing it to stdout. // Send the message to the language client by writing it to stdout.
void Send(std::ostream& out) { void Write(std::ostream& out) {
rapidjson::StringBuffer output; rapidjson::StringBuffer output;
Writer writer(output); Writer writer(output);
writer.StartObject(); auto that = static_cast<TDerived*>(this);
writer.Key("jsonrpc"); Reflect(writer, *that);
writer.String("2.0");
WriteMessageBody(writer);
writer.EndObject();
out << "Content-Length: " << output.GetSize(); out << "Content-Length: " << output.GetSize();
out << (char)13 << char(10) << char(13) << char(10); out << (char)13 << char(10) << char(13) << char(10); // CRLFCRLF
out << output.GetString(); out << output.GetString();
out.flush(); out.flush();
} }
}; };
struct OutRequestMessage : public lsOutMessage {
RequestId id;
virtual std::string Method() = 0;
virtual void SerializeParams(Writer& visitor) = 0;
// Message:
void WriteMessageBody(Writer& visitor) override {
auto& value = *this;
auto method = Method();
REFLECT_MEMBER(id);
REFLECT_MEMBER2("method", method);
visitor.Key("params");
SerializeParams(visitor);
}
};
struct lsResponseError { struct lsResponseError {
@ -276,47 +255,6 @@ struct lsResponseError {
} }
}; };
struct OutResponseMessage : public lsOutMessage {
RequestId id;
virtual optional<lsResponseError> Error() {
return nullopt;
}
virtual void WriteResult(Writer& visitor) = 0;
// Message:
void WriteMessageBody(Writer& visitor) override {
auto& value = *this;
REFLECT_MEMBER(id);
optional<lsResponseError> error = Error();
if (error) {
visitor.Key("error");
error->Write(visitor);
}
else {
visitor.Key("result");
WriteResult(visitor);
}
}
};
struct OutNotificationMessage : public lsOutMessage {
virtual std::string Method() = 0;
virtual void SerializeParams(Writer& writer) = 0;
// Message:
void WriteMessageBody(Writer& visitor) override {
visitor.Key("method");
std::string method = Method();
::Reflect(visitor, method);
visitor.Key("params");
SerializeParams(visitor);
}
};
@ -1250,7 +1188,6 @@ struct Ipc_InitializeRequest : public IpcMessage<Ipc_InitializeRequest>{
RequestId id; RequestId id;
lsInitializeParams params; lsInitializeParams params;
}; };
template<typename TVisitor> template<typename TVisitor>
void Reflect(TVisitor& visitor, Ipc_InitializeRequest& value) { void Reflect(TVisitor& visitor, Ipc_InitializeRequest& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
@ -1259,14 +1196,18 @@ void Reflect(TVisitor& visitor, Ipc_InitializeRequest& value) {
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct Out_InitializeResponse : public OutResponseMessage { struct Out_InitializeResponse : public lsOutMessage<Out_InitializeResponse> {
RequestId id;
lsInitializeResult result; lsInitializeResult result;
// OutResponseMessage:
void WriteResult(Writer& writer) override {
Reflect(writer, result);
}
}; };
template<typename TVisitor>
void Reflect(TVisitor& visitor, Out_InitializeResponse& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
REFLECT_MEMBER(result);
REFLECT_MEMBER_END();
}
struct Ipc_InitializedNotification : public IpcMessage<Ipc_InitializedNotification>{ struct Ipc_InitializedNotification : public IpcMessage<Ipc_InitializedNotification>{
const static lsMethodId kMethod = lsMethodId::Initialized; const static lsMethodId kMethod = lsMethodId::Initialized;
@ -1321,7 +1262,7 @@ void Reflect(TVisitor& visitor, Ipc_InitializedNotification& value) {
// Cancel an existing request.
struct Ipc_CancelRequest : public IpcMessage<Ipc_CancelRequest> { struct Ipc_CancelRequest : public IpcMessage<Ipc_CancelRequest> {
static const lsMethodId kMethod = lsMethodId::CancelRequest; static const lsMethodId kMethod = lsMethodId::CancelRequest;
RequestId id; RequestId id;
@ -1333,7 +1274,7 @@ void Reflect(TVisitor& visitor, Ipc_CancelRequest& value) {
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
// List symbols in a document.
struct lsDocumentSymbolParams { struct lsDocumentSymbolParams {
lsTextDocumentIdentifier textDocument; lsTextDocumentIdentifier textDocument;
}; };
@ -1343,7 +1284,6 @@ void Reflect(TVisitor& visitor, lsDocumentSymbolParams& value) {
REFLECT_MEMBER(textDocument); REFLECT_MEMBER(textDocument);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct Ipc_TextDocumentDocumentSymbol : public IpcMessage<Ipc_TextDocumentDocumentSymbol>{ struct Ipc_TextDocumentDocumentSymbol : public IpcMessage<Ipc_TextDocumentDocumentSymbol>{
const static lsMethodId kMethod = lsMethodId::TextDocumentDocumentSymbol; const static lsMethodId kMethod = lsMethodId::TextDocumentDocumentSymbol;
@ -1357,21 +1297,20 @@ void Reflect(TVisitor& visitor, Ipc_TextDocumentDocumentSymbol& value) {
REFLECT_MEMBER(params); REFLECT_MEMBER(params);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct Out_TextDocumentDocumentSymbol : public lsOutMessage<Out_TextDocumentDocumentSymbol>{
// TODO: refactor OutResponseMessage so we have a normal Reflect visitor (ie, remove 'hidden' base state) RequestId id;
struct Out_TextDocumentDocumentSymbol : public OutResponseMessage {
std::vector<lsSymbolInformation> result; std::vector<lsSymbolInformation> result;
// OutResponseMessage:
void WriteResult(Writer& writer) override {
::Reflect(writer, result);
}
}; };
template<typename TVisitor>
void Reflect(TVisitor& visitor, Out_TextDocumentDocumentSymbol& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
REFLECT_MEMBER(result);
REFLECT_MEMBER_END();
}
// List code lens in a document.
struct lsDocumentCodeLensParams { struct lsDocumentCodeLensParams {
lsTextDocumentIdentifier textDocument; lsTextDocumentIdentifier textDocument;
}; };
@ -1381,21 +1320,17 @@ void Reflect(TVisitor& visitor, lsDocumentCodeLensParams& value) {
REFLECT_MEMBER(textDocument); REFLECT_MEMBER(textDocument);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct lsCodeLensUserData {}; struct lsCodeLensUserData {};
template<typename TVisitor> template<typename TVisitor>
void Reflect(TVisitor& visitor, lsCodeLensUserData& value) { void Reflect(TVisitor& visitor, lsCodeLensUserData& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct lsCodeLensCommandArguments { struct lsCodeLensCommandArguments {
lsDocumentUri uri; lsDocumentUri uri;
lsPosition position; lsPosition position;
std::vector<lsLocation> locations; std::vector<lsLocation> locations;
}; };
void Reflect(Writer& visitor, lsCodeLensCommandArguments& value) { void Reflect(Writer& visitor, lsCodeLensCommandArguments& value) {
visitor.StartArray(); visitor.StartArray();
Reflect(visitor, value.uri); Reflect(visitor, value.uri);
@ -1403,7 +1338,6 @@ void Reflect(Writer& visitor, lsCodeLensCommandArguments& value) {
Reflect(visitor, value.locations); Reflect(visitor, value.locations);
visitor.EndArray(); visitor.EndArray();
} }
void Reflect(Reader& visitor, lsCodeLensCommandArguments& value) { void Reflect(Reader& visitor, lsCodeLensCommandArguments& value) {
auto it = visitor.Begin(); auto it = visitor.Begin();
Reflect(*it, value.uri); Reflect(*it, value.uri);
@ -1412,16 +1346,13 @@ void Reflect(Reader& visitor, lsCodeLensCommandArguments& value) {
++it; ++it;
Reflect(*it, value.locations); Reflect(*it, value.locations);
} }
using TCodeLens = lsCodeLens<lsCodeLensUserData, lsCodeLensCommandArguments>; using TCodeLens = lsCodeLens<lsCodeLensUserData, lsCodeLensCommandArguments>;
struct Ipc_TextDocumentCodeLens : public IpcMessage<Ipc_TextDocumentCodeLens>{ struct Ipc_TextDocumentCodeLens : public IpcMessage<Ipc_TextDocumentCodeLens>{
const static lsMethodId kMethod = lsMethodId::TextDocumentCodeLens; const static lsMethodId kMethod = lsMethodId::TextDocumentCodeLens;
RequestId id; RequestId id;
lsDocumentCodeLensParams params; lsDocumentCodeLensParams params;
}; };
template<typename TVisitor> template<typename TVisitor>
void Reflect(TVisitor& visitor, Ipc_TextDocumentCodeLens& value) { void Reflect(TVisitor& visitor, Ipc_TextDocumentCodeLens& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
@ -1429,23 +1360,24 @@ void Reflect(TVisitor& visitor, Ipc_TextDocumentCodeLens& value) {
REFLECT_MEMBER(params); REFLECT_MEMBER(params);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct Out_TextDocumentCodeLens : public lsOutMessage<Out_TextDocumentCodeLens> {
struct Out_TextDocumentCodeLens : public OutResponseMessage { RequestId id;
std::vector<lsCodeLens<lsCodeLensUserData, lsCodeLensCommandArguments>> result; std::vector<lsCodeLens<lsCodeLensUserData, lsCodeLensCommandArguments>> result;
// OutResponseMessage:
void WriteResult(Writer& writer) override {
::Reflect(writer, result);
}
}; };
template<typename TVisitor>
void Reflect(TVisitor& visitor, Out_TextDocumentCodeLens& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
REFLECT_MEMBER(result);
REFLECT_MEMBER_END();
}
struct Ipc_CodeLensResolve : public IpcMessage<Ipc_CodeLensResolve>{ struct Ipc_CodeLensResolve : public IpcMessage<Ipc_CodeLensResolve>{
const static lsMethodId kMethod = lsMethodId::CodeLensResolve; const static lsMethodId kMethod = lsMethodId::CodeLensResolve;
RequestId id; RequestId id;
TCodeLens params; TCodeLens params;
}; };
template<typename TVisitor> template<typename TVisitor>
void Reflect(TVisitor& visitor, Ipc_CodeLensResolve& value) { void Reflect(TVisitor& visitor, Ipc_CodeLensResolve& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
@ -1453,41 +1385,34 @@ void Reflect(TVisitor& visitor, Ipc_CodeLensResolve& value) {
REFLECT_MEMBER(params); REFLECT_MEMBER(params);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct Out_CodeLensResolve : public lsOutMessage<Out_CodeLensResolve> {
struct Out_CodeLensResolve : public OutResponseMessage { RequestId id;
TCodeLens result; TCodeLens result;
// OutResponseMessage:
void WriteResult(Writer& writer) override {
::Reflect(writer, result);
}
}; };
template<typename TVisitor>
void Reflect(TVisitor& visitor, Out_CodeLensResolve& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
REFLECT_MEMBER(result);
REFLECT_MEMBER_END();
}
// Search for symbols in the workspace.
struct lsWorkspaceSymbolParams { struct lsWorkspaceSymbolParams {
std::string query; std::string query;
}; };
template<typename TVisitor> template<typename TVisitor>
void Reflect(TVisitor& visitor, lsWorkspaceSymbolParams& value) { void Reflect(TVisitor& visitor, lsWorkspaceSymbolParams& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER(query); REFLECT_MEMBER(query);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct Ipc_WorkspaceSymbol : public IpcMessage<Ipc_WorkspaceSymbol >{ struct Ipc_WorkspaceSymbol : public IpcMessage<Ipc_WorkspaceSymbol >{
const static lsMethodId kMethod = lsMethodId::WorkspaceSymbol; const static lsMethodId kMethod = lsMethodId::WorkspaceSymbol;
RequestId id; RequestId id;
lsWorkspaceSymbolParams params; lsWorkspaceSymbolParams params;
}; };
template<typename TVisitor> template<typename TVisitor>
void Reflect(TVisitor& visitor, Ipc_WorkspaceSymbol& value) { void Reflect(TVisitor& visitor, Ipc_WorkspaceSymbol& value) {
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
@ -1495,92 +1420,70 @@ void Reflect(TVisitor& visitor, Ipc_WorkspaceSymbol& value) {
REFLECT_MEMBER(params); REFLECT_MEMBER(params);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
} }
struct Out_WorkspaceSymbol : public lsOutMessage<Out_WorkspaceSymbol> {
struct Out_WorkspaceSymbol : public OutResponseMessage { RequestId id;
std::vector<lsSymbolInformation> result; std::vector<lsSymbolInformation> result;
// OutResponseMessage:
void WriteResult(Writer& writer) override {
::Reflect(writer, result);
}
}; };
template<typename TVisitor>
void Reflect(TVisitor& visitor, Out_WorkspaceSymbol& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
REFLECT_MEMBER(result);
REFLECT_MEMBER_END();
}
// Show a message to the user.
enum class lsMessageType : int { enum class lsMessageType : int {
Error = 1, Error = 1,
Warning = 2, Warning = 2,
Info = 3, Info = 3,
Log = 4 Log = 4
}; };
template<typename TWriter> template<typename TWriter>
void Reflect(TWriter& writer, lsMessageType& value) { void Reflect(TWriter& writer, lsMessageType& value) {
int value0 = static_cast<int>(value); int value0 = static_cast<int>(value);
Reflect(writer, value0); Reflect(writer, value0);
value = static_cast<lsMessageType>(value0); value = static_cast<lsMessageType>(value0);
} }
struct Out_ShowLogMessageParams {
struct ShowMessageOutNotification : public OutNotificationMessage {
lsMessageType type = lsMessageType::Error; lsMessageType type = lsMessageType::Error;
std::string message; std::string message;
};
// OutNotificationMessage: template<typename TVisitor>
std::string Method() override { void Reflect(TVisitor& visitor, Out_ShowLogMessageParams& value) {
return "window/showMessage";
}
void SerializeParams(Writer& visitor) override {
auto& value = *this;
REFLECT_MEMBER_START(); REFLECT_MEMBER_START();
REFLECT_MEMBER(type); REFLECT_MEMBER(type);
REFLECT_MEMBER(message); REFLECT_MEMBER(message);
REFLECT_MEMBER_END(); REFLECT_MEMBER_END();
}
struct Out_ShowLogMessage : public lsOutMessage<Out_ShowLogMessage> {
enum class DisplayType {
Show, Log
};
DisplayType display_type = DisplayType::Show;
std::string method() {
switch (display_type) {
case Out_ShowLogMessage::DisplayType::Show:
return "window/showMessage";
break;
case Out_ShowLogMessage::DisplayType::Log:
return "window/logMessage";
break;
} }
}; assert(false);
struct LogMessageOutNotification : public OutNotificationMessage {
lsMessageType type = lsMessageType::Error;
std::string message;
// OutNotificationMessage:
std::string Method() override {
return "window/logMessage"; return "window/logMessage";
} }
void SerializeParams(Writer& visitor) override {
auto& value = *this; Out_ShowLogMessageParams params;
REFLECT_MEMBER_START();
REFLECT_MEMBER(type);
REFLECT_MEMBER(message);
REFLECT_MEMBER_END();
}
}; };
template<typename TVisitor>
void Reflect(TVisitor& visitor, Out_ShowLogMessage& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER2("method", value.method());
REFLECT_MEMBER(params);
REFLECT_MEMBER_END();
}