context.h 3.3 KB

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