#pragma once #include #include #include #include #include #include #include #include #include #include namespace vls::index { // Zero-based LSP-style character coordinates within one physical line. struct Location { std::string path; std::uint32_t line{}, character{}; std::uint32_t endLine{}, endCharacter{}; auto operator<=>(const Location &) const = default; }; enum class SymbolKind { unknown, function, type, variable, field, namespace_, module }; struct SymbolRecord { std::string name; SymbolKind kind{SymbolKind::unknown}; Location location; // declaration or definition std::optional definition; std::vector references; std::string provenance; // Compiler-provided call signature, when the observer reported one. std::optional signature; auto operator<=>(const SymbolRecord &) const = default; }; struct Limits { std::size_t contexts{64}; std::size_t records{65536}; std::size_t recordsPerContext{16384}; std::size_t referencesPerRecord{256}; std::size_t nameBytes{256}; std::size_t signatureBytes{320}; std::size_t pathBytes{4096}; std::size_t snapshotBytes{32 * 1024 * 1024}; }; struct ContextInput { std::string context; // Build::ContextKey alternative std::vector dependencies; // Path and content identity of every source this context consumed. std::vector> sources; std::vector records; bool complete{true}; std::string reason; }; struct Coverage { bool indexed{}; // at least one context or overlay was considered bool complete{}; std::vector incomplete; // context names, sorted auto operator<=>(const Coverage &) const = default; }; struct Query { std::string name; bool substring{}; std::optional context; std::size_t limit{100}; }; struct Result { std::vector symbols; Coverage coverage; bool truncated{}; }; // Immutable published snapshot. Readers keep it alive across later publications // and evictions. class Generation { public: [[nodiscard]] std::uint64_t number() const noexcept { return number_; } [[nodiscard]] const std::vector &symbols() const noexcept { return records_; } [[nodiscard]] const std::vector> &contexts() const noexcept { return contexts_; } private: friend class SymbolIndex; Generation(std::uint64_t number, std::vector records, std::vector> contexts) : number_(number), records_(std::move(records)), contexts_(std::move(contexts)) {} std::uint64_t number_; std::vector records_; std::vector> contexts_; }; // Context-aware, bounded symbol index. Committed records belong to explicit // analysis contexts; unsaved document overlays shadow committed records for // the same path and are reconciled on save/close. A changed, deleted or // renamed source marks dependent contexts incomplete without discarding the // last-good records (degraded navigation, never an authoritative rename). class SymbolIndex { public: explicit SymbolIndex(Limits = Limits{}); ~SymbolIndex(); SymbolIndex(const SymbolIndex &) = delete; SymbolIndex &operator=(const SymbolIndex &) = delete; // Atomically replaces one context and publishes a new generation. [[nodiscard]] bool publish(const ContextInput &); [[nodiscard]] bool remove(std::string_view context); // Unsaved document records. The newest overlay for a path shadows the // committed records of that path until it is cleared. [[nodiscard]] bool set_overlay(std::string path, std::string content, std::vector records); [[nodiscard]] bool clear_overlay(std::string_view path); // A new content (or absence) invalidates every context that consumed the // path. Matching content is a no-op. [[nodiscard]] std::size_t invalidate(std::string_view path, const std::optional &content); [[nodiscard]] Result find(const Query &) const; // Records whose use location is in PATH, with the same overlay shadowing, // deduplication and coverage reporting as find(). [[nodiscard]] Result find_by_path(std::string_view path, std::size_t limit = 4096) const; [[nodiscard]] Coverage coverage() const; [[nodiscard]] std::shared_ptr current() const; [[nodiscard]] std::uint64_t generation() const; [[nodiscard]] std::size_t records() const; [[nodiscard]] const Limits &limits() const noexcept; // Versioned persistence. restore() classifies the snapshot; incompatible or // corrupt bytes are quarantined and the index rebuilds into a new generation. enum class LoadStatus { loaded, missing, incompatible, corrupt, oversized }; struct LoadResult { LoadStatus status{LoadStatus::missing}; std::uint64_t generation{}; std::optional quarantine; // True when a schema-1 snapshot was accepted and upgraded in memory. bool migrated{}; }; // Writes the versioned snapshot (schema 2 with a canonical payload digest). // restore() also accepts schema 1 and reports the upgrade as migrated. [[nodiscard]] bool save(const std::filesystem::path &) const; [[nodiscard]] std::expected restore(const std::filesystem::path &); // True only when a compatible snapshot was loaded. [[nodiscard]] bool load(const std::filesystem::path &); // Generation-directory backend: each save keeps the previous generation for // crash recovery, CURRENT selects the newest, and a legacy index.json is // migrated on the first restore. [[nodiscard]] bool save_generation(const std::filesystem::path &); [[nodiscard]] std::expected restore_generation(const std::filesystem::path &); private: struct State; std::shared_ptr state_; }; } // namespace vls::index