mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	fix(lsp): rename undofile when renaming (#27684)
Problem: After `rename()`, the undo information for the renamed file(s) are lost. Solution: Rename the undofile as well.
This commit is contained in:
		| @@ -724,6 +724,13 @@ function M.rename(old_fname, new_fname, opts) | |||||||
|   local ok, err = os.rename(old_fname_full, new_fname) |   local ok, err = os.rename(old_fname_full, new_fname) | ||||||
|   assert(ok, err) |   assert(ok, err) | ||||||
|  |  | ||||||
|  |   local old_undofile = vim.fn.undofile(old_fname_full) | ||||||
|  |   if uv.fs_stat(old_undofile) ~= nil then | ||||||
|  |     local new_undofile = vim.fn.undofile(new_fname) | ||||||
|  |     vim.fn.mkdir(assert(vim.fs.dirname(new_undofile)), 'p') | ||||||
|  |     os.rename(old_undofile, new_undofile) | ||||||
|  |   end | ||||||
|  |  | ||||||
|   if vim.fn.isdirectory(new_fname) == 0 then |   if vim.fn.isdirectory(new_fname) == 0 then | ||||||
|     local newbuf = vim.fn.bufadd(new_fname) |     local newbuf = vim.fn.bufadd(new_fname) | ||||||
|     if win then |     if win then | ||||||
|   | |||||||
| @@ -2593,6 +2593,58 @@ describe('LSP', function() | |||||||
|         eq('New file', read_file(new)) |         eq('New file', read_file(new)) | ||||||
|       end |       end | ||||||
|     ) |     ) | ||||||
|  |     it('Maintains undo information for loaded buffer', function() | ||||||
|  |       local old = tmpname() | ||||||
|  |       write_file(old, 'line') | ||||||
|  |       local new = tmpname() | ||||||
|  |       os.remove(new) | ||||||
|  |  | ||||||
|  |       local undo_kept = exec_lua( | ||||||
|  |         [[ | ||||||
|  |         local old = select(1, ...) | ||||||
|  |         local new = select(2, ...) | ||||||
|  |         vim.opt.undofile = true | ||||||
|  |         vim.cmd.edit(old) | ||||||
|  |         vim.cmd.normal('dd') | ||||||
|  |         vim.cmd.write() | ||||||
|  |         local undotree = vim.fn.undotree() | ||||||
|  |         vim.lsp.util.rename(old, new) | ||||||
|  |         return vim.deep_equal(undotree, vim.fn.undotree()) | ||||||
|  |       ]], | ||||||
|  |         old, | ||||||
|  |         new | ||||||
|  |       ) | ||||||
|  |       eq(false, exec_lua('return vim.uv.fs_stat(...) ~= nil', old)) | ||||||
|  |       eq(true, exec_lua('return vim.uv.fs_stat(...) ~= nil', new)) | ||||||
|  |       eq(true, undo_kept) | ||||||
|  |     end) | ||||||
|  |     it('Maintains undo information for unloaded buffer', function() | ||||||
|  |       local old = tmpname() | ||||||
|  |       write_file(old, 'line') | ||||||
|  |       local new = tmpname() | ||||||
|  |       os.remove(new) | ||||||
|  |  | ||||||
|  |       local undo_kept = exec_lua( | ||||||
|  |         [[ | ||||||
|  |         local old = select(1, ...) | ||||||
|  |         local new = select(2, ...) | ||||||
|  |         vim.opt.undofile = true | ||||||
|  |         vim.cmd.split(old) | ||||||
|  |         vim.cmd.normal('dd') | ||||||
|  |         vim.cmd.write() | ||||||
|  |         local undotree = vim.fn.undotree() | ||||||
|  |         vim.cmd.bdelete() | ||||||
|  |         vim.lsp.util.rename(old, new) | ||||||
|  |         vim.cmd.edit(new) | ||||||
|  |         return vim.deep_equal(undotree, vim.fn.undotree()) | ||||||
|  |       ]], | ||||||
|  |         old, | ||||||
|  |         new | ||||||
|  |       ) | ||||||
|  |       eq(false, exec_lua('return vim.uv.fs_stat(...) ~= nil', old)) | ||||||
|  |       eq(true, exec_lua('return vim.uv.fs_stat(...) ~= nil', new)) | ||||||
|  |       eq(true, undo_kept) | ||||||
|  |     end) | ||||||
|     it('Does override target if overwrite is true', function() |     it('Does override target if overwrite is true', function() | ||||||
|       local old = tmpname() |       local old = tmpname() | ||||||
|       write_file(old, 'Old file') |       write_file(old, 'Old file') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jaehwang Jung
					Jaehwang Jung