Index: clangd/CMakeLists.txt =================================================================== --- clangd/CMakeLists.txt +++ clangd/CMakeLists.txt @@ -10,6 +10,7 @@ DraftStore.cpp GlobalCompilationDatabase.cpp JSONRPCDispatcher.cpp + JSONExpr.cpp Logger.cpp Protocol.cpp ProtocolHandlers.cpp Index: clangd/ClangdLSPServer.cpp =================================================================== --- clangd/ClangdLSPServer.cpp +++ clangd/ClangdLSPServer.cpp @@ -15,40 +15,46 @@ namespace { -std::string +json::ary replacementsToEdits(StringRef Code, const std::vector &Replacements) { // Turn the replacements into the format specified by the Language Server // Protocol. Fuse them into one big JSON array. - std::string Edits; + json::ary Edits; for (auto &R : Replacements) { Range ReplacementRange = { offsetToPosition(Code, R.getOffset()), offsetToPosition(Code, R.getOffset() + R.getLength())}; TextEdit TE = {ReplacementRange, R.getReplacementText()}; - Edits += TextEdit::unparse(TE); - Edits += ','; + Edits.push_back(TE); } - if (!Edits.empty()) - Edits.pop_back(); - return Edits; } } // namespace void ClangdLSPServer::onInitialize(Ctx C, InitializeParams &Params) { - C.reply( - R"({"capabilities":{ - "textDocumentSync": 1, - "documentFormattingProvider": true, - "documentRangeFormattingProvider": true, - "documentOnTypeFormattingProvider": {"firstTriggerCharacter":"}","moreTriggerCharacter":[]}, - "codeActionProvider": true, - "completionProvider": {"resolveProvider": false, "triggerCharacters": [".",">",":"]}, - "signatureHelpProvider": {"triggerCharacters": ["(",","]}, - "definitionProvider": true - }})"); + C.reply(json::obj{ + {"textDocumentSync", 1}, + {"documentFormattingProvider", true}, + {"documentRangeFormattingProvider", true}, + {"documentOnTypeFormattingProvider", + json::obj{ + {"firstTriggerCharacter", "}"}, + {"moreTriggerCharacter", {}}, + }}, + {"codeActionProvider", true}, + {"completionProvider", + json::obj{ + {"resolveProvider", false}, + {"triggerCharacters", {".", ">", ":"}}, + }}, + {"signatureHelpProvider", + json::obj{ + {"triggerCharacters", {"(", ","}}, + }}, + {"definitionProvider", true}, + }); if (Params.rootUri && !Params.rootUri->file.empty()) Server.setRootPath(Params.rootUri->file); else if (Params.rootPath && !Params.rootPath->empty()) @@ -58,7 +64,7 @@ void ClangdLSPServer::onShutdown(Ctx C, ShutdownParams &Params) { // Do essentially nothing, just say we're ready to exit. ShutdownRequestReceived = true; - C.reply("null"); + C.reply(nullptr); } void ClangdLSPServer::onExit(Ctx C, ExitParams &Params) { IsDone = true; } @@ -93,49 +99,41 @@ Ctx C, DocumentOnTypeFormattingParams &Params) { auto File = Params.textDocument.uri.file; std::string Code = Server.getDocument(File); - std::string Edits = - replacementsToEdits(Code, Server.formatOnType(File, Params.position)); - C.reply("[" + Edits + "]"); + C.reply( + replacementsToEdits(Code, Server.formatOnType(File, Params.position))); } void ClangdLSPServer::onDocumentRangeFormatting( Ctx C, DocumentRangeFormattingParams &Params) { auto File = Params.textDocument.uri.file; std::string Code = Server.getDocument(File); - std::string Edits = - replacementsToEdits(Code, Server.formatRange(File, Params.range)); - C.reply("[" + Edits + "]"); + C.reply(replacementsToEdits(Code, Server.formatRange(File, Params.range))); } void ClangdLSPServer::onDocumentFormatting(Ctx C, DocumentFormattingParams &Params) { auto File = Params.textDocument.uri.file; std::string Code = Server.getDocument(File); - std::string Edits = replacementsToEdits(Code, Server.formatFile(File)); - C.reply("[" + Edits + "]"); + C.reply(replacementsToEdits(Code, Server.formatFile(File))); } void ClangdLSPServer::onCodeAction(Ctx C, CodeActionParams &Params) { // We provide a code action for each diagnostic at the requested location // which has FixIts available. std::string Code = Server.getDocument(Params.textDocument.uri.file); - std::string Commands; + json::ary Commands; for (Diagnostic &D : Params.context.diagnostics) { std::vector Fixes = getFixIts(Params.textDocument.uri.file, D); - std::string Edits = replacementsToEdits(Code, Fixes); - + auto Edits = replacementsToEdits(Code, Fixes); if (!Edits.empty()) - Commands += - R"({"title":"Apply FixIt ')" + llvm::yaml::escape(D.message) + - R"('", "command": "clangd.applyFix", "arguments": [")" + - llvm::yaml::escape(Params.textDocument.uri.uri) + - R"(", [)" + Edits + - R"(]]},)"; + Commands.push_back(json::obj{ + {"title", llvm::formatv("Apply FixIt {0}", D.message)}, + {"command", "clangd.applyFix"}, + {"arguments", {Params.textDocument.uri.uri, std::move(Edits)}}, + }); } - if (!Commands.empty()) - Commands.pop_back(); - C.reply("[" + Commands + "]"); + C.reply(std::move(Commands)); } void ClangdLSPServer::onCompletion(Ctx C, TextDocumentPositionParams &Params) { @@ -147,15 +145,7 @@ // had an API that would allow to attach callbacks to // futures returned by ClangdServer. .Value; - - std::string Completions; - for (const auto &Item : Items) { - Completions += CompletionItem::unparse(Item); - Completions += ","; - } - if (!Completions.empty()) - Completions.pop_back(); - C.reply("[" + Completions + "]"); + C.reply(json::ary(Items)); } void ClangdLSPServer::onSignatureHelp(Ctx C, @@ -165,7 +155,7 @@ Position{Params.position.line, Params.position.character}); if (!SignatureHelp) return C.replyError(-32602, llvm::toString(SignatureHelp.takeError())); - C.reply(SignatureHelp::unparse(SignatureHelp->Value)); + C.reply(SignatureHelp->Value); } void ClangdLSPServer::onGoToDefinition(Ctx C, @@ -175,22 +165,14 @@ Position{Params.position.line, Params.position.character}); if (!Items) return C.replyError(-32602, llvm::toString(Items.takeError())); - - std::string Locations; - for (const auto &Item : Items->Value) { - Locations += Location::unparse(Item); - Locations += ","; - } - if (!Locations.empty()) - Locations.pop_back(); - C.reply("[" + Locations + "]"); + C.reply(json::ary(Items->Value)); } void ClangdLSPServer::onSwitchSourceHeader(Ctx C, TextDocumentIdentifier &Params) { llvm::Optional Result = Server.switchSourceHeader(Params.uri.file); std::string ResultUri; - C.reply(Result ? URI::unparse(URI::fromFile(*Result)) : R"("")"); + C.reply(Result ? URI::fromFile(*Result).uri : ""); } ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount, @@ -240,17 +222,16 @@ void ClangdLSPServer::onDiagnosticsReady( PathRef File, Tagged> Diagnostics) { - std::string DiagnosticsJSON; + json::ary DiagnosticsJSON; DiagnosticToReplacementMap LocalFixIts; // Temporary storage for (auto &DiagWithFixes : Diagnostics.Value) { auto Diag = DiagWithFixes.Diag; - DiagnosticsJSON += - R"({"range":)" + Range::unparse(Diag.range) + - R"(,"severity":)" + std::to_string(Diag.severity) + - R"(,"message":")" + llvm::yaml::escape(Diag.message) + - R"("},)"; - + DiagnosticsJSON.push_back(json::obj{ + {"range", Diag.range}, + {"severity", Diag.severity}, + {"message", Diag.message}, + }); // We convert to Replacements to become independent of the SourceManager. auto &FixItsForDiagnostic = LocalFixIts[Diag]; std::copy(DiagWithFixes.FixIts.begin(), DiagWithFixes.FixIts.end(), @@ -265,10 +246,13 @@ } // Publish diagnostics. - if (!DiagnosticsJSON.empty()) - DiagnosticsJSON.pop_back(); // Drop trailing comma. - Out.writeMessage( - R"({"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":")" + - URI::fromFile(File).uri + R"(","diagnostics":[)" + DiagnosticsJSON + - R"(]}})"); + Out.writeMessage(json::obj{ + {"jsonrpc", "2.0"}, + {"method", "textDocument/publishDiagnostics"}, + {"params", + json::obj{ + {"uri", URI::fromFile(File)}, + {"diagnostics", std::move(DiagnosticsJSON)}, + }}, + }); } Index: clangd/JSONExpr.h =================================================================== --- /dev/null +++ clangd/JSONExpr.h @@ -0,0 +1,238 @@ +//===--- JSONExpr.h - composable JSON expressions ---------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSON_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSON_H + +#include + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/raw_ostream.h" + +namespace clang { +namespace clangd { +namespace json { + +// An Expr is an opaque temporary JSON structure used to compose documents. +// They can be copied, but should generally be moved. +// +// You can implicitly construct literals from: +// - strings: std::string, SmallString, formatv, StringRef, char* +// (char*, and StringRef are references, not copies!) +// - numbers +// - booleans +// - null: nullptr +// - arrays: {"foo", 42.0, false} +// - serializable things: any T with a T::unparse(const T&) -> Expr +// +// They can also be constructed from object/array helpers: +// - json::obj is a type like map +// - json::arr is a type like vector +// These can be list-initialized, or used to build up collections in a loop. +// json::arr(Collection) converts all items in a collection to Exprs. +// +// Exprs can be serialized to JSON: +// 1) raw_ostream << Expr // Basic formatting. +// 2) raw_ostream << formatv("{0}", Expr) // Basic formatting. +// 3) raw_ostream << formatv("{0:2}", Expr) // Pretty-print with indent 2. +class Expr { +public: + class Object; + class ObjectKey; + class Array; + + Expr() : Type(T_Null) {} + Expr(const Expr &M) { copyFrom(M); } + Expr(Expr &&M) { moveFrom(std::move(M)); } + // "cheating" move-constructor for moving from initializer_list. + Expr(const Expr &&M) { moveFrom(std::move(M)); } + Expr(std::initializer_list Elements) : Expr(Array(Elements)) {} + Expr(Array &&Elements) : Type(T_Array) { create(std::move(Elements)); } + Expr(Object &&Properties) : Type(T_Object) { + create(std::move(Properties)); + } + // Strings: types with value semantics. + Expr(std::string &&V) : Type(T_String) { create(std::move(V)); } + Expr(const std::string &V) : Type(T_String) { create(V); } + Expr(const llvm::SmallVectorImpl &V) : Type(T_String) { + create(V.begin(), V.end()); + } + Expr(const llvm::formatv_object_base &V) : Expr(V.str()){}; + // Strings: types with reference semantics. + Expr(llvm::StringRef V) : Type(T_StringRef) { create(V); } + Expr(const char *V) : Type(T_StringRef) { create(V); } + Expr(std::nullptr_t) : Type(T_Null) {} + // Prevent implicit conversions to boolean. + template ::value>::type> + Expr(T B) : Type(T_Boolean) { + create(B); + } + // Numbers: arithmetic types that are not boolean. + template < + typename T, + typename = typename std::enable_if::value>::type, + typename = typename std::enable_if::value>::value>::type> + Expr(T D) : Type(T_Number) { + create(D); + } + // Types with a static T::unparse function returning an Expr. + template ::value>> + Expr(const T &V) : Expr(T::unparse(V)) {} + + Expr &operator=(const Expr &M) { + destroy(); + copyFrom(M); + return *this; + } + Expr &operator=(Expr &&M) { + destroy(); + moveFrom(std::move(M)); + return *this; + } + ~Expr() { destroy(); } + + friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Expr &); + +private: + void destroy(); + void copyFrom(const Expr &M); + // We allow moving from *const* Exprs, by marking all members as mutable! + // This hack is needed to support initializer-list syntax efficiently. + // (std::initializer_list is a container of const T). + void moveFrom(const Expr &&M); + + template void create(U &&... V) { + new (&as()) T(std::forward(V)...); + } + template T &as() const { + return *reinterpret_cast(Union.buffer); + } + + template + void print(llvm::raw_ostream &, const Indenter &) const; + friend struct llvm::format_provider; + + enum ExprType : char { + T_Null, + T_Boolean, + T_Number, + T_StringRef, + T_String, + T_Object, + T_Array, + }; + mutable ExprType Type; + +public: + // ObjectKey is a used to capture keys in Expr::Objects. It's like Expr but: + // - only strings are allowed + // - it's copyable (for std::map) + // - we're slightly more eager to copy, to allow efficient key compares + // - it's optimized for the string literal case (Owned == nullptr) + class ObjectKey { + public: + ObjectKey(const char *S) : Data(S) {} + ObjectKey(llvm::StringRef S) : Data(S) {} + ObjectKey(std::string &&V) + : Owned(new std::string(std::move(V))), Data(*Owned) {} + ObjectKey(const std::string &V) : Owned(new std::string(V)), Data(*Owned) {} + ObjectKey(const llvm::SmallVectorImpl &V) + : ObjectKey(std::string(V.begin(), V.end())) {} + ObjectKey(const llvm::formatv_object_base &V) : ObjectKey(V.str()) {} + + ObjectKey(const ObjectKey &C) { *this = C; } + ObjectKey(ObjectKey &&C) = default; + ObjectKey &operator=(const ObjectKey &C) { + if (C.Owned) { + Owned.reset(new std::string(*C.Owned)); + Data = *Owned; + } else { + Data = C.Data; + } + return *this; + } + ObjectKey &operator=(ObjectKey &&) = default; + + operator llvm::StringRef() const { return Data; } + + friend bool operator<(const ObjectKey &L, const ObjectKey &R) { + return L.Data < R.Data; + } + + // "cheating" move-constructor for moving from initializer_list. + ObjectKey(const ObjectKey &&V) { + Owned = std::move(V.Owned); + Data = V.Data; + } + + private: + mutable std::unique_ptr Owned; // mutable for cheating. + llvm::StringRef Data; + }; + + class Object : public std::map { + public: + explicit Object() {} + // Use a custom struct for list-init, because pair forces extra copies. + struct KV; + explicit Object(std::initializer_list Properties); + }; + + class Array : public std::vector { + public: + explicit Array() {} + explicit Array(std::initializer_list Elements) { + reserve(Elements.size()); + for (const Expr &V : Elements) + emplace_back(std::move(V)); + }; + template explicit Array(const Collection &C) { + for (const auto &V : C) + emplace_back(V); + } + }; + +private: + mutable llvm::AlignedCharArrayUnion + Union; +}; + +struct Expr::Object::KV { + ObjectKey K; + Expr V; +}; + +inline Expr::Object::Object(std::initializer_list Properties) { + for (const auto &P : Properties) + emplace(std::move(P.K), std::move(P.V)); +} + +// Give Expr::{Object,Array} more convenient names for literal use. +using obj = Expr::Object; +using ary = Expr::Array; + +} // namespace json +} // namespace clangd +} // namespace clang + +namespace llvm { +template <> struct format_provider { + static void format(const clang::clangd::json::Expr &, raw_ostream &, + StringRef); +}; +} // namespace llvm + +#endif Index: clangd/JSONExpr.cpp =================================================================== --- /dev/null +++ clangd/JSONExpr.cpp @@ -0,0 +1,216 @@ +#include "JSONExpr.h" + +#include "llvm/Support/Format.h" + +namespace clang { +namespace clangd { +namespace json { +using namespace llvm; + +void Expr::copyFrom(const Expr &M) { + Type = M.Type; + switch (Type) { + case T_Null: + case T_Boolean: + case T_Number: + memcpy(Union.buffer, M.Union.buffer, sizeof(Union.buffer)); + break; + case T_StringRef: + create(M.as()); + break; + case T_String: + create(M.as()); + break; + case T_Object: + create(M.as()); + break; + case T_Array: + create(M.as()); + break; + } +} + +void Expr::moveFrom(const Expr &&M) { + Type = M.Type; + switch (Type) { + case T_Null: + case T_Boolean: + case T_Number: + memcpy(Union.buffer, M.Union.buffer, sizeof(Union.buffer)); + break; + case T_StringRef: + create(M.as()); + break; + case T_String: + create(std::move(M.as())); + M.Type = T_Null; + break; + case T_Object: + create(std::move(M.as())); + M.Type = T_Null; + break; + case T_Array: + create(std::move(M.as())); + M.Type = T_Null; + break; + } +} + +void Expr::destroy() { + switch (Type) { + case T_Null: + case T_Boolean: + case T_Number: + break; + case T_StringRef: + as().~StringRef(); + break; + case T_String: + as().~basic_string(); + break; + case T_Object: + as().~Object(); + break; + case T_Array: + as().~Array(); + break; + } +} + +} // namespace json +} // namespace clangd +} // namespace clang + +namespace { +void quote(llvm::raw_ostream &OS, llvm::StringRef S) { + OS << '\"'; + for (unsigned char C : S) { + if (C == 0x22 || C == 0x5C) + OS << '\\'; + if (C >= 0x20) { + OS << C; + continue; + } + OS << '\\'; + switch (C) { + // A few characters are common enough to make short escapes worthwhile. + case '\t': + OS << 't'; + break; + case '\n': + OS << 'n'; + break; + case '\r': + OS << 'r'; + break; + default: + OS << 'u'; + llvm::write_hex(OS, C, llvm::HexPrintStyle::Lower, 4); + break; + } + } + OS << '\"'; +} + +enum IndenterAction { + Indent, + Outdent, + Newline, + Space, +}; +} // namespace + +// Prints JSON. The indenter can be used to control formatting. +template +void clang::clangd::json::Expr::print(raw_ostream &OS, + const Indenter &I) const { + switch (Type) { + case T_Null: + OS << "null"; + break; + case T_Boolean: + OS << (as() ? "true" : "false"); + break; + case T_Number: + OS << format("%g", as()); + break; + case T_StringRef: + quote(OS, as()); + break; + case T_String: + quote(OS, as()); + break; + case T_Object: { + bool Comma = false; + OS << '{'; + I(Indent); + for (const auto &P : as()) { + if (Comma) + OS << ','; + Comma = true; + I(Newline); + quote(OS, P.first); + OS << ':'; + I(Space); + P.second.print(OS, I); + } + I(Outdent); + if (Comma) + I(Newline); + OS << '}'; + break; + } + case T_Array: { + bool Comma = false; + OS << '['; + I(Indent); + for (const auto &E : as()) { + if (Comma) + OS << ','; + Comma = true; + I(Newline); + E.print(OS, I); + } + I(Outdent); + if (Comma) + I(Newline); + OS << ']'; + break; + } + } +} + +llvm::raw_ostream &clang::clangd::json::operator<<(raw_ostream &OS, + const Expr &E) { + E.print(OS, [](IndenterAction A) { /*ignore*/ }); + return OS; +} + +void llvm::format_provider::format( + const clang::clangd::json::Expr &E, raw_ostream &OS, StringRef Options) { + if (Options.empty()) { + OS << E; + return; + } + unsigned IndentAmount = 0; + if (Options.getAsInteger(10, IndentAmount)) + assert(false && "json::Expr format options should be an integer"); + unsigned IndentLevel = 0; + E.print(OS, [&](IndenterAction A) { + switch (A) { + case Newline: + OS << '\n'; + OS.indent(IndentLevel); + break; + case Space: + OS << ' '; + break; + case Indent: + IndentLevel += IndentAmount; + break; + case Outdent: + IndentLevel -= IndentAmount; + break; + }; + }); +} Index: clangd/JSONRPCDispatcher.h =================================================================== --- clangd/JSONRPCDispatcher.h +++ clangd/JSONRPCDispatcher.h @@ -10,6 +10,7 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSONRPCDISPATCHER_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSONRPCDISPATCHER_H +#include "JSONExpr.h" #include "Logger.h" #include "clang/Basic/LLVM.h" #include "llvm/ADT/SmallString.h" @@ -26,11 +27,11 @@ class JSONOutput : public Logger { public: JSONOutput(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs, - llvm::raw_ostream *InputMirror = nullptr) - : Outs(Outs), Logs(Logs), InputMirror(InputMirror) {} + llvm::raw_ostream *InputMirror = nullptr, bool Pretty = false) + : Outs(Outs), Logs(Logs), InputMirror(InputMirror), Pretty(Pretty) {} /// Emit a JSONRPC message. - void writeMessage(const Twine &Message); + void writeMessage(const json::Expr &Result); /// Write to the logging stream. /// No newline is implicitly added. (TODO: we should fix this!) @@ -45,6 +46,7 @@ llvm::raw_ostream &Outs; llvm::raw_ostream &Logs; llvm::raw_ostream *InputMirror; + bool Pretty; std::mutex StreamMutex; }; @@ -52,16 +54,17 @@ /// Context object passed to handlers to allow replies. class RequestContext { public: - RequestContext(JSONOutput &Out, StringRef ID) : Out(Out), ID(ID) {} + RequestContext(JSONOutput &Out, llvm::Optional ID) + : Out(Out), ID(std::move(ID)) {} - /// Sends a successful reply. Result should be well-formed JSON. - void reply(const Twine &Result); + /// Sends a successful reply. + void reply(json::Expr &&Result); /// Sends an error response to the client, and logs it. void replyError(int code, const llvm::StringRef &Message); private: JSONOutput &Out; - llvm::SmallString<64> ID; // Valid JSON, or empty for a notification. + llvm::Optional ID; }; /// Main JSONRPC entry point. This parses the JSONRPC "header" and calls the Index: clangd/JSONRPCDispatcher.cpp =================================================================== --- clangd/JSONRPCDispatcher.cpp +++ clangd/JSONRPCDispatcher.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "JSONRPCDispatcher.h" +#include "JSONExpr.h" #include "ProtocolHandlers.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/SourceMgr.h" @@ -17,17 +18,22 @@ using namespace clang; using namespace clangd; -void JSONOutput::writeMessage(const Twine &Message) { - llvm::SmallString<128> Storage; - StringRef M = Message.toStringRef(Storage); +void JSONOutput::writeMessage(const json::Expr &Message) { + std::string S; + llvm::raw_string_ostream OS(S); + if (Pretty) + OS << llvm::formatv("{0:2}", Message); + else + OS << Message; + OS.flush(); std::lock_guard Guard(StreamMutex); // Log without headers. - Logs << "--> " << M << '\n'; + Logs << "--> " << S << '\n'; Logs.flush(); // Emit message with header. - Outs << "Content-Length: " << M.size() << "\r\n\r\n" << M; + Outs << "Content-Length: " << S.size() << "\r\n\r\n" << S; Outs.flush(); } @@ -45,22 +51,26 @@ InputMirror->flush(); } -void RequestContext::reply(const llvm::Twine &Result) { - if (ID.empty()) { +void RequestContext::reply(json::Expr &&Result) { + if (!ID) { Out.log("Attempted to reply to a notification!\n"); return; } - Out.writeMessage(llvm::Twine(R"({"jsonrpc":"2.0","id":)") + ID + - R"(,"result":)" + Result + "}"); + Out.writeMessage(json::obj{ + {"jsonrpc", "2.0"}, + {"id", *ID}, + {"result", std::move(Result)}, + }); } void RequestContext::replyError(int code, const llvm::StringRef &Message) { Out.log("Error " + llvm::Twine(code) + ": " + Message + "\n"); - if (!ID.empty()) { - Out.writeMessage(llvm::Twine(R"({"jsonrpc":"2.0","id":)") + ID + - R"(,"error":{"code":)" + llvm::Twine(code) + - R"(,"message":")" + llvm::yaml::escape(Message) + - R"("}})"); + if (ID) { + Out.writeMessage(json::obj{ + {"jsonrpc", "2.0"}, + {"id", *ID}, + {"result", Message}, + }); } } @@ -71,13 +81,13 @@ static void callHandler(const llvm::StringMap &Handlers, - llvm::yaml::ScalarNode *Method, llvm::yaml::ScalarNode *Id, + llvm::yaml::ScalarNode *Method, llvm::Optional ID, llvm::yaml::MappingNode *Params, const JSONRPCDispatcher::Handler &UnknownHandler, JSONOutput &Out) { llvm::SmallString<64> MethodStorage; auto I = Handlers.find(Method->getValue(MethodStorage)); auto &Handler = I != Handlers.end() ? I->second : UnknownHandler; - Handler(RequestContext(Out, Id ? Id->getRawValue() : ""), Params); + Handler(RequestContext(Out, std::move(ID)), Params); } bool JSONRPCDispatcher::call(StringRef Content, JSONOutput &Out) const { @@ -95,7 +105,7 @@ llvm::yaml::ScalarNode *Version = nullptr; llvm::yaml::ScalarNode *Method = nullptr; llvm::yaml::MappingNode *Params = nullptr; - llvm::yaml::ScalarNode *Id = nullptr; + llvm::Optional ID; for (auto &NextKeyValue : *Object) { auto *KeyString = dyn_cast_or_null(NextKeyValue.getKey()); @@ -116,7 +126,18 @@ } else if (KeyValue == "method") { Method = dyn_cast(Value); } else if (KeyValue == "id") { - Id = dyn_cast(Value); + // ID may be either a string or a number. + if (auto *IdNode = dyn_cast(Value)) { + llvm::SmallString<32> S; + llvm::StringRef V = IdNode->getValue(S); + if (IdNode->getRawValue().startswith("\"")) { + ID.emplace(V.str()); + } else { + double D; + if (!V.getAsDouble(D)) + ID.emplace(D); + } + } } else if (KeyValue == "params") { if (!Method) return false; @@ -125,7 +146,7 @@ // because it will break clients that put the id after params. A possible // fix would be to split the parsing and execution phases. Params = dyn_cast(Value); - callHandler(Handlers, Method, Id, Params, UnknownHandler, Out); + callHandler(Handlers, Method, std::move(ID), Params, UnknownHandler, Out); return true; } else { return false; @@ -136,7 +157,7 @@ // leftovers. if (!Method) return false; - callHandler(Handlers, Method, Id, nullptr, UnknownHandler, Out); + callHandler(Handlers, Method, std::move(ID), nullptr, UnknownHandler, Out); return true; } Index: clangd/Protocol.h =================================================================== --- clangd/Protocol.h +++ clangd/Protocol.h @@ -21,6 +21,7 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H +#include "JSONExpr.h" #include "llvm/ADT/Optional.h" #include "llvm/Support/YAMLParser.h" #include @@ -39,7 +40,7 @@ static URI fromFile(llvm::StringRef file); static URI parse(llvm::yaml::ScalarNode *Param); - static std::string unparse(const URI &U); + static json::Expr unparse(const URI &U); friend bool operator==(const URI &LHS, const URI &RHS) { return LHS.uri == RHS.uri; @@ -80,7 +81,7 @@ static llvm::Optional parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger); - static std::string unparse(const Position &P); + static json::Expr unparse(const Position &P); }; struct Range { @@ -99,7 +100,7 @@ static llvm::Optional parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger); - static std::string unparse(const Range &P); + static json::Expr unparse(const Range &P); }; struct Location { @@ -119,7 +120,7 @@ return std::tie(LHS.uri, LHS.range) < std::tie(RHS.uri, RHS.range); } - static std::string unparse(const Location &P); + static json::Expr unparse(const Location &P); }; struct Metadata { @@ -140,7 +141,7 @@ static llvm::Optional parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger); - static std::string unparse(const TextEdit &P); + static json::Expr unparse(const TextEdit &P); }; struct TextDocumentItem { @@ -282,7 +283,7 @@ static llvm::Optional parse(llvm::yaml::MappingNode *Params, clangd::Logger &Logger); - static std::string unparse(const FormattingOptions &P); + static json::Expr unparse(const FormattingOptions &P); }; struct DocumentRangeFormattingParams { @@ -486,7 +487,7 @@ // // data?: any - A data entry field that is preserved on a completion item // between a completion and a completion resolve request. - static std::string unparse(const CompletionItem &P); + static json::Expr unparse(const CompletionItem &P); }; /// A single parameter of a particular signature. @@ -498,7 +499,7 @@ /// The documentation of this parameter. Optional. std::string documentation; - static std::string unparse(const ParameterInformation &); + static json::Expr unparse(const ParameterInformation &); }; /// Represents the signature of something callable. @@ -513,7 +514,7 @@ /// The parameters of this signature. std::vector parameters; - static std::string unparse(const SignatureInformation &); + static json::Expr unparse(const SignatureInformation &); }; /// Represents the signature of a callable. @@ -528,7 +529,7 @@ /// The active parameter of the active signature. int activeParameter = 0; - static std::string unparse(const SignatureHelp &); + static json::Expr unparse(const SignatureHelp &); }; } // namespace clangd Index: clangd/Protocol.cpp =================================================================== --- clangd/Protocol.cpp +++ clangd/Protocol.cpp @@ -63,7 +63,7 @@ return URI::fromUri(Param->getValue(Storage)); } -std::string URI::unparse(const URI &U) { return "\"" + U.uri + "\""; } +json::Expr URI::unparse(const URI &U) { return U.uri; } llvm::Optional TextDocumentIdentifier::parse(llvm::yaml::MappingNode *Params, @@ -125,11 +125,11 @@ return Result; } -std::string Position::unparse(const Position &P) { - std::string Result; - llvm::raw_string_ostream(Result) - << llvm::format(R"({"line": %d, "character": %d})", P.line, P.character); - return Result; +json::Expr Position::unparse(const Position &P) { + return json::obj{ + {"line", P.line}, + {"character", P.character}, + }; } llvm::Optional Range::parse(llvm::yaml::MappingNode *Params, @@ -165,20 +165,18 @@ return Result; } -std::string Range::unparse(const Range &P) { - std::string Result; - llvm::raw_string_ostream(Result) << llvm::format( - R"({"start": %s, "end": %s})", Position::unparse(P.start).c_str(), - Position::unparse(P.end).c_str()); - return Result; +json::Expr Range::unparse(const Range &P) { + return json::obj{ + {"start", P.start}, + {"end", P.end}, + }; } -std::string Location::unparse(const Location &P) { - std::string Result; - llvm::raw_string_ostream(Result) << llvm::format( - R"({"uri": %s, "range": %s})", URI::unparse(P.uri).c_str(), - Range::unparse(P.range).c_str()); - return Result; +json::Expr Location::unparse(const Location &P) { + return json::obj{ + {"uri", P.uri}, + {"range", P.range}, + }; } llvm::Optional @@ -279,12 +277,11 @@ return Result; } -std::string TextEdit::unparse(const TextEdit &P) { - std::string Result; - llvm::raw_string_ostream(Result) << llvm::format( - R"({"range": %s, "newText": "%s"})", Range::unparse(P.range).c_str(), - llvm::yaml::escape(P.newText).c_str()); - return Result; +json::Expr TextEdit::unparse(const TextEdit &P) { + return json::obj{ + {"range", P.range}, + {"newText", P.newText}, + }; } namespace { @@ -598,11 +595,11 @@ return Result; } -std::string FormattingOptions::unparse(const FormattingOptions &P) { - std::string Result; - llvm::raw_string_ostream(Result) << llvm::format( - R"({"tabSize": %d, "insertSpaces": %d})", P.tabSize, P.insertSpaces); - return Result; +json::Expr FormattingOptions::unparse(const FormattingOptions &P) { + return json::obj{ + {"tabSize", P.tabSize}, + {"insertSpaces", P.insertSpaces}, + }; } llvm::Optional @@ -880,96 +877,57 @@ return Result; } -std::string CompletionItem::unparse(const CompletionItem &CI) { - std::string Result = "{"; - llvm::raw_string_ostream Os(Result); +json::Expr CompletionItem::unparse(const CompletionItem &CI) { assert(!CI.label.empty() && "completion item label is required"); - Os << R"("label":")" << llvm::yaml::escape(CI.label) << R"(",)"; + json::obj Result{{"label", CI.label}}; if (CI.kind != CompletionItemKind::Missing) - Os << R"("kind":)" << static_cast(CI.kind) << R"(,)"; + Result["kind"] = static_cast(CI.kind); if (!CI.detail.empty()) - Os << R"("detail":")" << llvm::yaml::escape(CI.detail) << R"(",)"; + Result["detail"] = CI.detail; if (!CI.documentation.empty()) - Os << R"("documentation":")" << llvm::yaml::escape(CI.documentation) - << R"(",)"; + Result["documentation"] = CI.documentation; if (!CI.sortText.empty()) - Os << R"("sortText":")" << llvm::yaml::escape(CI.sortText) << R"(",)"; + Result["sortText"] = CI.sortText; if (!CI.filterText.empty()) - Os << R"("filterText":")" << llvm::yaml::escape(CI.filterText) << R"(",)"; + Result["filterText"] = CI.filterText; if (!CI.insertText.empty()) - Os << R"("insertText":")" << llvm::yaml::escape(CI.insertText) << R"(",)"; - if (CI.insertTextFormat != InsertTextFormat::Missing) { - Os << R"("insertTextFormat":)" << static_cast(CI.insertTextFormat) - << R"(,)"; - } + Result["insertText"] = CI.insertText; + if (CI.insertTextFormat != InsertTextFormat::Missing) + Result["insertTextFormat"] = static_cast(CI.insertTextFormat); if (CI.textEdit) - Os << R"("textEdit":)" << TextEdit::unparse(*CI.textEdit) << ','; - if (!CI.additionalTextEdits.empty()) { - Os << R"("additionalTextEdits":[)"; - for (const auto &Edit : CI.additionalTextEdits) - Os << TextEdit::unparse(Edit) << ","; - Os.flush(); - // The list additionalTextEdits is guaranteed nonempty at this point. - // Replace the trailing comma with right brace. - Result.back() = ']'; - } - Os.flush(); - // Label is required, so Result is guaranteed to have a trailing comma. - Result.back() = '}'; - return Result; + Result["textEdit"] = *CI.textEdit; + if (!CI.additionalTextEdits.empty()) + Result["additionalTextEdits"] = json::ary(CI.additionalTextEdits); + return std::move(Result); } -std::string ParameterInformation::unparse(const ParameterInformation &PI) { - std::string Result = "{"; - llvm::raw_string_ostream Os(Result); +json::Expr ParameterInformation::unparse(const ParameterInformation &PI) { assert(!PI.label.empty() && "parameter information label is required"); - Os << R"("label":")" << llvm::yaml::escape(PI.label) << '\"'; + json::obj Result{{"label", PI.label}}; if (!PI.documentation.empty()) - Os << R"(,"documentation":")" << llvm::yaml::escape(PI.documentation) - << '\"'; - Os << '}'; - Os.flush(); - return Result; + Result["documentation"] = PI.documentation; + return std::move(Result); } -std::string SignatureInformation::unparse(const SignatureInformation &SI) { - std::string Result = "{"; - llvm::raw_string_ostream Os(Result); +json::Expr SignatureInformation::unparse(const SignatureInformation &SI) { assert(!SI.label.empty() && "signature information label is required"); - Os << R"("label":")" << llvm::yaml::escape(SI.label) << '\"'; + json::obj Result{ + {"label", SI.label}, + {"parameters", json::ary(SI.parameters)}, + }; if (!SI.documentation.empty()) - Os << R"(,"documentation":")" << llvm::yaml::escape(SI.documentation) - << '\"'; - Os << R"(,"parameters":[)"; - for (const auto &Parameter : SI.parameters) { - Os << ParameterInformation::unparse(Parameter) << ','; - } - Os.flush(); - if (SI.parameters.empty()) - Result.push_back(']'); - else - Result.back() = ']'; // Replace the last `,` with an `]`. - Result.push_back('}'); - return Result; + Result["documentation"] = SI.documentation; + return std::move(Result); } -std::string SignatureHelp::unparse(const SignatureHelp &SH) { - std::string Result = "{"; - llvm::raw_string_ostream Os(Result); +json::Expr SignatureHelp::unparse(const SignatureHelp &SH) { assert(SH.activeSignature >= 0 && "Unexpected negative value for number of active signatures."); assert(SH.activeParameter >= 0 && "Unexpected negative value for active parameter index"); - Os << R"("activeSignature":)" << SH.activeSignature - << R"(,"activeParameter":)" << SH.activeParameter << R"(,"signatures":[)"; - for (const auto &Signature : SH.signatures) { - Os << SignatureInformation::unparse(Signature) << ','; - } - Os.flush(); - if (SH.signatures.empty()) - Result.push_back(']'); - else - Result.back() = ']'; // Replace the last `,` with an `]`. - Result.push_back('}'); - return Result; + return json::obj{ + {"activeSignature", SH.activeSignature}, + {"activeParameter", SH.activeParameter}, + {"signatures", json::ary(SH.signatures)}, + }; } Index: clangd/tool/ClangdMain.cpp =================================================================== --- clangd/tool/ClangdMain.cpp +++ clangd/tool/ClangdMain.cpp @@ -40,6 +40,10 @@ "Present snippet completions instead of plaintext completions"), llvm::cl::init(false)); +static llvm::cl::opt + PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"), + llvm::cl::init(false)); + static llvm::cl::opt RunSynchronously( "run-synchronously", llvm::cl::desc("Parse on main thread. If set, -j is ignored"), @@ -85,7 +89,8 @@ llvm::raw_ostream &Outs = llvm::outs(); llvm::raw_ostream &Logs = llvm::errs(); JSONOutput Out(Outs, Logs, - InputMirrorStream ? InputMirrorStream.getPointer() : nullptr); + InputMirrorStream ? InputMirrorStream.getPointer() : nullptr, + PrettyPrint); // If --compile-commands-dir arg was invoked, check value and override default // path. Index: test/clangd/initialize-params.test =================================================================== --- test/clangd/initialize-params.test +++ test/clangd/initialize-params.test @@ -1,26 +1,43 @@ -# RUN: clangd -run-synchronously < %s | FileCheck %s +# RUN: clangd -pretty -run-synchronously < %s | FileCheck -strict-whitespace %s # It is absolutely vital that this file has CRLF line endings. # # Test initialize request parameters with rootUri Content-Length: 143 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootUri":"file:///path/to/workspace","capabilities":{},"trace":"off"}} -# CHECK: Content-Length: 535 -# CHECK: {"jsonrpc":"2.0","id":0,"result":{"capabilities":{ -# CHECK: "textDocumentSync": 1, -# CHECK: "documentFormattingProvider": true, -# CHECK: "documentRangeFormattingProvider": true, -# CHECK: "documentOnTypeFormattingProvider": {"firstTriggerCharacter":"}","moreTriggerCharacter":[]}, -# CHECK: "codeActionProvider": true, -# CHECK: "completionProvider": {"resolveProvider": false, "triggerCharacters": [".",">",":"]}, -# CHECK: "signatureHelpProvider": {"triggerCharacters": ["(",","]}, -# CHECK: "definitionProvider": true -# CHECK: }}} -# +# CHECK: "id": 0, +# CHECK-NEXT: "jsonrpc": "2.0", +# CHECK-NEXT: "result": { +# CHECK-NEXT: "codeActionProvider": true, +# CHECK-NEXT: "completionProvider": { +# CHECK-NEXT: "resolveProvider": false, +# CHECK-NEXT: "triggerCharacters": [ +# CHECK-NEXT: ".", +# CHECK-NEXT: ">", +# CHECK-NEXT: ":" +# CHECK-NEXT: ] +# CHECK-NEXT: }, +# CHECK-NEXT: "definitionProvider": true, +# CHECK-NEXT: "documentFormattingProvider": true, +# CHECK-NEXT: "documentOnTypeFormattingProvider": { +# CHECK-NEXT: "firstTriggerCharacter": "}", +# CHECK-NEXT: "moreTriggerCharacter": null +# CHECK-NEXT: }, +# CHECK-NEXT: "documentRangeFormattingProvider": true, +# CHECK-NEXT: "signatureHelpProvider": { +# CHECK-NEXT: "triggerCharacters": [ +# CHECK-NEXT: "(", +# CHECK-NEXT: "," +# CHECK-NEXT: ] +# CHECK-NEXT: }, +# CHECK-NEXT: "textDocumentSync": 1 +# CHECK-NEXT: } Content-Length: 44 {"jsonrpc":"2.0","id":3,"method":"shutdown"} -# CHECK: {"jsonrpc":"2.0","id":3,"result":null} +# CHECK: "id": 3, +# CHECK-NEXT: "jsonrpc": "2.0", +# CHECK-NEXT: "result": null Content-Length: 33 {"jsonrpc":"2.0":"method":"exit"}