From a075bd55d873d27059d3098ddcfbc6d980415925 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 02:31:17 +0300 Subject: [PATCH] Apply versioned workspace edits in the Neovim client apply_workspace_edit accepts a full WorkspaceEdit: a changes map or documentChanges with versioned TextDocumentEdits. Stale tracked versions and resource operations are refused before any buffer is touched, keeping the transactional rollback. The headless rename test covers the versioned apply and both refusals. --- clients/neovim/lua/vls/init.lua | 46 +++++++++++++++++++++++++---- docs/agent-workflow/LSP-PARITY.md | 2 +- docs/agent-workflow/VERIFICATION.md | 15 ++++++++++ tests/native/NeovimRename.lua | 30 ++++++++++++++++++- 4 files changed, 86 insertions(+), 7 deletions(-) diff --git a/clients/neovim/lua/vls/init.lua b/clients/neovim/lua/vls/init.lua index 647fb54..fff2393 100644 --- a/clients/neovim/lua/vls/init.lua +++ b/clients/neovim/lua/vls/init.lua @@ -276,13 +276,49 @@ local function before(left, right) return left.character > right.character end --- Applies an LSP `changes` map transactionally. Every range is validated --- before any buffer is touched; an application failure restores the exact --- pre-edit lines and modified state of every buffer already changed. -function M.apply_workspace_edit(changes) - if type(changes) ~= "table" then +-- Normalizes a WorkspaceEdit (`changes` or versioned `documentChanges`) to a +-- changes map. Resource operations and stale document versions are refused. +local function normalize_workspace_edit(edit) + if type(edit) ~= "table" then return nil, "no workspace edit changes" end + if edit.changes == nil and edit.documentChanges == nil then + return edit -- already a changes map + end + if edit.changes ~= nil then + return edit.changes + end + local changes = {} + for _, change in ipairs(edit.documentChanges) do + local document = change.textDocument + if type(document) ~= "table" or type(document.uri) ~= "string" or + type(change.edits) ~= "table" then + return nil, "unsupported document change" + end + local version = document.version + if version ~= nil then + local tracked = vim.lsp.util.buf_versions[vim.uri_to_bufnr(document.uri)] + if tracked ~= nil and tracked ~= version then + return nil, "stale document version for " .. document.uri + end + end + changes[document.uri] = changes[document.uri] or {} + for _, text_edit in ipairs(change.edits) do + changes[document.uri][#changes[document.uri] + 1] = text_edit + end + end + return changes +end + +-- Applies an LSP WorkspaceEdit (`changes` or `documentChanges`) transactionally. +-- Every range is validated before any buffer is touched; an application failure +-- restores the exact pre-edit lines and modified state of every buffer already +-- changed. +function M.apply_workspace_edit(edit) + local changes, message = normalize_workspace_edit(edit) + if not changes then + return nil, message + end local plan = {} for uri, edits in pairs(changes) do if type(edits) ~= "table" or #edits == 0 then diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md index 6223dda..71da3de 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. | Overload-set signature help | observed overloads are listed with the compiler-selected one first; visibility and overload resolution are not proven | | textDocument/formatting | needs a plan | | callHierarchy / typeHierarchy | prepare, incomingCalls and outgoingCalls are implemented and served; outgoing calls are verified over stdio now that the observer reports full function extents. Type hierarchy is open. | -| Client-side application of server-issued workspace edits | rename and code actions return edits the clients apply directly | +| Client-side application of server-issued workspace edits | Neovim applies `changes` and versioned `documentChanges`; VS Code applies `changes` | | Applying multi-file renames through client buffers | plans can be ready; application is client work | | richer token types | legend has `function` and `type` (class, base, definition names) plus a `declaration` modifier; more types need further compiler spans | diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index dbb6211..966f0b3 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -61,6 +61,21 @@ start coordinate only, so ranges are zero-width until an end is available. LSP adapters and index/rename/debugging integration remain open. `vbs test` passed all 17 native suites; CMake passed the reflection and trace suites. +## December 6 versioned workspace edits in the Neovim client + +`clients/neovim/lua/vls/init.lua` `apply_workspace_edit` now accepts a full +WorkspaceEdit: a plain `changes` map as before, or `documentChanges` with +versioned TextDocumentEdits. A document whose tracked LSP version differs is +refused before any buffer is touched, resource operations are refused +explicitly, and the existing transactional rollback is unchanged. +`tests/native/NeovimRename.lua` runs headless and now covers the versioned +apply, stale-version refusal and resource-operation refusal: +``` +Neovim rename passed: atomic apply, injected-failure rollback, invalid range, +read-only refusal, documentChanges and stale-version refusal +``` +The VS Code client still applies `changes` only. + ## December 6 reflection definition and capability advertisement `reflection::definition_at` selects the innermost entity covering a position diff --git a/tests/native/NeovimRename.lua b/tests/native/NeovimRename.lua index bc11520..83d68ba 100644 --- a/tests/native/NeovimRename.lua +++ b/tests/native/NeovimRename.lua @@ -82,4 +82,32 @@ assert(not vls.apply_workspace_edit(changes())) vim.bo[right_buffer].modifiable = true assert(snapshot(left_buffer) == before_left and snapshot(right_buffer) == before_right) -print("Neovim rename passed: atomic apply, injected-failure rollback, invalid range and read-only refusal") +-- documentChanges: versioned edits apply and stale versions are refused. +local doc_left_path, doc_right_path = vim.fn.tempname(), vim.fn.tempname() +vim.fn.writefile({ "value one", "value two" }, doc_left_path) +vim.fn.writefile({ "value three" }, doc_right_path) +local doc_left, doc_right = vim.fn.bufadd(doc_left_path), vim.fn.bufadd(doc_right_path) +vim.fn.bufload(doc_left) +vim.fn.bufload(doc_right) +local left_uri, right_uri = vim.uri_from_bufnr(doc_left), vim.uri_from_bufnr(doc_right) +vim.lsp.util.buf_versions[vim.uri_to_bufnr(left_uri)] = 7 +vim.lsp.util.buf_versions[vim.uri_to_bufnr(right_uri)] = 7 + +local function document_changes(version) + return { documentChanges = { + { textDocument = { uri = left_uri, version = version }, + edits = { edit(doc_left, 0, "value", "count"), edit(doc_left, 1, "value", "count") } }, + { textDocument = { uri = right_uri, version = version }, + edits = { edit(doc_right, 0, "value", "count") } } } } +end +local stale_ok, stale_err = vls.apply_workspace_edit(document_changes(6)) +assert(not stale_ok and stale_err:find("stale", 1, true), vim.inspect({ stale_ok, stale_err })) +assert(vim.api.nvim_buf_get_lines(doc_left, 0, 1, false)[1] == "value one") +local applied_ok, applied_err = vls.apply_workspace_edit(document_changes(7)) +assert(applied_ok, vim.inspect(applied_err)) +assert(vim.api.nvim_buf_get_lines(doc_left, 0, 1, false)[1] == "count one") +assert(vim.api.nvim_buf_get_lines(doc_right, 0, 1, false)[1] == "count three") +-- Resource operations are refused explicitly. +assert(not vls.apply_workspace_edit({ documentChanges = { { kind = "create", uri = left_uri } } })) + +print("Neovim rename passed: atomic apply, injected-failure rollback, invalid range, read-only refusal, documentChanges and stale-version refusal")