incoming_messages.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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<void(
  36. Context& context, llvm::json::Value raw_params,
  37. llvm::function_ref<void(llvm::Expected<llvm::json::Value>)> on_done)>;
  38. using NotificationHandler =
  39. std::function<void(Context& context, llvm::json::Value raw_params)>;
  40. template <typename ParamsT, typename ResultT>
  41. auto AddCallHandler(
  42. llvm::StringRef name,
  43. void (*handler)(Context&, const ParamsT&,
  44. llvm::function_ref<void(llvm::Expected<ResultT>)>))
  45. -> void;
  46. template <typename ParamsT>
  47. auto AddNotificationHandler(llvm::StringRef name,
  48. void (*handler)(Context&, const ParamsT&))
  49. -> void;
  50. // The connection to the client.
  51. clang::clangd::Transport* transport_;
  52. // The context for handlers.
  53. Context* context_;
  54. // Handlers for LSP calls.
  55. Map<std::string, CallHandler> call_handlers_;
  56. // Handlers for LSP notifications.
  57. Map<std::string, NotificationHandler> notification_handlers_;
  58. };
  59. } // namespace Carbon::LanguageServer
  60. #endif // CARBON_TOOLCHAIN_LANGUAGE_SERVER_INCOMING_MESSAGES_H_