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.
This commit is contained in:
faumaray committed 2026-12-06 02:31:17 +03:00
1 parent 7e3ce6fc6c
commit a075bd55d8
4 files changed
+86 -7

No files matched your search

+41 -5
View File
@@ -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