-- 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) print("Neovim rename passed: atomic apply, injected-failure rollback, invalid range and read-only refusal")