server.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_SERVER_H_
  5. #define CARBON_TOOLCHAIN_LANGUAGE_SERVER_SERVER_H_
  6. #include <memory>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include "clang-tools-extra/clangd/LSPBinder.h"
  10. #include "clang-tools-extra/clangd/Protocol.h"
  11. #include "clang-tools-extra/clangd/Transport.h"
  12. #include "common/error.h"
  13. namespace Carbon::LanguageServer {
  14. // Provides a LSP implementation for Carbon.
  15. class Server : public clang::clangd::Transport::MessageHandler,
  16. public clang::clangd::LSPBinder::RawOutgoing {
  17. public:
  18. // Prepares the server to run on the provided streams.
  19. explicit Server(std::FILE* input_stream, llvm::raw_ostream& output_stream);
  20. // Runs the server in a loop, returning the result. Currently this always
  21. // returns an error when the input stream is closed.
  22. auto Run() -> ErrorOr<Success>;
  23. // Transport::MessageHandler
  24. // Handlers returns true to keep processing messages, or false to shut down.
  25. // Handler called on notification by client.
  26. auto onNotify(llvm::StringRef method, llvm::json::Value value)
  27. -> bool override;
  28. // Handler called on method call by client.
  29. auto onCall(llvm::StringRef method, llvm::json::Value params,
  30. llvm::json::Value id) -> bool override;
  31. // Handler called on response of Transport::call.
  32. auto onReply(llvm::json::Value id, llvm::Expected<llvm::json::Value> result)
  33. -> bool override;
  34. // LSPBinder::RawOutgoing
  35. // Send method call to client
  36. auto callMethod(llvm::StringRef method, llvm::json::Value params,
  37. clang::clangd::Callback<llvm::json::Value> reply)
  38. -> void override {
  39. // TODO: Implement when needed.
  40. }
  41. // Send notification to client
  42. auto notify(llvm::StringRef method, llvm::json::Value params)
  43. -> void override {
  44. transport_->notify(method, params);
  45. }
  46. private:
  47. // Typed handlers for notifications and method calls by client.
  48. // Client opened a document.
  49. auto OnDidOpenTextDocument(
  50. clang::clangd::DidOpenTextDocumentParams const& params) -> void;
  51. // Client updated content of a document.
  52. auto OnDidChangeTextDocument(
  53. clang::clangd::DidChangeTextDocumentParams const& params) -> void;
  54. // Capabilities negotiation
  55. auto OnInitialize(clang::clangd::NoParams const& client_capabilities,
  56. clang::clangd::Callback<llvm::json::Object> cb) -> void;
  57. // Code outline
  58. auto OnDocumentSymbol(
  59. clang::clangd::DocumentSymbolParams const& params,
  60. clang::clangd::Callback<std::vector<clang::clangd::DocumentSymbol>> cb)
  61. -> void;
  62. const std::unique_ptr<clang::clangd::Transport> transport_;
  63. // content of files managed by the language client.
  64. std::unordered_map<std::string, std::string> files_;
  65. // handlers for client methods and notifications
  66. clang::clangd::LSPBinder::RawHandlers handlers_;
  67. // Binds client calls to member methods.
  68. clang::clangd::LSPBinder binder_;
  69. };
  70. } // namespace Carbon::LanguageServer
  71. #endif // CARBON_TOOLCHAIN_LANGUAGE_SERVER_SERVER_H_