From 158125ace522ef480d633995af7d170fb2791b4c Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 16:03:06 +0300 Subject: [PATCH] Handle workspace file-operation notifications The server advertises didCreate/didRename/didDelete file operations with file-scheme filters. A bounded core converts notifications into canonical filesystem changes (rename as old-gone/new-present, non-file URIs ignored, invalid URIs rejected) with registered tests, and EditorDiagnostics invalidates every dependent index context for changed or removed paths before re-queueing open documents. A real stdio session observed the index become incomplete after a deleted header, and a malformed notification left the server answering. --- CMakeLists.txt | 2 +- build.cpp | 4 +- docs/agent-workflow/LSP-PARITY.md | 9 ++-- docs/agent-workflow/STATE.md | 2 +- docs/agent-workflow/VERIFICATION.md | 18 +++++++ include/vls/features/FileOperations.hpp | 48 +++++++++++++++++ include/vls/semantic/EditorDiagnostics.hpp | 4 ++ src/Server.cpp | 51 ++++++++++++++++-- src/features/FileOperations.cpp | 45 ++++++++++++++++ src/semantic/EditorDiagnostics.cpp | 12 +++++ tests/CMakeLists.txt | 2 +- tests/features/FileOperationsTests.cpp | 63 ++++++++++++++++++++++ 12 files changed, 248 insertions(+), 12 deletions(-) create mode 100644 include/vls/features/FileOperations.hpp create mode 100644 src/features/FileOperations.cpp create mode 100644 tests/features/FileOperationsTests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 474ee65..6978d54 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/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/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 be1f6c8..b2b0ca9 100644 --- a/build.cpp +++ b/build.cpp @@ -74,6 +74,7 @@ void build(Void::Build::Project &project) { "src/features/CodeActions.cpp", "src/features/SemanticTokens.cpp", "src/features/InlayHints.cpp", + "src/features/FileOperations.cpp", "src/debug/live/LiveSession.cpp", "src/modules/ModuleWorkQueue.cpp", "src/modules/GccScan.cpp", @@ -154,7 +155,8 @@ void build(Void::Build::Project &project) { {"tests/features/CompletionTests.cpp", "tests/features/IndexNavigationTests.cpp", "tests/features/SignatureHelpTests.cpp", "tests/features/DocumentSymbolsTests.cpp", "tests/features/TextStructureTests.cpp", "tests/features/CodeActionsTests.cpp", - "tests/features/SemanticTokensTests.cpp", "tests/features/InlayHintsTests.cpp"}); + "tests/features/SemanticTokensTests.cpp", "tests/features/InlayHintsTests.cpp", + "tests/features/FileOperationsTests.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 3707f49..94d4d79 100644 --- a/docs/agent-workflow/LSP-PARITY.md +++ b/docs/agent-workflow/LSP-PARITY.md @@ -22,6 +22,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints. | textDocument/semanticTokens/full | `vls_feature_tests`, real stdio session | | textDocument/inlayHint (parameter types) | `vls_feature_tests`, real stdio session | | workspace/symbol | stock occurrence corpus | +| workspace fileOperations (didCreate/didRename/didDelete) | `vls_feature_tests`, real stdio session | | voidLanguageServer/analysisStatus, providerStatus | stdio corpora | | Debug adapter (DAP) separately through `vls-dap` | `vls_live_tests`, `vls_dap_tests` | @@ -34,7 +35,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints. | textDocument/formatting | needs a plan | | callHierarchy / typeHierarchy | needs compiler call/type relationships | | textDocument/codeLens, executeCommand | command surface not designed | -| workspace edits and file operations | client-side application is also open | +| 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 | @@ -42,6 +43,6 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints. ## Next slices -1. `workspace/applyEdit` and client-side application of ready rename plans. -2. call hierarchy from stock call facts. -3. Formatting via the selected compiler. +1. call hierarchy from stock call facts. +2. Formatting via the selected compiler. +3. code lens and executeCommand. diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index 5e28017..79f5848 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. 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 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. 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 6f78916..66caffa 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -79,6 +79,24 @@ 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 workspace file operations + +The server advertises `workspace.fileOperations` (`didCreate`, `didRename`, +`didDelete`) with file-scheme filters. `include/vls/features/FileOperations.hpp` +converts notifications into canonical filesystem changes (rename yields the old +path gone and the new path present; non-file URIs are counted as ignored; +invalid URIs reject the notification) with registered `vls_feature_tests` +cases. `EditorDiagnostics::files_changed` invalidates every dependent index +context for changed or removed paths (clearing an overlay for removed files) and +the editor re-queues open documents. + +A real stdio session with a temporary workspace whose `main.cpp` includes +`extra.hpp`: before deletion `ready` with `index {'complete': True, 'records': 1}`; +after deleting the header and sending `workspace/didDeleteFiles` the analysis +re-ran to `partial` with `index {'complete': False, 'records': 0}`, and a +malformed rename notification left the server answering. All 18 native suites +and all 18 CMake tests passed. + ## December 5 inlay hints from occurrence signatures `include/vls/features/InlayHints.hpp` finds the top-level argument starts of a diff --git a/include/vls/features/FileOperations.hpp b/include/vls/features/FileOperations.hpp new file mode 100644 index 0000000..36768b2 --- /dev/null +++ b/include/vls/features/FileOperations.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace vls::features::file_operations { + +// LSP file-operation notifications. +enum class Kind { create, rename, delete_ }; + +// One entry of a notification's `files` array. Rename carries both URIs. +struct Input { + Kind kind{Kind::create}; + std::string uri; + std::string old_uri; +}; + +// A resulting filesystem change. `gone` means the path no longer exists at +// that identity, so dependent analysis is invalid rather than merely stale. +struct Change { + std::string path; + bool gone{}; + auto operator<=>(const Change &) const = default; +}; + +struct Limits { + std::size_t files{4096}; + std::size_t pathBytes{4096}; +}; + +struct Result { + std::vector changes; + // Non-file URIs are ignored (they cannot belong to a filesystem context). + std::size_t ignored{}; +}; + +// Converts notifications into canonical filesystem changes. A rename yields +// the old path as gone and the new path as present. Malformed entries are +// rejected rather than partially applied. +[[nodiscard]] std::expected parse(std::span, + const Limits & = Limits{}); + +} // namespace vls::features::file_operations diff --git a/include/vls/semantic/EditorDiagnostics.hpp b/include/vls/semantic/EditorDiagnostics.hpp index c387f26..3d36d9a 100644 --- a/include/vls/semantic/EditorDiagnostics.hpp +++ b/include/vls/semantic/EditorDiagnostics.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -16,6 +17,9 @@ class EditorDiagnostics { WorkspaceSession &, const Documents::DocumentStore &, Send); ~EditorDiagnostics(); void set_version_support(bool); + // Filesystem changes from workspace file-operation notifications. Paths + // that changed or disappeared invalidate every dependent analysis context. + void files_changed(std::span); void begin_update(); void finish_update(bool workspace_changed = false); void stop(); diff --git a/src/Server.cpp b/src/Server.cpp index 27da03a..4e70aa8 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -21,6 +21,13 @@ using Protocol::as_object; using Protocol::as_string; using Protocol::find_member; +Json file_operation_registration() { + return Json{Json::Object{ + {"filters", Json{Json::Array{Json{Json::Object{ + {"scheme", Json{std::string{"file"}}}, + {"pattern", Json{Json::Object{{"glob", Json{std::string{"**/*"}}}}}}}}}}}}}; +} + const Json *member(const Json *value, std::string_view key) { const auto *object = as_object(value); return object ? find_member(*object, key) : nullptr; @@ -213,6 +220,39 @@ DispatchResult Server::handle(std::string_view body) { .diagnostic = updated.error().substr(0, 256)}; return {}; } + if (*method == "workspace/didCreateFiles" || *method == "workspace/didRenameFiles" || + *method == "workspace/didDeleteFiles") { + if (!diagnostics_) + return {}; + const auto *files = as_array(member(params, "files")); + if (!files) + return {.response = std::nullopt, + .exit_code = std::nullopt, + .diagnostic = "invalid file operation notification"}; + const auto kind = *method == "workspace/didCreateFiles" + ? features::file_operations::Kind::create + : *method == "workspace/didRenameFiles" + ? features::file_operations::Kind::rename + : features::file_operations::Kind::delete_; + std::vector inputs; + for (const auto &item : *files) { + const auto *fields = as_object(&item); + const auto *uri = fields ? as_string(find_member(*fields, "uri")) : nullptr; + const auto *old_uri = + fields ? as_string(find_member(*fields, "oldUri")) : nullptr; + inputs.push_back( + {kind, uri ? *uri : std::string{}, old_uri ? *old_uri : std::string{}}); + } + const auto parsed = features::file_operations::parse(inputs); + if (!parsed) + return {.response = std::nullopt, + .exit_code = std::nullopt, + .diagnostic = parsed.error()}; + diagnostics_->begin_update(); + diagnostics_->files_changed(parsed->changes); + diagnostics_->finish_update(true); + return {}; + } const bool change = *method == "textDocument/didOpen" || *method == "textDocument/didChange" || *method == "textDocument/didClose"; @@ -253,14 +293,17 @@ DispatchResult Server::handle(std::string_view body) { Protocol::as_bool(member(edit, "documentChanges")).value_or(false)); } Json::Object sync{{"openClose", Json{true}}, {"change", Json{std::int64_t{2}}}}; + Json::Object folders{{"supported", Json{true}}, {"changeNotifications", Json{true}}}; + Json::Object workspace{ + {"workspaceFolders", Json{std::move(folders)}}, + {"fileOperations", Json{Json::Object{{"didCreate", file_operation_registration()}, + {"didRename", file_operation_registration()}, + {"didDelete", file_operation_registration()}}}}}; Json::Object capabilities{ {"positionEncoding", Json{std::string{Void::LanguageServer::Documents::encoding_name(encoding)}}}, {"textDocumentSync", Json{std::move(sync)}}, - {"workspace", - Json{Json::Object{ - {"workspaceFolders", Json{Json::Object{{"supported", Json{true}}, - {"changeNotifications", Json{true}}}}}}}}}; + {"workspace", Json{std::move(workspace)}}}; if (diagnostics_) { capabilities.emplace("documentSymbolProvider", Json{true}); capabilities.emplace("selectionRangeProvider", Json{true}); diff --git a/src/features/FileOperations.cpp b/src/features/FileOperations.cpp new file mode 100644 index 0000000..30b0d96 --- /dev/null +++ b/src/features/FileOperations.cpp @@ -0,0 +1,45 @@ +#include + +#include + +namespace vls::features::file_operations { + +std::expected parse(std::span inputs, const Limits &limits) { + if (inputs.size() > limits.files) + return std::unexpected("too many file operations"); + Result result; + const auto add = [&](const std::string &uri, bool gone) -> std::expected { + if (uri.size() > limits.pathBytes) + return std::unexpected("file operation URI exceeds its bound"); + const auto identity = Void::LanguageServer::Documents::normalize_uri(uri); + if (!identity) + return std::unexpected("file operation URI is invalid"); + if (!identity->is_file_uri) { + ++result.ignored; + return {}; + } + result.changes.push_back({identity->canonical_path.native(), gone}); + return {}; + }; + for (const auto &input : inputs) { + switch (input.kind) { + case Kind::create: + if (auto added = add(input.uri, false); !added) + return std::unexpected(added.error()); + break; + case Kind::delete_: + if (auto added = add(input.uri, true); !added) + return std::unexpected(added.error()); + break; + case Kind::rename: + if (auto added = add(input.old_uri, true); !added) + return std::unexpected(added.error()); + if (auto added = add(input.uri, false); !added) + return std::unexpected(added.error()); + break; + } + } + return result; +} + +} // namespace vls::features::file_operations diff --git a/src/semantic/EditorDiagnostics.cpp b/src/semantic/EditorDiagnostics.cpp index 22d179d..f8ba0ca 100644 --- a/src/semantic/EditorDiagnostics.cpp +++ b/src/semantic/EditorDiagnostics.cpp @@ -696,6 +696,18 @@ void EditorDiagnostics::begin_update() { s.module_work.cancel(key); } } +void EditorDiagnostics::files_changed(std::span changes) { + auto &s = *state_; + std::lock_guard lock{s.mutex}; + for (const auto &change : changes) { + if (change.gone) + s.symbols.clear_overlay(change.path); + // The new content is unknown, so the path is treated as changed: every + // context that consumed it is marked incomplete, never silently kept. + s.symbols.invalidate(change.path, std::nullopt); + } +} + void EditorDiagnostics::finish_update(bool workspace_changed) { auto &s = *state_; auto overlays = s.documents.list_overlays(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 31ad498..73546d7 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) +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) 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/FileOperationsTests.cpp b/tests/features/FileOperationsTests.cpp new file mode 100644 index 0000000..ec4ddd2 --- /dev/null +++ b/tests/features/FileOperationsTests.cpp @@ -0,0 +1,63 @@ +#include + +#include +#include + +#include +#include + +namespace { +using namespace vls::features::file_operations; +using Void::Testing::expect; +using Void::Testing::require; + +Input input(Kind kind, std::string uri, std::string old_uri = {}) { + return {kind, std::move(uri), std::move(old_uri)}; +} +} // namespace + +VOID_TEST(FileOperations, create_and_delete_map_to_presence_and_absence) { + const std::vector inputs{input(Kind::create, "file:///project/a.cpp"), + input(Kind::delete_, "file:///project/b.cpp")}; + const auto result = parse(inputs); + require(result.has_value()); + require(result->changes.size() == 2); + expect(result->changes[0] == Change{"/project/a.cpp", false}); + expect(result->changes[1] == Change{"/project/b.cpp", true}); +} + +VOID_TEST(FileOperations, rename_reports_the_old_path_gone_and_the_new_present) { + const std::vector inputs{ + input(Kind::rename, "file:///project/new.cpp", "file:///project/old.cpp")}; + const auto result = parse(inputs); + require(result.has_value()); + require(result->changes.size() == 2); + expect(result->changes[0] == Change{"/project/old.cpp", true}); + expect(result->changes[1] == Change{"/project/new.cpp", false}); +} + +VOID_TEST(FileOperations, non_file_uris_are_ignored_not_rejected) { + const std::vector inputs{input(Kind::create, "untitled:Untitled-1"), + input(Kind::create, "file:///project/a.cpp")}; + const auto result = parse(inputs); + require(result.has_value()); + expect(result->ignored == 1); + require(result->changes.size() == 1); + expect(result->changes.front().path == "/project/a.cpp"); +} + +VOID_TEST(FileOperations, invalid_uris_reject_the_notification) { + const std::vector invalid{input(Kind::create, "")}; + expect(!parse(invalid).has_value()); + const std::vector malformed{input(Kind::create, "file://host/a.cpp")}; + expect(!parse(malformed).has_value()); +} + +VOID_TEST(FileOperations, counts_and_paths_are_bounded) { + const std::vector inputs{input(Kind::create, "file:///project/a.cpp"), + input(Kind::create, "file:///project/b.cpp")}; + expect(!parse(inputs, Limits{.files = 1}).has_value()); + const std::vector one{input(Kind::create, "file:///project/a.cpp")}; + expect(!parse(one, Limits{.pathBytes = 8}).has_value()); + require(parse(one, Limits{.pathBytes = 4096}).has_value()); +}