From 62d6ad01e87e39174e3b3ce50abbcd498afadd0d Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 19:21:23 +0300 Subject: [PATCH] Serve document links for quoted includes A bounded core resolves quoted include directives from the document directory and reports the spelling range; the server advertises documentLinkProvider and serves textDocument/documentLink from snapshots, keeping only targets that exist on disk. Angle includes are skipped because their search roots are unknown. Registered feature tests cover resolution, indentation, line counting, skipping and bounds, and the exact-capability-set assertion was updated. A real stdio session linked extra.hpp and skipped and a missing file. --- CMakeLists.txt | 2 +- build.cpp | 4 +- docs/agent-workflow/LSP-PARITY.md | 1 + docs/agent-workflow/STATE.md | 2 +- docs/agent-workflow/VERIFICATION.md | 19 ++++++++ extra.hpp | 1 + include/vls/features/DocumentLinks.hpp | 41 +++++++++++++++++ main.cpp | 3 ++ src/Server.cpp | 32 ++++++++++++++ src/features/DocumentLinks.cpp | 57 ++++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- tests/TestSupport.hpp | 4 +- tests/features/DocumentLinksTests.cpp | 61 ++++++++++++++++++++++++++ 13 files changed, 224 insertions(+), 5 deletions(-) create mode 100644 extra.hpp create mode 100644 include/vls/features/DocumentLinks.hpp create mode 100644 main.cpp create mode 100644 src/features/DocumentLinks.cpp create mode 100644 tests/features/DocumentLinksTests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 65830dc..e456233 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/features/DocumentSymbols.cpp src/features/TextStructure.cpp src/features/CodeActions.cpp src/features/SemanticTokens.cpp src/features/InlayHints.cpp src/features/FileOperations.cpp src/features/IncludeUpdate.cpp src/features/CallHierarchy.cpp src/features/CodeLens.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/features/TextStructure.cpp src/features/CodeActions.cpp src/features/SemanticTokens.cpp src/features/InlayHints.cpp src/features/FileOperations.cpp src/features/IncludeUpdate.cpp src/features/CallHierarchy.cpp src/features/CodeLens.cpp src/features/DocumentLinks.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 ba83fa5..f186595 100644 --- a/build.cpp +++ b/build.cpp @@ -78,6 +78,7 @@ void build(Void::Build::Project &project) { "src/features/IncludeUpdate.cpp", "src/features/CallHierarchy.cpp", "src/features/CodeLens.cpp", + "src/features/DocumentLinks.cpp", "src/debug/live/LiveSession.cpp", "src/modules/ModuleWorkQueue.cpp", "src/modules/GccScan.cpp", @@ -160,7 +161,8 @@ void build(Void::Build::Project &project) { "tests/features/TextStructureTests.cpp", "tests/features/CodeActionsTests.cpp", "tests/features/SemanticTokensTests.cpp", "tests/features/InlayHintsTests.cpp", "tests/features/FileOperationsTests.cpp", "tests/features/IncludeUpdateTests.cpp", - "tests/features/CallHierarchyTests.cpp", "tests/features/CodeLensTests.cpp"}); + "tests/features/CallHierarchyTests.cpp", "tests/features/CodeLensTests.cpp", + "tests/features/DocumentLinksTests.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 index 63b78b2..93f2ac0 100644 --- a/docs/agent-workflow/LSP-PARITY.md +++ b/docs/agent-workflow/LSP-PARITY.md @@ -23,6 +23,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints. | textDocument/inlayHint (parameter and return types) | `vls_feature_tests`, real stdio session | | textDocument/codeLens + workspace/executeCommand (`vls.showReferences`) | `vls_feature_tests`, real stdio session | | workspace/symbol | stock occurrence corpus | +| textDocument/documentLink (quoted includes) | `vls_feature_tests`, real stdio session | | workspace fileOperations (didCreate/didRename/didDelete) | `vls_feature_tests`, real stdio session | | workspace/willRenameFiles (include updates) | `vls_feature_tests`, real stdio session | | voidLanguageServer/analysisStatus, providerStatus | stdio corpora | diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index d28c43b..f8df37f 100644 --- a/docs/agent-workflow/STATE.md +++ b/docs/agent-workflow/STATE.md @@ -103,7 +103,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. 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. Selection and folding ranges are served from immutable document snapshots with registered bounded cores. Code actions now offer the compiler's own suggested fixes (quickfix) from retained published diagnostics, with SARIF fix decoding bound to the owner snapshot. Semantic tokens encode the proven call occurrences and same-file definition names, the latter with a declaration modifier, under a bounded legend. Inlay hints project parameter types from the occurrence signature at each argument start. Workspace file-operation notifications invalidate dependent index contexts and re-queue open documents. A will-rename request returns the include updates for open documents. Code lenses show index reference counts and the `vls.showReferences` command resolves them to navigation locations. Call hierarchy serves prepare and incoming calls (grouped by enclosing definition or explicitly by file) from the stock call facts. Outgoing calls are served and verified: the stock observer now reports full function extents, so nested calls are attributed to the enclosing definition. 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. Selection and folding ranges are served from immutable document snapshots with registered bounded cores. Code actions now offer the compiler's own suggested fixes (quickfix) from retained published diagnostics, with SARIF fix decoding bound to the owner snapshot. Semantic tokens encode the proven call occurrences and same-file definition names, the latter with a declaration modifier, under a bounded legend. Inlay hints project parameter types from the occurrence signature at each argument start. Workspace file-operation notifications invalidate dependent index contexts and re-queue open documents. A will-rename request returns the include updates for open documents. Document links resolve quoted includes to existing files without the compiler. Code lenses show index reference counts and the `vls.showReferences` command resolves them to navigation locations. Call hierarchy serves prepare and incoming calls (grouped by enclosing definition or explicitly by file) from the stock call facts. Outgoing calls are served and verified: the stock observer now reports full function extents, so nested calls are attributed to the enclosing definition. 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/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index 2923599..7960eeb 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -79,6 +79,25 @@ 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 links for includes + +`include/vls/features/DocumentLinks.hpp` resolves quoted include directives +from the document's directory (angle includes are skipped because their search +roots are unknown) and returns the spelling range per directive, with +registered `vls_feature_tests` cases for resolution, indentation, line counting, +skipping and bounds. `Server.cpp` advertises `documentLinkProvider` and serves +`textDocument/documentLink` from document snapshots, keeping only targets that +exist on disk. No compiler is involved. + +Real stdio session on a document with `#include "extra.hpp"`, +`#include ` and `#include "missing.hpp"`: +``` +documentLink: 1 [({'character': 10, 'line': 0}, 'extra.hpp')] +``` +The angle include and the missing file produce no link. The exact-capability-set +assertion in `tests/TestSupport.hpp` was updated to the fourth advertised +capability. All 18 native suites and all 18 CMake tests passed. + ## December 5 index schema 3 and overload-set signature help `SymbolRecord` now carries the optional compiler signature and the snapshot diff --git a/extra.hpp b/extra.hpp new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/extra.hpp @@ -0,0 +1 @@ +#pragma once diff --git a/include/vls/features/DocumentLinks.hpp b/include/vls/features/DocumentLinks.hpp new file mode 100644 index 0000000..4bf1171 --- /dev/null +++ b/include/vls/features/DocumentLinks.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace vls::features::document_links { + +struct Position { + std::uint32_t line{}, character{}; + auto operator<=>(const Position &) const = default; +}; +struct Range { + Position start, end; + auto operator<=>(const Range &) const = default; +}; + +// A quoted include resolved lexically against the document's directory. +struct Link { + Range range; // the spelling inside the quotes + std::string path; + auto operator<=>(const Link &) const = default; +}; + +struct Limits { + std::size_t links{256}; + std::size_t pathBytes{4096}; + std::size_t scanBytes{8 * 1024 * 1024}; +}; + +// Quoted include directives in TEXT resolved from DOCUMENT_PATH's directory. +// Angle includes are skipped because their search roots are unknown, and a +// directive without a terminated spelling on its line is skipped. The result +// is bounded; existence on disk is the caller's check. +[[nodiscard]] std::vector includes(std::string_view text, std::string_view document_path, + const Limits & = Limits{}); + +} // namespace vls::features::document_links diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e7d9120 --- /dev/null +++ b/main.cpp @@ -0,0 +1,3 @@ +#include "extra.hpp" +#include +#include "missing.hpp" diff --git a/src/Server.cpp b/src/Server.cpp index e2f9675..c6fbe43 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -2,11 +2,14 @@ #include #include +#include #include +#include #include #include #include +#include #include #include #include @@ -305,6 +308,8 @@ DispatchResult Server::handle(std::string_view body) { Json{std::string{Void::LanguageServer::Documents::encoding_name(encoding)}}}, {"textDocumentSync", Json{std::move(sync)}}, {"workspace", Json{std::move(workspace)}}}; + capabilities.emplace("documentLinkProvider", + Json{Json::Object{{"resolveProvider", Json{false}}}}); if (diagnostics_) { capabilities.emplace("documentSymbolProvider", Json{true}); capabilities.emplace("selectionRangeProvider", Json{true}); @@ -441,6 +446,33 @@ DispatchResult Server::handle(std::string_view body) { } return reply(id, "result", diagnostics_->selection_ranges(*uri, selected)); } + if (*method == "textDocument/documentLink") { + 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"); + const auto snapshot = documents_.snapshot(*uri); + if (!snapshot) + return reply(id, "result", Json{Json::Array{}}); + const auto links = features::document_links::includes( + snapshot->text, snapshot->identity.canonical_path.native()); + Json::Array result; + for (const auto &link : links) { + std::error_code error; + if (!std::filesystem::exists(link.path, error)) + continue; + const auto point = [](Void::LanguageServer::Documents::Position value) { + return Json{ + Json::Object{{"line", Json{static_cast(value.line)}}, + {"character", Json{static_cast(value.character)}}}}; + }; + result.push_back(Json{Json::Object{ + {"range", Json{Json::Object{ + {"start", point({link.range.start.line, link.range.start.character})}, + {"end", point({link.range.end.line, link.range.end.character})}}}}, + {"target", Json{Void::LanguageServer::Documents::path_to_file_uri(link.path)}}}}); + } + return reply(id, "result", Json{std::move(result)}); + } if (*method == "textDocument/codeLens") { if (!diagnostics_ || !diagnostics_->supports_occurrences()) return error(id, -32601, "Method not found"); diff --git a/src/features/DocumentLinks.cpp b/src/features/DocumentLinks.cpp new file mode 100644 index 0000000..51605ae --- /dev/null +++ b/src/features/DocumentLinks.cpp @@ -0,0 +1,57 @@ +#include + +#include +#include + +namespace vls::features::document_links { +namespace fs = std::filesystem; + +std::vector includes(std::string_view text, std::string_view document_path, + const Limits &limits) { + std::vector links; + if (document_path.size() > limits.pathBytes) + return links; + const fs::path directory = fs::path{document_path}.parent_path(); + const auto stop = std::min(text.size(), limits.scanBytes); + std::size_t offset{}; + std::uint32_t line_number{}; + while (offset < stop) { + auto end = text.find('\n', offset); + if (end == std::string_view::npos || end > stop) + end = stop; + auto line = text.substr(offset, end - offset); + const auto first = line.find_first_not_of(" \t"); + if (first != std::string_view::npos) { + line.remove_prefix(first); + if (line.starts_with("#include")) { + const auto rest = line.substr(8); + const auto space = rest.find_first_not_of(" \t"); + if (space != std::string_view::npos && rest[space] == '"') { + const auto open = offset + first + 8 + space + 1; + const auto close = text.find('"', open); + if (close != std::string_view::npos && close < end) { + const auto spelling = text.substr(open, close - open); + if (!spelling.empty() && spelling.size() <= limits.pathBytes) { + const fs::path include{spelling}; + const auto resolved = include.is_absolute() + ? include.lexically_normal() + : (directory / include).lexically_normal(); + const auto start = offset + first + 8 + space + 1; + links.push_back( + {{{line_number, static_cast(start - offset)}, + {line_number, static_cast(close - offset)}}, + resolved.native()}); + if (links.size() == limits.links) + return links; + } + } + } + } + } + offset = end + 1; + ++line_number; + } + return links; +} + +} // namespace vls::features::document_links diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 137a103..f26a30a 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 features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp features/CodeActionsTests.cpp features/SemanticTokensTests.cpp features/InlayHintsTests.cpp features/FileOperationsTests.cpp features/IncludeUpdateTests.cpp features/CallHierarchyTests.cpp features/CodeLensTests.cpp) +vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp features/CodeActionsTests.cpp features/SemanticTokensTests.cpp features/InlayHintsTests.cpp features/FileOperationsTests.cpp features/IncludeUpdateTests.cpp features/CallHierarchyTests.cpp features/CodeLensTests.cpp features/DocumentLinksTests.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/TestSupport.hpp b/tests/TestSupport.hpp index b57f81e..cb4c3d4 100644 --- a/tests/TestSupport.hpp +++ b/tests/TestSupport.hpp @@ -63,7 +63,9 @@ inline void expect_sync_capabilities(const vls::Json &result, std::string_view encoding = "utf-16") { const auto &capabilities = field(result, "capabilities"); Void::Testing::expect_eq(std::get(capabilities.value).size(), - std::size_t{3}); + std::size_t{4}); + Void::Testing::expect(!std::get( + field(field(capabilities, "documentLinkProvider"), "resolveProvider").value)); const auto &folders = field(field(capabilities, "workspace"), "workspaceFolders"); Void::Testing::expect(std::get(field(folders, "supported").value)); Void::Testing::expect(std::get(field(folders, "changeNotifications").value)); diff --git a/tests/features/DocumentLinksTests.cpp b/tests/features/DocumentLinksTests.cpp new file mode 100644 index 0000000..4cbac16 --- /dev/null +++ b/tests/features/DocumentLinksTests.cpp @@ -0,0 +1,61 @@ +#include + +#include +#include + +#include +#include + +namespace { +using namespace vls::features::document_links; +using Void::Testing::expect; +using Void::Testing::require; +} // namespace + +VOID_TEST(DocumentLinks, resolves_quoted_includes_from_the_document_directory) { + const std::string text = "#pragma once\n#include \"extra.hpp\"\nint x;\n"; + const auto links = includes(text, "/project/main.cpp"); + require(links.size() == 1); + expect(links.front().path == "/project/extra.hpp"); + expect(links.front().range.start == Position{1, 10}); + expect(links.front().range.end == Position{1, 19}); + const auto nested = includes("#include \"sub/extra.hpp\"\n", "/project/main.cpp"); + require(nested.size() == 1); + expect(nested.front().path == "/project/sub/extra.hpp"); +} + +VOID_TEST(DocumentLinks, skips_angle_includes_and_system_roots) { + const std::string text = "#include \n#include \"local.hpp\"\n"; + const auto links = includes(text, "/project/main.cpp"); + require(links.size() == 1); + expect(links.front().path == "/project/local.hpp"); +} + +VOID_TEST(DocumentLinks, keeps_absolute_spellings) { + const auto links = includes("#include \"/opt/inc/a.hpp\"\n", "/project/main.cpp"); + require(links.size() == 1); + expect(links.front().path == "/opt/inc/a.hpp"); +} + +VOID_TEST(DocumentLinks, skips_unterminated_and_indented_forms) { + expect(includes("#include \"unterminated\n", "/project/main.cpp").empty()); + const auto indented = includes(" #include \"a.hpp\"\n", "/project/main.cpp"); + require(indented.size() == 1); + expect(indented.front().range.start.character == 12); +} + +VOID_TEST(DocumentLinks, counts_lines_across_the_document) { + const std::string text = "int a;\n\n#include \"x.hpp\"\n"; + const auto links = includes(text, "/project/main.cpp"); + require(links.size() == 1); + expect(links.front().range.start.line == 2); +} + +VOID_TEST(DocumentLinks, is_bounded) { + std::string text; + for (int line = 0; line != 4; ++line) + text += "#include \"a.hpp\"\n"; + expect(includes(text, "/project/main.cpp", Limits{.links = 2}).size() == 2); + expect(includes(text, "/project/main.cpp", Limits{.scanBytes = 10}).empty()); + expect(includes(text, "/project/main.cpp", Limits{.pathBytes = 4}).empty()); +}