diff --git a/CMakeLists.txt b/CMakeLists.txt index 6978d54..3e01d2b 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/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/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 b2b0ca9..8a95a9c 100644 --- a/build.cpp +++ b/build.cpp @@ -75,6 +75,7 @@ void build(Void::Build::Project &project) { "src/features/SemanticTokens.cpp", "src/features/InlayHints.cpp", "src/features/FileOperations.cpp", + "src/features/IncludeUpdate.cpp", "src/debug/live/LiveSession.cpp", "src/modules/ModuleWorkQueue.cpp", "src/modules/GccScan.cpp", @@ -156,7 +157,7 @@ void build(Void::Build::Project &project) { "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/FileOperationsTests.cpp"}); + "tests/features/FileOperationsTests.cpp", "tests/features/IncludeUpdateTests.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 94d4d79..ea4ed95 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 types) | `vls_feature_tests`, real stdio session | | workspace/symbol | stock occurrence corpus | | 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 | | Debug adapter (DAP) separately through `vls-dap` | `vls_live_tests`, `vls_dap_tests` | diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index 79f5848..c4d7330 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. 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. A will-rename request returns the include updates for 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 66caffa..2b9c182 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 include updates on file rename + +`include/vls/features/IncludeUpdate.hpp` finds quoted include directives that +resolve from the document's directory to a renamed path and returns byte-offset +edits replacing only the spelling (relative stays relative, `./` is preserved, +absolute stays absolute; angle includes are never touched because their search +roots are unknown), with registered `vls_feature_tests` cases. The server +advertises `willRename` alongside the other file operations and answers +`workspace/willRenameFiles` through `EditorDiagnostics::renamed_files`, which +converts the edits to negotiated positions with each open overlay. + +Real stdio session: renaming `extra.hpp` to `renamed.hpp` while `main.cpp` +includes it returned +``` +willRenameFiles: {"changes": {"file:///.../main.cpp": [{"newText": "renamed.hpp", + "range": {"end": {"character": 19, "line": 0}, "start": {"character": 10, "line": 0}}}]}} +``` +All 18 native suites and all 18 CMake tests passed. + ## December 5 workspace file operations The server advertises `workspace.fileOperations` (`didCreate`, `didRename`, diff --git a/include/vls/features/IncludeUpdate.hpp b/include/vls/features/IncludeUpdate.hpp new file mode 100644 index 0000000..386f909 --- /dev/null +++ b/include/vls/features/IncludeUpdate.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace vls::features::include_update { + +// One edit as byte offsets into the document text. The caller converts them to +// negotiated LSP positions with the document snapshot. +struct Edit { + std::size_t start{}, end{}; + std::string replacement; + auto operator<=>(const Edit &) const = default; +}; + +struct Limits { + std::size_t edits{256}; + std::size_t pathBytes{4096}; + std::size_t scanBytes{8 * 1024 * 1024}; +}; + +// Edits for every quoted include directive in TEXT that resolves from the +// document's directory to OLD_PATH, replacing the spelling with NEW_PATH +// relative to that directory (or absolute when it was absolute). Angle +// includes are never touched because their search roots are unknown. +[[nodiscard]] std::vector include_edits(std::string_view text, std::string_view document_path, + std::string_view old_path, std::string_view new_path, + const Limits & = Limits{}); + +} // namespace vls::features::include_update diff --git a/include/vls/semantic/EditorDiagnostics.hpp b/include/vls/semantic/EditorDiagnostics.hpp index 3d36d9a..ec13f6a 100644 --- a/include/vls/semantic/EditorDiagnostics.hpp +++ b/include/vls/semantic/EditorDiagnostics.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -20,6 +21,13 @@ class EditorDiagnostics { // Filesystem changes from workspace file-operation notifications. Paths // that changed or disappeared invalidate every dependent analysis context. void files_changed(std::span); + struct RenamedFile { + std::string old_path, new_path; + }; + // Workspace edit updating quoted includes in open documents that resolve + // to a renamed path. Null when nothing would change. + [[nodiscard]] Void::LanguageServer::Protocol::Json + renamed_files(std::span) const; void begin_update(); void finish_update(bool workspace_changed = false); void stop(); diff --git a/src/Server.cpp b/src/Server.cpp index 4e70aa8..55aea20 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -298,7 +298,8 @@ DispatchResult Server::handle(std::string_view body) { {"workspaceFolders", Json{std::move(folders)}}, {"fileOperations", Json{Json::Object{{"didCreate", file_operation_registration()}, {"didRename", file_operation_registration()}, - {"didDelete", file_operation_registration()}}}}}; + {"didDelete", file_operation_registration()}, + {"willRename", file_operation_registration()}}}}}; Json::Object capabilities{ {"positionEncoding", Json{std::string{Void::LanguageServer::Documents::encoding_name(encoding)}}}, @@ -431,6 +432,29 @@ DispatchResult Server::handle(std::string_view body) { } return reply(id, "result", diagnostics_->selection_ranges(*uri, selected)); } + if (*method == "workspace/willRenameFiles") { + if (!diagnostics_) + return error(id, -32601, "Method not found"); + const auto *files = as_array(member(params, "files")); + if (!files || files->size() > 4096) + return error(id, -32602, "Expected a bounded files array"); + std::vector renames; + for (const auto &item : *files) { + const auto *fields = as_object(&item); + const auto *old_uri = fields ? as_string(find_member(*fields, "oldUri")) : nullptr; + const auto *new_uri = fields ? as_string(find_member(*fields, "newUri")) : nullptr; + if (!old_uri || !new_uri) + return error(id, -32602, "Expected oldUri and newUri"); + const auto old_identity = Void::LanguageServer::Documents::normalize_uri(*old_uri); + const auto new_identity = Void::LanguageServer::Documents::normalize_uri(*new_uri); + if (!old_identity || !new_identity || !old_identity->is_file_uri || + !new_identity->is_file_uri) + return error(id, -32602, "Expected file URIs in the rename list"); + renames.push_back( + {old_identity->canonical_path.native(), new_identity->canonical_path.native()}); + } + return reply(id, "result", diagnostics_->renamed_files(renames)); + } if (*method == "textDocument/inlayHint") { if (!diagnostics_ || !diagnostics_->supports_occurrences()) return error(id, -32601, "Method not found"); diff --git a/src/features/IncludeUpdate.cpp b/src/features/IncludeUpdate.cpp new file mode 100644 index 0000000..a5750d0 --- /dev/null +++ b/src/features/IncludeUpdate.cpp @@ -0,0 +1,75 @@ +#include + +#include +#include + +namespace vls::features::include_update { +namespace fs = std::filesystem; +namespace { +bool blank(std::string_view text) { + return text.find_first_not_of(" \t") == std::string_view::npos; +} +} // namespace + +std::vector include_edits(std::string_view text, std::string_view document_path, + std::string_view old_path, std::string_view new_path, + const Limits &limits) { + std::vector edits; + if (document_path.size() > limits.pathBytes || old_path.size() > limits.pathBytes || + new_path.size() > limits.pathBytes || old_path.empty() || new_path.empty()) + return edits; + const fs::path directory = fs::path{document_path}.parent_path(); + const fs::path old_target = fs::path{old_path}.lexically_normal(); + const fs::path new_target = fs::path{new_path}.lexically_normal(); + const auto stop = std::min(text.size(), limits.scanBytes); + std::size_t offset{}; + 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")) { + 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); + const fs::path include{spelling}; + const auto resolved = include.is_absolute() + ? include.lexically_normal() + : (directory / include).lexically_normal(); + if (resolved == old_target) { + std::string replacement; + if (include.is_absolute()) + replacement = new_target.native(); + else { + auto relative = new_target.lexically_relative(directory); + if (relative.empty() || relative.native().starts_with("..")) + relative = new_target; + replacement = relative.native(); + if (spelling.starts_with("./") && !replacement.starts_with("./")) + replacement = "./" + replacement; + } + if (replacement.size() > limits.pathBytes) + return edits; + edits.push_back({open, close, std::move(replacement)}); + if (edits.size() == limits.edits) + return edits; + } + } + } else { + static_cast(blank(rest)); + } + } + } + offset = end + 1; + } + return edits; +} + +} // namespace vls::features::include_update diff --git a/src/semantic/EditorDiagnostics.cpp b/src/semantic/EditorDiagnostics.cpp index f8ba0ca..e419e1e 100644 --- a/src/semantic/EditorDiagnostics.cpp +++ b/src/semantic/EditorDiagnostics.cpp @@ -708,6 +708,38 @@ void EditorDiagnostics::files_changed(std::span renames) const { + auto &s = *state_; + Json::Object changes; + for (const auto &overlay : s.documents.list_overlays()) { + if (!overlay || !overlay->identity.is_file_uri) + continue; + for (const auto &rename : renames) { + const auto edits = features::include_update::include_edits( + overlay->text, overlay->identity.canonical_path.native(), rename.old_path, + rename.new_path); + if (edits.empty()) + continue; + auto &list = changes[overlay->identity.display_uri]; + if (!std::holds_alternative(list.value)) + list = Json{Json::Array{}}; + auto &array = std::get(list.value); + for (const auto &edit : edits) { + const auto start = overlay->offset_to_position(edit.start); + const auto end = overlay->offset_to_position(edit.end); + if (!start || !end) + continue; + array.push_back( + Json{Json::Object{{"range", range_json(Documents::Range{*start, *end})}, + {"newText", Json{edit.replacement}}}}); + } + } + } + if (changes.empty()) + return Json{nullptr}; + return Json{Json::Object{{"changes", Json{std::move(changes)}}}}; +} + 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 73546d7..8bf72b5 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) +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) 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/IncludeUpdateTests.cpp b/tests/features/IncludeUpdateTests.cpp new file mode 100644 index 0000000..dcb158e --- /dev/null +++ b/tests/features/IncludeUpdateTests.cpp @@ -0,0 +1,79 @@ +#include + +#include +#include + +#include + +namespace { +using namespace vls::features::include_update; +using Void::Testing::expect; +using Void::Testing::require; + +std::string replacement(const Edit &edit, std::string_view text) { + return std::string{text.substr(edit.start, edit.end - edit.start)}; +} +} // namespace + +VOID_TEST(IncludeUpdate, rewrites_a_quoted_include_resolved_from_the_document) { + const std::string text = "#pragma once\n#include \"extra.hpp\"\nint x;\n"; + const auto edits = + include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp"); + require(edits.size() == 1); + expect(replacement(edits.front(), text) == "extra.hpp"); + expect(edits.front().replacement == "renamed.hpp"); +} + +VOID_TEST(IncludeUpdate, keeps_relative_spelling_and_directory) { + const std::string text = "#include \"sub/extra.hpp\"\n"; + const auto edits = include_edits(text, "/project/main.cpp", "/project/sub/extra.hpp", + "/project/sub/renamed.hpp"); + require(edits.size() == 1); + expect(edits.front().replacement == "sub/renamed.hpp"); + const std::string dotted = "#include \"./extra.hpp\"\n"; + const auto prefixed = + include_edits(dotted, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp"); + require(prefixed.size() == 1); + expect(prefixed.front().replacement == "./renamed.hpp"); +} + +VOID_TEST(IncludeUpdate, leaves_unrelated_and_angle_includes_alone) { + const std::string text = + "#include \n#include \"other.hpp\"\n#include \"extra.hpp\"\n"; + const auto edits = + include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp"); + require(edits.size() == 1); + expect(replacement(edits.front(), text) == "extra.hpp"); + expect(edits.front().start > text.find("#include ")); +} + +VOID_TEST(IncludeUpdate, absolute_spelling_stays_absolute) { + const std::string text = "#include \"/project/extra.hpp\"\n"; + const auto edits = + include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp"); + require(edits.size() == 1); + expect(edits.front().replacement == "/project/renamed.hpp"); +} + +VOID_TEST(IncludeUpdate, unrelated_documents_and_paths_produce_nothing) { + const std::string text = "#include \"extra.hpp\"\n"; + expect(include_edits(text, "/other/main.cpp", "/project/extra.hpp", "/project/renamed.hpp") + .empty()); + expect(include_edits(text, "/project/main.cpp", "/project/missing.hpp", "/project/renamed.hpp") + .empty()); + expect(include_edits(text, "/project/main.cpp", "", "/project/renamed.hpp").empty()); +} + +VOID_TEST(IncludeUpdate, edits_are_bounded) { + std::string text; + for (int line = 0; line != 4; ++line) + text += "#include \"extra.hpp\"\n"; + expect(include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp") + .size() == 4); + expect(include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp", + Limits{.edits = 2}) + .size() == 2); + expect(include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp", + Limits{.pathBytes = 4}) + .empty()); +}