language_server.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. #include "toolchain/language_server/language_server.h"
  5. #include "clang-tools-extra/clangd/LSPBinder.h"
  6. #include "clang-tools-extra/clangd/Transport.h"
  7. #include "common/raw_string_ostream.h"
  8. #include "toolchain/language_server/context.h"
  9. #include "toolchain/language_server/incoming_messages.h"
  10. #include "toolchain/language_server/outgoing_messages.h"
  11. namespace Carbon::LanguageServer {
  12. auto Run(std::FILE* input_stream, llvm::raw_ostream& output_stream)
  13. -> ErrorOr<Success> {
  14. // Set up the connection.
  15. std::unique_ptr<clang::clangd::Transport> transport(
  16. clang::clangd::newJSONTransport(input_stream, output_stream,
  17. /*InMirror=*/nullptr,
  18. /*Pretty=*/true));
  19. Context context;
  20. IncomingMessages incoming(transport.get(), &context);
  21. OutgoingMessages outgoing(transport.get());
  22. // Run the server loop.
  23. llvm::Error err = transport->loop(incoming);
  24. if (err) {
  25. RawStringOstream out;
  26. out << err;
  27. return Error(out.TakeStr());
  28. } else {
  29. return Success();
  30. }
  31. }
  32. } // namespace Carbon::LanguageServer