summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/JSONRPCDispatcher.h
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2017-02-07 10:28:20 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2017-02-07 10:28:20 +0000
commitbb1cdb63e5b1dfdb44ccb1f70b9d203138abec9a (patch)
tree1c91a61cd4f099499cef0a704e8df9c111c38285 /clang-tools-extra/clangd/JSONRPCDispatcher.h
parentb2b70975e03db7644a07df65139c49fa004e2e11 (diff)
downloadbcm5719-llvm-bb1cdb63e5b1dfdb44ccb1f70b9d203138abec9a.tar.gz
bcm5719-llvm-bb1cdb63e5b1dfdb44ccb1f70b9d203138abec9a.zip
Add a prototype for clangd
clangd is a language server protocol implementation based on clang. It's supposed to provide editor integration while not suffering from the confined ABI of libclang. This implementation is limited to the bare minimum functionality of doing (whole-document) formatting and rangeFormatting. The JSON parsing is based on LLVM's YAMLParser but yet most of the code of clangd is currently dealing with JSON serialization and deserialization. This was only tested with VS Code so far, mileage with other LSP clients may vary. Differential Revision: https://reviews.llvm.org/D29451 llvm-svn: 294291
Diffstat (limited to 'clang-tools-extra/clangd/JSONRPCDispatcher.h')
-rw-r--r--clang-tools-extra/clangd/JSONRPCDispatcher.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/JSONRPCDispatcher.h b/clang-tools-extra/clangd/JSONRPCDispatcher.h
new file mode 100644
index 00000000000..c27ebaf21d8
--- /dev/null
+++ b/clang-tools-extra/clangd/JSONRPCDispatcher.h
@@ -0,0 +1,66 @@
+//===--- JSONRPCDispatcher.h - Main JSON parser entry point -----*- 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_JSONRPCDISPATCHER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_JSONRPCDISPATCHER_H
+
+#include "clang/Basic/LLVM.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/Support/YAMLParser.h"
+
+namespace clang {
+namespace clangd {
+
+/// Callback for messages sent to the server, called by the JSONRPCDispatcher.
+class Handler {
+public:
+ Handler(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs)
+ : Outs(Outs), Logs(Logs) {}
+ virtual ~Handler() = default;
+
+ /// Called when the server receives a method call. This is supposed to return
+ /// a result on Outs. The default implementation returns an "unknown method"
+ /// error to the client and logs a warning.
+ virtual void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID);
+ /// Called when the server receives a notification. No result should be
+ /// written to Outs. The default implemetation logs a warning.
+ virtual void handleNotification(llvm::yaml::MappingNode *Params);
+
+protected:
+ llvm::raw_ostream &Outs;
+ llvm::raw_ostream &Logs;
+
+ /// Helper to write a JSONRPC result to Outs.
+ void writeMessage(const Twine &Message);
+};
+
+/// Main JSONRPC entry point. This parses the JSONRPC "header" and calls the
+/// registered Handler for the method received.
+class JSONRPCDispatcher {
+public:
+ /// Create a new JSONRPCDispatcher. UnknownHandler is called when an unknown
+ /// method is received.
+ JSONRPCDispatcher(std::unique_ptr<Handler> UnknownHandler)
+ : UnknownHandler(std::move(UnknownHandler)) {}
+
+ /// Registers a Handler for the specified Method.
+ void registerHandler(StringRef Method, std::unique_ptr<Handler> H);
+
+ /// Parses a JSONRPC message and calls the Handler for it.
+ bool call(StringRef Content) const;
+
+private:
+ llvm::StringMap<std::unique_ptr<Handler>> Handlers;
+ std::unique_ptr<Handler> UnknownHandler;
+};
+
+} // namespace clangd
+} // namespace clang
+
+#endif
OpenPOWER on IntegriCloud