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.
114 lines
5.0 KiB
Lua
114 lines
5.0 KiB
Lua
-- Run with nvim --headless -u NONE -i NONE -l tests/native/NeovimRename.lua.
|
|
local repository = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h:h:h")
|
|
vim.opt.runtimepath:prepend(repository .. "/clients/neovim")
|
|
local vls = require("vls")
|
|
|
|
local root = vim.fn.tempname()
|
|
vim.fn.mkdir(root .. "/left", "p")
|
|
vim.fn.mkdir(root .. "/right", "p")
|
|
local left = root .. "/left/unit.cpp"
|
|
local right = root .. "/right/header.hpp"
|
|
vim.fn.writefile({ "int value = 17;", "int read() { return value; }" }, left)
|
|
vim.fn.writefile({ "int value = 3;" }, right)
|
|
|
|
local function load(path)
|
|
vim.cmd.edit(vim.fn.fnameescape(path))
|
|
return vim.api.nvim_get_current_buf()
|
|
end
|
|
local left_buffer = load(left)
|
|
local right_buffer = load(right)
|
|
|
|
local function snapshot(buffer)
|
|
return table.concat(vim.api.nvim_buf_get_lines(buffer, 0, -1, false), "\n")
|
|
end
|
|
|
|
local function edit(buffer, line, spelling, replacement)
|
|
local text = vim.api.nvim_buf_get_lines(buffer, line, line + 1, false)[1]
|
|
local start = assert(text:find(spelling, 1, true)) - 1
|
|
return { range = { start = { line = line, character = start },
|
|
["end"] = { line = line, character = start + #spelling } },
|
|
newText = replacement }
|
|
end
|
|
|
|
local function changes()
|
|
return {
|
|
[vim.uri_from_bufnr(left_buffer)] = {
|
|
edit(left_buffer, 0, "value", "count"),
|
|
edit(left_buffer, 1, "value", "count"),
|
|
},
|
|
[vim.uri_from_bufnr(right_buffer)] = {
|
|
edit(right_buffer, 0, "value", "count"),
|
|
},
|
|
}
|
|
end
|
|
|
|
local before_left, before_right = snapshot(left_buffer), snapshot(right_buffer)
|
|
assert(vls.apply_workspace_edit(changes()))
|
|
assert(snapshot(left_buffer):find("count", 1, true))
|
|
assert(snapshot(right_buffer):find("count", 1, true))
|
|
|
|
vim.api.nvim_buf_set_lines(left_buffer, 0, -1, false,
|
|
{ "int value = 17;", "int read() { return value; }" })
|
|
vim.api.nvim_buf_set_lines(right_buffer, 0, -1, false, { "int value = 3;" })
|
|
vim.bo[left_buffer].modified = false
|
|
vim.bo[right_buffer].modified = false
|
|
assert(snapshot(left_buffer) == before_left and snapshot(right_buffer) == before_right)
|
|
|
|
-- Inject a failure in the second buffer write and require a full rollback.
|
|
local original = vim.api.nvim_buf_set_text
|
|
local calls = 0
|
|
vim.api.nvim_buf_set_text = function(...)
|
|
calls = calls + 1
|
|
if calls == 2 then
|
|
error("injected buffer failure")
|
|
end
|
|
return original(...)
|
|
end
|
|
local ok, err = vls.apply_workspace_edit(changes())
|
|
vim.api.nvim_buf_set_text = original
|
|
assert(not ok and err:find("injected", 1, true), vim.inspect({ ok, err }))
|
|
assert(snapshot(left_buffer) == before_left and snapshot(right_buffer) == before_right)
|
|
assert(not vim.bo[left_buffer].modified and not vim.bo[right_buffer].modified)
|
|
|
|
-- An invalid range is rejected before any buffer is touched.
|
|
local invalid = changes()
|
|
invalid[vim.uri_from_bufnr(right_buffer)][1].range.start.line = 99
|
|
assert(not vls.apply_workspace_edit(invalid))
|
|
assert(snapshot(left_buffer) == before_left and snapshot(right_buffer) == before_right)
|
|
|
|
-- A read-only buffer refuses the whole transaction.
|
|
vim.bo[right_buffer].modifiable = false
|
|
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)
|
|
|
|
-- 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")
|