context.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LANGUAGE_SERVER_CONTEXT_H_
  6. #include <memory>
  7. #include <string>
  8. #include "clang-tools-extra/clangd/LSPBinder.h"
  9. #include "common/map.h"
  10. #include "toolchain/base/shared_value_stores.h"
  11. #include "toolchain/diagnostics/diagnostic_consumer.h"
  12. #include "toolchain/diagnostics/diagnostic_emitter.h"
  13. #include "toolchain/diagnostics/file_diagnostics.h"
  14. #include "toolchain/lex/tokenized_buffer.h"
  15. #include "toolchain/parse/tree_and_subtrees.h"
  16. #include "toolchain/sem_ir/file.h"
  17. #include "toolchain/source/source_buffer.h"
  18. namespace Carbon::LanguageServer {
  19. // Context for LSP call handling.
  20. class Context {
  21. public:
  22. // Cached information for an open file.
  23. class File {
  24. public:
  25. explicit File(clang::clangd::URIForFile uri) : uri_(std::move(uri)) {}
  26. // Changes the file's text, updating dependent state.
  27. auto SetText(Context& context, std::optional<int64_t> version,
  28. llvm::StringRef text) -> void;
  29. auto text() const -> llvm::StringRef { return source_->text(); }
  30. auto tree_and_subtrees() const -> const Parse::TreeAndSubtrees& {
  31. return *tree_and_subtrees_;
  32. }
  33. private:
  34. // The filename, stable across instances.
  35. clang::clangd::URIForFile uri_;
  36. // Current file content, and derived values.
  37. std::unique_ptr<SourceBuffer> source_;
  38. std::unique_ptr<SharedValueStores> value_stores_;
  39. std::unique_ptr<Lex::TokenizedBuffer> tokens_;
  40. std::unique_ptr<Parse::Tree> tree_;
  41. std::unique_ptr<Parse::TreeAndSubtrees> tree_and_subtrees_;
  42. };
  43. // `vlog_stream` is optional; other parameters are required.
  44. explicit Context(llvm::raw_ostream* vlog_stream,
  45. Diagnostics::Consumer* consumer,
  46. clang::clangd::LSPBinder::RawOutgoing* outgoing)
  47. : vlog_stream_(vlog_stream),
  48. file_emitter_(consumer),
  49. no_loc_emitter_(consumer),
  50. outgoing_(outgoing) {}
  51. // Returns a reference to the file if it's known, or diagnoses and returns
  52. // null.
  53. auto LookupFile(llvm::StringRef filename) -> File*;
  54. // Wrapper for LSP notification.
  55. auto PublishDiagnostics(clang::clangd::PublishDiagnosticsParams params)
  56. -> void {
  57. outgoing_->notify("textDocument/publishDiagnostics", params);
  58. }
  59. auto vlog_stream() -> llvm::raw_ostream* { return vlog_stream_; }
  60. auto file_emitter() -> Diagnostics::FileEmitter& { return file_emitter_; }
  61. auto no_loc_emitter() -> Diagnostics::NoLocEmitter& {
  62. return no_loc_emitter_;
  63. }
  64. auto files() -> Map<std::string, File>& { return files_; }
  65. private:
  66. // Diagnostic and output streams.
  67. llvm::raw_ostream* vlog_stream_;
  68. Diagnostics::FileEmitter file_emitter_;
  69. Diagnostics::NoLocEmitter no_loc_emitter_;
  70. clang::clangd::LSPBinder::RawOutgoing* outgoing_;
  71. // Content of files managed by the language client.
  72. Map<std::string, File> files_;
  73. };
  74. } // namespace Carbon::LanguageServer
  75. #endif // CARBON_TOOLCHAIN_LANGUAGE_SERVER_CONTEXT_H_