From 8ca3dcbf748ef1291c2e3f89cf6095b1c71d032d Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 14:21:26 +0300 Subject: [PATCH] Serve document symbols and highlights over LSP SymbolIndex::find_by_path exposes one document's records with the same overlay shadowing and coverage as find. EditorDiagnostics maps them to textDocument/documentSymbol, and a bounded same-file highlight core (vls/features/DocumentSymbols.hpp) backs textDocument/documentHighlight from verified occurrence facts with the ambiguous-binding rule. Both are advertised and exercised over a real stdio session with the stock observer. Record the LSP parity matrix. --- CMakeLists.txt | 2 +- build.cpp | 8 ++- docs/agent-workflow/LSP-PARITY.md | 43 ++++++++++++ docs/agent-workflow/STATE.md | 2 +- docs/agent-workflow/TASKS.md | 1 + docs/agent-workflow/VERIFICATION.md | 20 ++++++ include/vls/features/DocumentSymbols.hpp | 49 +++++++++++++ include/vls/index/SymbolIndex.hpp | 3 + include/vls/semantic/EditorDiagnostics.hpp | 5 ++ src/Server.cpp | 18 ++++- src/features/DocumentSymbols.cpp | 29 ++++++++ src/index/SymbolIndex.cpp | 34 +++++++++ src/semantic/EditorDiagnostics.cpp | 80 ++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- tests/features/DocumentSymbolsTests.cpp | 61 +++++++++++++++++ tests/index/SymbolIndexTests.cpp | 17 +++++ 16 files changed, 366 insertions(+), 8 deletions(-) create mode 100644 docs/agent-workflow/LSP-PARITY.md create mode 100644 include/vls/features/DocumentSymbols.hpp create mode 100644 src/features/DocumentSymbols.cpp create mode 100644 tests/features/DocumentSymbolsTests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ae0ba2f..4cb75ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ add_library(vls_protocol STATIC src/Protocol/Framing.cpp src/Protocol/Json.cpp src/Build/ProviderFile.cpp src/Build/CommandImport.cpp src/Build/VbsProvider.cpp src/Build/CompdbProvider.cpp src/DocumentSync.cpp src/WorkspaceSession.cpp src/Server.cpp src/semantic/Diagnostics.cpp src/semantic/SourceCatalog.cpp src/semantic/StockCompiler.cpp src/semantic/EditorDiagnostics.cpp src/semantic/Occurrences.cpp src/scheduler/Scheduler.cpp - src/modules/ModuleGraph.cpp src/modules/ArtifactCache.cpp src/modules/GccArtifacts.cpp src/modules/ScheduledArtifacts.cpp src/index/SymbolIndex.cpp src/navigation/Navigation.cpp src/reflection/Provenance.cpp src/features/Completion.cpp src/features/IndexNavigation.cpp src/features/SignatureHelp.cpp src/debug/live/LiveSession.cpp src/modules/ModuleWorkQueue.cpp src/modules/GccScan.cpp src/modules/ScanCommand.cpp) + src/modules/ModuleGraph.cpp src/modules/ArtifactCache.cpp src/modules/GccArtifacts.cpp src/modules/ScheduledArtifacts.cpp src/index/SymbolIndex.cpp src/navigation/Navigation.cpp src/reflection/Provenance.cpp src/features/Completion.cpp src/features/IndexNavigation.cpp src/features/SignatureHelp.cpp src/features/DocumentSymbols.cpp src/debug/live/LiveSession.cpp src/modules/ModuleWorkQueue.cpp src/modules/GccScan.cpp src/modules/ScanCommand.cpp) target_include_directories(vls_protocol PUBLIC "${PROJECT_SOURCE_DIR}/include") target_link_libraries(vls_protocol PUBLIC Void::Data PRIVATE Void::Unicode Void::Text Void::Crypto Void::System) target_compile_options(vls_protocol PUBLIC -freflection PRIVATE -Wall -Wextra -Wpedantic) diff --git a/build.cpp b/build.cpp index 48b0233..b27557f 100644 --- a/build.cpp +++ b/build.cpp @@ -69,6 +69,7 @@ void build(Void::Build::Project &project) { "src/features/Completion.cpp", "src/features/IndexNavigation.cpp", "src/features/SignatureHelp.cpp", + "src/features/DocumentSymbols.cpp", "src/debug/live/LiveSession.cpp", "src/modules/ModuleWorkQueue.cpp", "src/modules/GccScan.cpp", @@ -144,9 +145,10 @@ void build(Void::Build::Project &project) { auto index_tests = suite("vls_index_tests", {"tests/index/SymbolIndexTests.cpp"}); auto navigation_tests = suite("vls_navigation_tests", {"tests/navigation/NavigationTests.cpp"}); auto reflection_tests = suite("vls_reflection_tests", {"tests/reflection/ProvenanceTests.cpp"}); - auto feature_tests = suite("vls_feature_tests", {"tests/features/CompletionTests.cpp", - "tests/features/IndexNavigationTests.cpp", - "tests/features/SignatureHelpTests.cpp"}); + auto feature_tests = + suite("vls_feature_tests", + {"tests/features/CompletionTests.cpp", "tests/features/IndexNavigationTests.cpp", + "tests/features/SignatureHelpTests.cpp", "tests/features/DocumentSymbolsTests.cpp"}); auto live_worker = project.executable("vls_live_worker_fixture"); live_worker.cxx_standard(CxxStandard::Cxx26) .sources({"tests/debug/LiveWorkerFixture.cpp"}) diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md new file mode 100644 index 0000000..be7d78b --- /dev/null +++ b/docs/agent-workflow/LSP-PARITY.md @@ -0,0 +1,43 @@ +# LSP parity matrix + +Target (user direction): clangd feature parity plus every LSP capability that +applies to a C++ language server. Status is evidence-based; `PARTIAL` names the +missing part. + +## Implemented and verified + +| Capability | Evidence | +| --- | --- | +| initialize / initialized / shutdown / exit | native transcripts and registered tests | +| textDocument sync (full + incremental, UTF-8/UTF-16) | document suite, real stdio corpora | +| publishDiagnostics (stock GCC, in-process default profile) | diagnostic suite, stdio corpus | +| textDocument/hover, definition | stock occurrence corpus | +| textDocument/references, prepareRename, rename | navigation suite, stock corpus refusals | +| textDocument/completion (+ completionItem/resolve) | `vls_feature_tests`, real stdio session | +| textDocument/signatureHelp | `vls_feature_tests`, real stdio session | +| textDocument/documentSymbol | `vls_index_tests` path query, real stdio session | +| textDocument/documentHighlight | `vls_feature_tests`, real stdio session | +| workspace/symbol | stock occurrence corpus | +| voidLanguageServer/analysisStatus, providerStatus | stdio corpora | +| Debug adapter (DAP) separately through `vls-dap` | `vls_live_tests`, `vls_dap_tests` | + +## Open + +| Capability | Notes | +| --- | --- | +| Compiler-native completion with visibility proof | index-backed completion is explicitly incomplete | +| Overload-set signature help | only the selected occurrence's signature is available | +| textDocument/selectionRange, foldingRange | straightforward next slices | +| textDocument/codeAction, formatting | compiler-backed; needs a plan | +| textDocument/semanticTokens, inlayHint | larger +| textDocument/codeLens, executeCommand | command surface not designed | +| callHierarchy / typeHierarchy | needs compiler call/type relationships | +| workspace edits and file operations | client-side application is also open | +| Applying multi-file renames through client buffers | plans can be ready; application is client work | +| `EditorDiagnostics` reflection LSP wiring | the server has no reflection data source | + +## Next slices + +1. selectionRange and foldingRange from document snapshots. +2. codeAction quick fixes from published diagnostics. +3. Semantic tokens from stock occurrences. diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index 70c630d..cad8f23 100644 --- a/docs/agent-workflow/STATE.md +++ b/docs/agent-workflow/STATE.md @@ -102,7 +102,7 @@ ingests occurrence facts after each analysis and the server answers Hash.hpp definition through it. Real persistence migrations and cross-file rename coverage remain open. -Index-backed completion (roadmap 14 first slice) is implemented: `include/vls/features/Completion.hpp`, `src/features/Completion.cpp` and registered `vls_feature_tests` cover prefix extraction, filtering with replacement ranges, label+kind deduplication, deterministic ranking and bounds. `Server.cpp` advertises `completionProvider` and answers `textDocument/completion` from the symbol index through `EditorDiagnostics::completion`; a real stdio session with the stock observer returned `fnv1a64` and `fnv1a64_update` at a real call site. Every result is marked incomplete because the index does not prove visibility. Signature help is now served from the compiler-provided occurrence signature (parameter labels and ranges, active parameter, innermost call selection) and advertised by the server. Compiler-native completion with visibility proof and deferred detail resolution remain open. +Index-backed completion (roadmap 14 first slice) is implemented: `include/vls/features/Completion.hpp`, `src/features/Completion.cpp` and registered `vls_feature_tests` cover prefix extraction, filtering with replacement ranges, label+kind deduplication, deterministic ranking and bounds. `Server.cpp` advertises `completionProvider` and answers `textDocument/completion` from the symbol index through `EditorDiagnostics::completion`; a real stdio session with the stock observer returned `fnv1a64` and `fnv1a64_update` at a real call site. Every result is marked incomplete because the index does not prove visibility. Signature help is now served from the compiler-provided occurrence signature (parameter labels and ranges, active parameter, innermost call selection) and advertised by the server. Document symbols are served from the index by path and document highlights from same-binding occurrence facts; both are advertised and were exercised over stdio with the stock observer. Compiler-native completion with visibility proof and deferred detail resolution remain open. The reflection provenance representation (roadmap 12 first slice) is implemented as a bounded library with a registered suite: innermost-first diff --git a/docs/agent-workflow/TASKS.md b/docs/agent-workflow/TASKS.md index 6ebd321..6ed61bb 100644 --- a/docs/agent-workflow/TASKS.md +++ b/docs/agent-workflow/TASKS.md @@ -66,6 +66,7 @@ The original writer released the foundation files. Current ownership follows. | T-PLAN-EDITORS-022 | COMPLETE | Root | Verified Neovim 0.12.6, nvim-dap cfa2d58, VS Code 1.147.0, Node 24.22.3. Two ownership-ready contracts: [T-CLIENT-NEOVIM-023](tasks/T-CLIENT-NEOVIM-023.md) and [T-CLIENT-VSCODE-022](tasks/T-CLIENT-VSCODE-022.md). | | T-CLIENT-NEOVIM-023 | IMPLEMENTED_TESTED | Root | `clients/neovim/` and `tests/native/NeovimClient.lua` passed with the installed server, adapter and nvim-dap. VS Code client remains. | | T-CLIENT-VSCODE-022 | IMPLEMENTED_TESTED | Root | `clients/vscode/` and `tests/native/VscodeClient.py` passed in a disposable extension host with VS Code 1.147.0. | +| T-LSP-PARITY-014 | PARTIAL | Root | Parity program (user direction: clangd parity plus the full applicable LSP surface). This slice: `textDocument/documentSymbol` from `SymbolIndex::find_by_path`, `textDocument/documentHighlight` from registered `vls_feature_tests` core, both advertised and exercised over stdio. Open: selectionRange, foldingRange, code actions, formatting, semantic tokens, inlay hints, call/type hierarchy, code lens, executeCommand, workspace edits/file operations, compiler-native completion, overload signature help, client-side multi-file rename application. [Matrix](LSP-PARITY.md). | Per D-027 the generic runtime uses an explicit GPT-6.2 model and Max for each assignment. The suggested Sol implementation assignment follows planner/preflight. diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index da2dd6f..516f1b1 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -79,6 +79,26 @@ the upgraded snapshot (keeping `snapshot.prev.json`) and rewrites a legacy about 25-30s in a Debug build (it passed VBS at ~27s); the full CMake run then passed 18/18. +## December 5 document symbols and document highlights + +`SymbolIndex::find_by_path` returns the records whose use location is in one +path with the same overlay shadowing, deduplication, coverage and truncation as +`find` (registered `vls_index_tests` case +`finds_records_by_path_with_overlay_shadowing`). `EditorDiagnostics` maps it to +`textDocument/documentSymbol` and advertises `documentSymbolProvider`. +`include/vls/features/DocumentSymbols.hpp` adds a bounded, same-file highlight +core (`highlights`), used by `textDocument/documentHighlight` from the verified +occurrence facts with the ambiguous-binding rule; the server advertises +`documentHighlightProvider`. + +A real stdio session with the stock observer on `DocumentSnapshot.cpp`: +``` +documentSymbol: 1 first: ['fnv1a64'] +documentHighlight: [(18, 33, 1)] +``` +All 18 native suites and all 18 CMake tests passed. The outline currently +contains only recorded uses (the observer reports calls), not declarations. + ## December 5 signature help from stock occurrence facts `include/vls/features/SignatureHelp.hpp` and diff --git a/include/vls/features/DocumentSymbols.hpp b/include/vls/features/DocumentSymbols.hpp new file mode 100644 index 0000000..50bd72f --- /dev/null +++ b/include/vls/features/DocumentSymbols.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace vls::features::documents { + +struct Position { + std::uint32_t line{}, character{}; + auto operator<=>(const Position &) const = default; +}; + +struct Range { + Position start, end; + auto operator<=>(const Range &) const = default; +}; + +struct Location { + std::string path; + Range range; + auto operator<=>(const Location &) const = default; +}; + +// Semantic identity of one binding, joined by definition like navigation. +struct Binding { + std::string name; + std::optional definition; + auto operator<=>(const Binding &) const = default; +}; + +struct Use { + Binding binding; + std::string path; + Range range; +}; + +// Ranges of every proven use of the binding at the position, restricted to +// the same file, deduplicated and bounded. An empty result means no unique +// binding covers the position. +[[nodiscard]] std::vector highlights(std::span, std::string_view path, Position, + std::size_t limit = 512); + +} // namespace vls::features::documents diff --git a/include/vls/index/SymbolIndex.hpp b/include/vls/index/SymbolIndex.hpp index dea382a..7968138 100644 --- a/include/vls/index/SymbolIndex.hpp +++ b/include/vls/index/SymbolIndex.hpp @@ -121,6 +121,9 @@ class SymbolIndex { 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; diff --git a/include/vls/semantic/EditorDiagnostics.hpp b/include/vls/semantic/EditorDiagnostics.hpp index 10a9089..62131e7 100644 --- a/include/vls/semantic/EditorDiagnostics.hpp +++ b/include/vls/semantic/EditorDiagnostics.hpp @@ -37,6 +37,11 @@ class EditorDiagnostics { // without details rather than details for a different entity. [[nodiscard]] Void::LanguageServer::Protocol::Json completion_resolve(const Void::LanguageServer::Protocol::Json &item) const; + // Outline of the records the index holds for one document path. + [[nodiscard]] Void::LanguageServer::Protocol::Json document_symbols(std::string_view uri) const; + // Same-binding uses within the document, or an empty list. + [[nodiscard]] Void::LanguageServer::Protocol::Json + document_highlights(std::string_view uri, Documents::Position position) const; struct NavigationReply { Void::LanguageServer::Protocol::Json result; diff --git a/src/Server.cpp b/src/Server.cpp index 3711a12..b5f9580 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -260,9 +260,13 @@ DispatchResult Server::handle(std::string_view body) { Json{Json::Object{ {"workspaceFolders", Json{Json::Object{{"supported", Json{true}}, {"changeNotifications", Json{true}}}}}}}}}; + if (diagnostics_) { + capabilities.emplace("documentSymbolProvider", Json{true}); + } if (diagnostics_ && diagnostics_->supports_occurrences()) { capabilities.emplace("hoverProvider", Json{true}); capabilities.emplace("definitionProvider", Json{true}); + capabilities.emplace("documentHighlightProvider", Json{true}); } if (diagnostics_) capabilities.emplace("completionProvider", @@ -301,7 +305,7 @@ DispatchResult Server::handle(std::string_view body) { : Json{Json::Object{{"state", Json{std::string{"disabled"}}}}}); } if (*method == "textDocument/hover" || *method == "textDocument/definition" || - *method == "textDocument/signatureHelp") { + *method == "textDocument/signatureHelp" || *method == "textDocument/documentHighlight") { if (!diagnostics_ || !diagnostics_->supports_occurrences()) return error(id, -32601, "Method not found"); const auto *uri = as_string(member(member(params, "textDocument"), "uri")); @@ -321,7 +325,9 @@ DispatchResult Server::handle(std::string_view body) { auto result = *method == "textDocument/hover" ? diagnostics_->hover(*uri, selected) : *method == "textDocument/definition" ? diagnostics_->definition(*uri, selected) - : diagnostics_->signature_help(*uri, selected); + : *method == "textDocument/signatureHelp" + ? diagnostics_->signature_help(*uri, selected) + : diagnostics_->document_highlights(*uri, selected); return reply(id, "result", std::move(result)); } if (*method == "completionItem/resolve") { @@ -331,6 +337,14 @@ DispatchResult Server::handle(std::string_view body) { return error(id, -32602, "Expected a completion item"); return reply(id, "result", diagnostics_->completion_resolve(*params)); } + if (*method == "textDocument/documentSymbol") { + if (!diagnostics_) + return error(id, -32601, "Method not found"); + const auto *uri = as_string(member(member(params, "textDocument"), "uri")); + if (!uri || !Void::LanguageServer::Documents::client_uri_key(*uri)) + return error(id, -32602, "Expected textDocument.uri"); + return reply(id, "result", diagnostics_->document_symbols(*uri)); + } if (*method == "textDocument/completion") { if (!diagnostics_) return error(id, -32601, "Method not found"); diff --git a/src/features/DocumentSymbols.cpp b/src/features/DocumentSymbols.cpp new file mode 100644 index 0000000..d8b442b --- /dev/null +++ b/src/features/DocumentSymbols.cpp @@ -0,0 +1,29 @@ +#include + +#include +#include + +namespace vls::features::documents { + +std::vector highlights(std::span uses, std::string_view path, Position position, + std::size_t limit) { + std::set candidates; + for (const auto &use : uses) + if (use.path == path && use.range.start <= position && position <= use.range.end) + candidates.insert(use.binding); + std::vector result; + if (candidates.size() != 1) + return result; + std::set unique; + for (const auto &use : uses) + if (use.path == path && use.binding == *candidates.begin()) + unique.insert(use.range); + for (const auto &range : unique) { + if (result.size() == limit) + break; + result.push_back(range); + } + return result; +} + +} // namespace vls::features::documents diff --git a/src/index/SymbolIndex.cpp b/src/index/SymbolIndex.cpp index cf45964..2730d63 100644 --- a/src/index/SymbolIndex.cpp +++ b/src/index/SymbolIndex.cpp @@ -462,6 +462,40 @@ Result SymbolIndex::find(const Query &query) const { return result; } +Result SymbolIndex::find_by_path(std::string_view path, std::size_t limit) const { + auto &state = *state_; + std::lock_guard lock{state.mutex}; + Result result; + result.coverage = state.coverage_locked(std::nullopt); + std::set> seen; + std::set shadowed; + for (const auto &[overlay_path, overlay] : state.overlays) + shadowed.insert(overlay_path); + const auto consider = [&](const SymbolRecord &record) { + if (record.location.path != path) + return; + if (!seen.emplace(record.name, record.kind, record.location).second) + return; + result.symbols.push_back(record); + }; + for (const auto &[name, entry] : state.contexts) + for (const auto &record : entry.records) + if (!shadowed.contains(record.location.path)) + consider(record); + for (const auto &[overlay_path, overlay] : state.overlays) + for (const auto &record : overlay.records) + consider(record); + std::ranges::sort(result.symbols, [](const auto &left, const auto &right) { + return std::tie(left.location, left.name, left.kind) < + std::tie(right.location, right.name, right.kind); + }); + if (result.symbols.size() > limit) { + result.symbols.resize(limit); + result.truncated = true; + } + return result; +} + Coverage SymbolIndex::coverage() const { auto &state = *state_; std::lock_guard lock{state.mutex}; diff --git a/src/semantic/EditorDiagnostics.cpp b/src/semantic/EditorDiagnostics.cpp index d2b67c9..f73c17c 100644 --- a/src/semantic/EditorDiagnostics.cpp +++ b/src/semantic/EditorDiagnostics.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -973,6 +974,85 @@ Json EditorDiagnostics::completion_resolve(const Json &item) const { return Json{std::move(enriched)}; } +Json EditorDiagnostics::document_symbols(std::string_view uri) const { + auto &s = *state_; + const auto identity = Documents::normalize_uri(uri); + if (!identity || !identity->is_file_uri) + return Json{nullptr}; + index::Result records; + { + std::lock_guard lock{s.mutex}; + records = s.symbols.find_by_path(identity->canonical_path.native()); + } + Json::Array symbols; + std::set> seen; + for (const auto &record : records.symbols) { + const auto &location = record.location; + if (!seen.emplace(record.name, static_cast(record.kind), location.line, + location.character) + .second) + continue; + const auto range = range_json(Documents::Range{{location.line, location.character}, + {location.endLine, location.endCharacter}}); + symbols.push_back(Json{Json::Object{{"name", Json{record.name}}, + {"kind", Json{symbol_kind(record.kind)}}, + {"range", range}, + {"selectionRange", range}}}); + } + return Json{std::move(symbols)}; +} + +Json EditorDiagnostics::document_highlights(std::string_view uri, + Documents::Position position) const { + auto &s = *state_; + auto key = Documents::client_uri_key(uri); + std::shared_ptr receipt; + std::shared_ptr> facts; + { + std::lock_guard lock{s.mutex}; + const auto i = key ? s.entries.find(*key) : s.entries.end(); + if (i == s.entries.end()) + return Json{nullptr}; + receipt = i->second.receipt; + facts = i->second.occurrences; + } + if (!receipt || !facts || !s.valid(*receipt)) + return Json{nullptr}; + const Documents::DocumentSnapshot *snapshot = nullptr; + for (const auto &entry : receipt->capture->entries()) + if (entry.snapshot && entry.snapshot->identity.canonical_uri == *key) { + snapshot = entry.snapshot.get(); + break; + } + if (!snapshot) + return Json{nullptr}; + std::vector uses; + for (const auto &occurrence : *facts) { + if (occurrence.use_file.get() != snapshot) + continue; + features::documents::Use use; + use.binding.name = occurrence.name; + if (occurrence.has_definition()) + use.binding.definition = features::documents::Location{ + occurrence.definition_file->identity.canonical_path.native(), + {{occurrence.definition.start.line, occurrence.definition.start.character}, + {occurrence.definition.end.line, occurrence.definition.end.character}}}; + use.path = snapshot->identity.canonical_path.native(); + use.range = {{occurrence.use.start.line, occurrence.use.start.character}, + {occurrence.use.end.line, occurrence.use.end.character}}; + uses.push_back(std::move(use)); + } + const auto ranges = features::documents::highlights( + uses, snapshot->identity.canonical_path.native(), {position.line, position.character}); + Json::Array result; + for (const auto &range : ranges) + result.push_back(Json{Json::Object{ + {"range", range_json(Documents::Range{{range.start.line, range.start.character}, + {range.end.line, range.end.character}})}, + {"kind", Json{std::int64_t{1}}}}}); + return Json{std::move(result)}; +} + void EditorDiagnostics::set_transactional_rename(bool supported) { std::lock_guard lock{state_->mutex}; state_->transactional_rename = supported; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e2b270b..4021385 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,7 +22,7 @@ vls_test_suite(vls_scheduler_tests scheduler/SchedulerTests.cpp) vls_test_suite(vls_index_tests index/SymbolIndexTests.cpp) vls_test_suite(vls_navigation_tests navigation/NavigationTests.cpp) vls_test_suite(vls_reflection_tests reflection/ProvenanceTests.cpp) -vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp) +vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp) vls_test_suite(vls_live_tests debug/LiveSessionTests.cpp debug/LiveDapTests.cpp) target_link_libraries(vls_live_tests PRIVATE Void::System) add_dependencies(vls_live_tests vls_live_worker_fixture vls-dap) diff --git a/tests/features/DocumentSymbolsTests.cpp b/tests/features/DocumentSymbolsTests.cpp new file mode 100644 index 0000000..0ff96c6 --- /dev/null +++ b/tests/features/DocumentSymbolsTests.cpp @@ -0,0 +1,61 @@ +#include + +#include +#include + +#include + +namespace { +using namespace vls::features::documents; +using Void::Testing::expect; +using Void::Testing::require; + +Range range(std::uint32_t line, std::uint32_t character) { + return {{line, character}, {line, character + 4}}; +} + +Use use(std::string name, std::string path, Range span, + std::optional definition = std::nullopt) { + return {{std::move(name), std::move(definition)}, std::move(path), span}; +} + +Location definition(std::string path, std::uint32_t line) { + return {std::move(path), range(line, 0)}; +} +} // namespace + +VOID_TEST(DocumentSymbols, highlights_every_use_of_the_binding_at_the_position) { + const auto target = definition("/project/lib.cpp", 9); + const std::vector uses{use("f", "/project/a.cpp", range(1, 0), target), + use("f", "/project/a.cpp", range(3, 8), target), + use("f", "/project/b.cpp", range(2, 0), target), + use("g", "/project/a.cpp", range(5, 0))}; + const auto result = highlights(uses, "/project/a.cpp", {1, 2}); + require(result.size() == 2); + expect(result[0] == range(1, 0)); + expect(result[1] == range(3, 8)); +} + +VOID_TEST(DocumentSymbols, no_unique_binding_means_no_highlights) { + const std::vector uses{use("f", "/project/a.cpp", range(1, 0)), + use("g", "/project/a.cpp", range(1, 0))}; + expect(highlights(uses, "/project/a.cpp", {1, 1}).empty()); + expect(highlights(uses, "/project/a.cpp", {9, 9}).empty()); + expect(highlights(uses, "/project/other.cpp", {1, 1}).empty()); +} + +VOID_TEST(DocumentSymbols, highlights_deduplicate_and_bound) { + const auto target = definition("/project/lib.cpp", 9); + const std::vector uses{use("f", "/project/a.cpp", range(1, 0), target), + use("f", "/project/a.cpp", range(1, 0), target), + use("f", "/project/a.cpp", range(2, 0), target)}; + expect(highlights(uses, "/project/a.cpp", {1, 1}).size() == 2); + expect(highlights(uses, "/project/a.cpp", {1, 1}, 1).size() == 1); +} + +VOID_TEST(DocumentSymbols, same_spelling_without_a_shared_definition_is_ambiguous) { + const std::vector uses{ + use("f", "/project/a.cpp", range(1, 0)), + use("f", "/project/a.cpp", range(1, 0), definition("/project/lib.cpp", 4))}; + expect(highlights(uses, "/project/a.cpp", {1, 1}).empty()); +} diff --git a/tests/index/SymbolIndexTests.cpp b/tests/index/SymbolIndexTests.cpp index 0290640..e2d9ee0 100644 --- a/tests/index/SymbolIndexTests.cpp +++ b/tests/index/SymbolIndexTests.cpp @@ -441,3 +441,20 @@ VOID_TEST(SymbolIndex, schema_2_rejects_edited_records) { expect(restored->status == SymbolIndex::LoadStatus::corrupt); expect(restored->quarantine.has_value()); } + +VOID_TEST(SymbolIndex, finds_records_by_path_with_overlay_shadowing) { + SymbolIndex index; + require(index.publish( + context("debug:a", {record("a", "/project/a.cpp", 1), record("b", "/project/b.cpp", 2)}))); + require(index.publish(context("debug:b", {record("c", "/project/a.cpp", 9)}))); + auto result = index.find_by_path("/project/a.cpp"); + require(result.symbols.size() == 2); + expect(result.symbols[0].name == "a" && result.symbols[1].name == "c"); + expect(result.coverage.indexed && !result.truncated); + expect(index.find_by_path("/project/a.cpp", 1).truncated); + require(index.set_overlay("/project/a.cpp", "content", {record("d", "/project/a.cpp", 4)})); + result = index.find_by_path("/project/a.cpp"); + require(result.symbols.size() == 1); + expect(result.symbols.front().name == "d"); + expect(index.find_by_path("/project/absent.cpp").symbols.empty()); +}