From 816c38da1e38a129ca00e40a67bcf415e73958a7 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 18:27:17 +0300 Subject: [PATCH] Mark same-file definitions in semantic tokens The server legend gains a declaration modifier, and EditorDiagnostics emits the definition-name token with it when the definition lies in the same document. No type token is emitted because the canonical type has no source span. Registered tests cover modifier encoding and the overlap rule; a real stdio session showed the modifier on both definition names. --- docs/agent-workflow/LSP-PARITY.md | 2 +- docs/agent-workflow/STATE.md | 2 +- docs/agent-workflow/VERIFICATION.md | 16 ++++++++++++++++ include/vls/features/SemanticTokens.hpp | 2 ++ src/Server.cpp | 15 +++++++-------- src/semantic/EditorDiagnostics.cpp | 9 +++++++++ tests/features/SemanticTokensTests.cpp | 18 ++++++++++++++++++ 7 files changed, 54 insertions(+), 10 deletions(-) diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md index e0fc0d9..46f14fb 100644 --- a/docs/agent-workflow/LSP-PARITY.md +++ b/docs/agent-workflow/LSP-PARITY.md @@ -39,7 +39,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints. | Client-side application of server-issued workspace edits | rename and code actions return edits the clients apply directly | | 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 | -| semantic token modifiers / richer token types | legend is one type (`function`) from occurrences | +| richer token types | legend is one type (`function`) plus a `declaration` modifier; type tokens need compiler-provided spans | | textDocument/formatting | no formatter is available; needs a dependency decision | ## Next slices diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index b2f2676..4240208 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. 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 with a bounded one-type 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. 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 5097a33..7e4fa15 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -79,6 +79,22 @@ 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 declaration tokens in semantic tokens + +The semantic-token slice now also emits the definition-name token when the +definition is in the same document, carrying the advertised `declaration` +modifier; the legend gained that modifier. The canonical type has no source +span, so no type token is emitted rather than one placed on unrelated text. +Registered `vls_feature_tests` cases cover modifier preservation, encoding and +the overlap rule (90 cases). + +Real stdio session with `inner`/`outer` in one file: +``` +semanticTokens: [(0, 4, 5, 1), (1, 4, 5, 1), (1, 21, 5, 0), (1, 31, 5, 0), (2, 20, 5, 0)] +``` +The first two entries are the definition names with the declaration modifier. +All 18 native suites and all 18 CMake tests passed. + ## December 5 return-type inlay hints `include/vls/features/InlayHints.hpp` gained `call_end` (the offset past the diff --git a/include/vls/features/SemanticTokens.hpp b/include/vls/features/SemanticTokens.hpp index 4b32b0c..e7a8209 100644 --- a/include/vls/features/SemanticTokens.hpp +++ b/include/vls/features/SemanticTokens.hpp @@ -11,6 +11,8 @@ namespace vls::features::semantic_tokens { // Indices into the legend the server advertises. Only tokens the stock // observer can prove are represented; there are no keyword or guess tokens. inline constexpr std::uint32_t function_type = 0; +// Bit flag in Token::modifiers, matching the server's advertised legend. +inline constexpr std::uint32_t declaration_modifier = 1; struct Token { std::uint32_t line{}, character{}, length{}; diff --git a/src/Server.cpp b/src/Server.cpp index 05af953..e2f9675 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -322,14 +322,13 @@ DispatchResult Server::handle(std::string_view body) { } if (diagnostics_ && diagnostics_->supports_occurrences()) capabilities.emplace("inlayHintProvider", Json{true}); - if (diagnostics_ && diagnostics_->supports_occurrences()) - capabilities.emplace( - "semanticTokensProvider", - Json{Json::Object{ - {"legend", Json{Json::Object{ - {"tokenTypes", Json{Json::Array{Json{std::string{"function"}}}}}, - {"tokenModifiers", Json{Json::Array{}}}}}}, - {"full", Json{true}}}}); + if (diagnostics_ && diagnostics_->supports_occurrences()) { + Json::Object legend{ + {"tokenTypes", Json{Json::Array{Json{std::string{"function"}}}}}, + {"tokenModifiers", Json{Json::Array{Json{std::string{"declaration"}}}}}}; + Json::Object semantic{{"legend", Json{std::move(legend)}}, {"full", Json{true}}}; + capabilities.emplace("semanticTokensProvider", Json{std::move(semantic)}); + } if (diagnostics_ && diagnostics_->supports_occurrences()) capabilities.emplace( "codeActionProvider", diff --git a/src/semantic/EditorDiagnostics.cpp b/src/semantic/EditorDiagnostics.cpp index 2a4826e..bf2309e 100644 --- a/src/semantic/EditorDiagnostics.cpp +++ b/src/semantic/EditorDiagnostics.cpp @@ -1289,6 +1289,15 @@ Json EditorDiagnostics::semantic_tokens(std::string_view uri) const { continue; tokens.push_back({occurrence.use.start.line, occurrence.use.start.character, occurrence.use.end.character - occurrence.use.start.character}); + // A definition in this same file is a declaration-name token too. + if (occurrence.has_definition() && occurrence.definition_file.get() == snapshot && + occurrence.definition.start.line == occurrence.definition.end.line && + occurrence.definition.end.character > occurrence.definition.start.character) + tokens.push_back( + {occurrence.definition.start.line, occurrence.definition.start.character, + occurrence.definition.end.character - occurrence.definition.start.character, + features::semantic_tokens::function_type, + features::semantic_tokens::declaration_modifier}); } const auto normalized = features::semantic_tokens::normalize(tokens); Json::Array data; diff --git a/tests/features/SemanticTokensTests.cpp b/tests/features/SemanticTokensTests.cpp index 8a4b6fd..7a91431 100644 --- a/tests/features/SemanticTokensTests.cpp +++ b/tests/features/SemanticTokensTests.cpp @@ -49,3 +49,21 @@ VOID_TEST(SemanticTokens, bounds_count_and_length) { const std::vector long_token{token(0, 0, 64)}; expect(normalize(long_token, Limits{.length = 8}).empty()); } + +VOID_TEST(SemanticTokens, modifiers_are_preserved_and_encoded) { + const std::vector tokens{{0, 0, 1, function_type, declaration_modifier}, + {0, 2, 1, function_type, 0}}; + const auto result = normalize(tokens); + require(result.size() == 2); + expect(result[0].modifiers == declaration_modifier); + expect(result[1].modifiers == 0); + expect(encode(result) == std::vector{0, 0, 1, 0, 1, 0, 2, 1, 0, 0}); +} + +VOID_TEST(SemanticTokens, an_overlapping_token_never_replaces_the_earlier_one) { + const std::vector tokens{{1, 4, 3, function_type, 0}, + {1, 4, 3, function_type, declaration_modifier}}; + const auto result = normalize(tokens); + require(result.size() == 1); + expect(result.front().modifiers == 0); +}