incoming_messages.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_LANGUAGE_SERVER_INCOMING_MESSAGES_H_
  5. #define CARBON_TOOLCHAIN_LANGUAGE_SERVER_INCOMING_MESSAGES_H_
  6. #include "clang-tools-extra/clangd/LSPBinder.h"
  7. #include "clang-tools-extra/clangd/Transport.h"
  8. #include "common/check.h"
  9. #include "common/map.h"
  10. #include "toolchain/language_server/context.h"
  11. namespace Carbon::LanguageServer {
  12. // Handles LSP messages from the client (IDE extension) by forwarding them to
  13. // `handlers_`.
  14. //
  15. // Handlers can return false to indicate server shutdown, although that's only
  16. // used for the `exit` notification.
  17. //
  18. // TODO: Consider adding multithreading support for calls.
  19. class IncomingMessages : public clang::clangd::Transport::MessageHandler {
  20. public:
  21. explicit IncomingMessages(clang::clangd::Transport* transport,
  22. Context* context);
  23. // Dispatches calls to the appropriate entry in `call_handlers_`. Always
  24. // returns true.
  25. auto onCall(llvm::StringRef name, llvm::json::Value params,
  26. llvm::json::Value id) -> bool override;
  27. // Dispatches notifications to the appropriate entry in
  28. // `notification_handlers_`, except for `exit` which directly returns false.
  29. auto onNotify(llvm::StringRef name, llvm::json::Value value) -> bool override;
  30. // Handles replies. Always returns true.
  31. auto onReply(llvm::json::Value /*id*/,
  32. llvm::Expected<llvm::json::Value> /*result*/) -> bool override;
  33. private:
  34. // These are the signatures expected for handlers.
  35. using CallHandler = std::function<
  36. auto(Context& context, llvm::json::Value raw_params,
  37. llvm::function_ref<auto(llvm::Expected<llvm::json::Value>)->void>
  38. on_done)
  39. ->void>;
  40. using NotificationHandler =
  41. std::function<auto(Context& context, llvm::json::Value raw_params)->void>;
  42. template <typename ParamsT, typename ResultT>
  43. auto AddCallHandler(
  44. llvm::StringRef name,
  45. auto (*handler)(Context&, const ParamsT&,
  46. llvm::function_ref<auto(llvm::Expected<ResultT>)->void>)
  47. ->void) -> void;
  48. template <typename ParamsT>
  49. auto AddNotificationHandler(llvm::StringRef name,
  50. auto (*handler)(Context&, const ParamsT&)->void)
  51. -> void;
  52. // The connection to the client.
  53. clang::clangd::Transport* transport_;
  54. // The context for handlers.
  55. Context* context_;
  56. // Handlers for LSP calls.
  57. Map<std::string, CallHandler> call_handlers_;
  58. // Handlers for LSP notifications.
  59. Map<std::string, NotificationHandler> notification_handlers_;
  60. };
  61. } // namespace Carbon::LanguageServer
  62. #endif // CARBON_TOOLCHAIN_LANGUAGE_SERVER_INCOMING_MESSAGES_H_