context.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, DiagnosticConsumer* consumer,
  45. clang::clangd::LSPBinder::RawOutgoing* outgoing)
  46. : vlog_stream_(vlog_stream),
  47. file_emitter_(consumer),
  48. no_loc_emitter_(consumer),
  49. outgoing_(outgoing) {}
  50. // Returns a reference to the file if it's known, or diagnoses and returns
  51. // null.
  52. auto LookupFile(llvm::StringRef filename) -> File*;
  53. // Wrapper for LSP notification.
  54. auto PublishDiagnostics(clang::clangd::PublishDiagnosticsParams params)
  55. -> void {
  56. outgoing_->notify("textDocument/publishDiagnostics", params);
  57. }
  58. auto vlog_stream() -> llvm::raw_ostream* { return vlog_stream_; }
  59. auto file_emitter() -> FileDiagnosticEmitter& { return file_emitter_; }
  60. auto no_loc_emitter() -> NoLocDiagnosticEmitter& { return no_loc_emitter_; }
  61. auto files() -> Map<std::string, File>& { return files_; }
  62. private:
  63. // Diagnostic and output streams.
  64. llvm::raw_ostream* vlog_stream_;
  65. FileDiagnosticEmitter file_emitter_;
  66. NoLocDiagnosticEmitter no_loc_emitter_;
  67. clang::clangd::LSPBinder::RawOutgoing* outgoing_;
  68. // Content of files managed by the language client.
  69. Map<std::string, File> files_;
  70. };
  71. } // namespace Carbon::LanguageServer
  72. #endif // CARBON_TOOLCHAIN_LANGUAGE_SERVER_CONTEXT_H_