context.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 tree_and_subtrees() const -> const Parse::TreeAndSubtrees& {
  30. return *tree_and_subtrees_;
  31. }
  32. private:
  33. // The filename, stable across instances.
  34. clang::clangd::URIForFile uri_;
  35. // Current file content, and derived values.
  36. std::unique_ptr<SourceBuffer> source_;
  37. std::unique_ptr<SharedValueStores> value_stores_;
  38. std::unique_ptr<Lex::TokenizedBuffer> tokens_;
  39. std::unique_ptr<Parse::Tree> tree_;
  40. std::unique_ptr<Parse::TreeAndSubtrees> tree_and_subtrees_;
  41. };
  42. // `vlog_stream` is optional; other parameters are required.
  43. explicit Context(llvm::raw_ostream* vlog_stream, DiagnosticConsumer* consumer,
  44. clang::clangd::LSPBinder::RawOutgoing* outgoing)
  45. : vlog_stream_(vlog_stream),
  46. file_emitter_(consumer),
  47. no_loc_emitter_(consumer),
  48. outgoing_(outgoing) {}
  49. // Returns a reference to the file if it's known, or diagnoses and returns
  50. // null.
  51. auto LookupFile(llvm::StringRef filename) -> File*;
  52. auto vlog_stream() -> llvm::raw_ostream* { return vlog_stream_; }
  53. auto file_emitter() -> FileDiagnosticEmitter& { return file_emitter_; }
  54. auto no_loc_emitter() -> NoLocDiagnosticEmitter& { return no_loc_emitter_; }
  55. auto outgoing() -> clang::clangd::LSPBinder::RawOutgoing& {
  56. return *outgoing_;
  57. }
  58. auto files() -> Map<std::string, File>& { return files_; }
  59. private:
  60. // Diagnostic and output streams.
  61. llvm::raw_ostream* vlog_stream_;
  62. FileDiagnosticEmitter file_emitter_;
  63. NoLocDiagnosticEmitter no_loc_emitter_;
  64. clang::clangd::LSPBinder::RawOutgoing* outgoing_;
  65. // Content of files managed by the language client.
  66. Map<std::string, File> files_;
  67. };
  68. } // namespace Carbon::LanguageServer
  69. #endif // CARBON_TOOLCHAIN_LANGUAGE_SERVER_CONTEXT_H_