// Part of the Carbon Language project, under the Apache License v2.0 with LLVM // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #ifndef CARBON_TOOLCHAIN_LANGUAGE_SERVER_CONTEXT_H_ #define CARBON_TOOLCHAIN_LANGUAGE_SERVER_CONTEXT_H_ #include #include #include "clang-tools-extra/clangd/LSPBinder.h" #include "common/map.h" #include "toolchain/base/shared_value_stores.h" #include "toolchain/diagnostics/diagnostic_consumer.h" #include "toolchain/diagnostics/diagnostic_emitter.h" #include "toolchain/diagnostics/file_diagnostics.h" #include "toolchain/lex/tokenized_buffer.h" #include "toolchain/parse/tree_and_subtrees.h" #include "toolchain/sem_ir/file.h" #include "toolchain/source/source_buffer.h" namespace Carbon::LanguageServer { // Context for LSP call handling. class Context { public: // Cached information for an open file. class File { public: explicit File(clang::clangd::URIForFile uri) : uri_(std::move(uri)) {} // Changes the file's text, updating dependent state. auto SetText(Context& context, std::optional version, llvm::StringRef text) -> void; auto tree_and_subtrees() const -> const Parse::TreeAndSubtrees& { return *tree_and_subtrees_; } private: // The filename, stable across instances. clang::clangd::URIForFile uri_; // Current file content, and derived values. std::unique_ptr source_; std::unique_ptr value_stores_; std::unique_ptr tokens_; std::unique_ptr tree_; std::unique_ptr tree_and_subtrees_; }; // `vlog_stream` is optional; other parameters are required. explicit Context(llvm::raw_ostream* vlog_stream, DiagnosticConsumer* consumer, clang::clangd::LSPBinder::RawOutgoing* outgoing) : vlog_stream_(vlog_stream), file_emitter_(consumer), no_loc_emitter_(consumer), outgoing_(outgoing) {} // Returns a reference to the file if it's known, or diagnoses and returns // null. auto LookupFile(llvm::StringRef filename) -> File*; auto vlog_stream() -> llvm::raw_ostream* { return vlog_stream_; } auto file_emitter() -> FileDiagnosticEmitter& { return file_emitter_; } auto no_loc_emitter() -> NoLocDiagnosticEmitter& { return no_loc_emitter_; } auto outgoing() -> clang::clangd::LSPBinder::RawOutgoing& { return *outgoing_; } auto files() -> Map& { return files_; } private: // Diagnostic and output streams. llvm::raw_ostream* vlog_stream_; FileDiagnosticEmitter file_emitter_; NoLocDiagnosticEmitter no_loc_emitter_; clang::clangd::LSPBinder::RawOutgoing* outgoing_; // Content of files managed by the language client. Map files_; }; } // namespace Carbon::LanguageServer #endif // CARBON_TOOLCHAIN_LANGUAGE_SERVER_CONTEXT_H_