diff options
author | Jaehwang Jung <tomtomjhj@gmail.com> | 2024-03-02 23:21:53 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-02 15:21:53 +0100 |
commit | dc8c086c7e73a9035c34be6416e7c465d61edc0e (patch) | |
tree | 1245199c87904a96e2d37ac14893a1cb11a7ce51 /test/functional/plugin/lsp_spec.lua | |
parent | 0d553c83474b8d024d74e9f66b77dc62380241ef (diff) | |
download | rneovim-dc8c086c7e73a9035c34be6416e7c465d61edc0e.tar.gz rneovim-dc8c086c7e73a9035c34be6416e7c465d61edc0e.tar.bz2 rneovim-dc8c086c7e73a9035c34be6416e7c465d61edc0e.zip |
fix(lsp): directly rename the existing buffers when renaming (#27690)
Problem:
`vim.lsp.util.rename()` deletes the buffers that are affected by
renaming. This has undesireable side effects. For example, when renaming
a directory, all buffers under that directory are deleted and windows
displaying those buffers are closed. Also, buffer options may change
after renaming.
Solution:
Rename the buffers with :saveas.
An alternative approach is to record all the relevant states and restore
it after renaming, but that seems to be more complex. In fact, the older
version was attempting to restore the states but only partially and
incorrectly.
Diffstat (limited to 'test/functional/plugin/lsp_spec.lua')
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 140 |
1 files changed, 47 insertions, 93 deletions
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 1e787d2b0c..4826153edb 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2383,12 +2383,13 @@ describe('LSP', function() [[ local old = select(1, ...) local new = select(2, ...) + local old_bufnr = vim.fn.bufadd(old) + vim.fn.bufload(old_bufnr) vim.lsp.util.rename(old, new) - - -- after rename the target file must have the contents of the source file - local bufnr = vim.fn.bufadd(new) - vim.fn.bufload(new) - return vim.api.nvim_buf_get_lines(bufnr, 0, -1, true) + -- the existing buffer is renamed in-place and its contents is kept + local new_bufnr = vim.fn.bufadd(new) + vim.fn.bufload(new_bufnr) + return (old_bufnr == new_bufnr) and vim.api.nvim_buf_get_lines(new_bufnr, 0, -1, true) ]], old, new @@ -2400,87 +2401,6 @@ describe('LSP', function() eq(true, exists) os.remove(new) end) - it('Kills old buffer after renaming an existing file', function() - local old = tmpname() - write_file(old, 'Test content') - local new = tmpname() - os.remove(new) -- only reserve the name, file must not exist for the test scenario - local lines = exec_lua( - [[ - local old = select(1, ...) - local oldbufnr = vim.fn.bufadd(old) - local new = select(2, ...) - vim.lsp.util.rename(old, new) - return vim.fn.bufloaded(oldbufnr) - ]], - old, - new - ) - eq(0, lines) - os.remove(new) - end) - it('new buffer remains unlisted and unloaded if the old was not in window before', function() - local old = tmpname() - write_file(old, 'Test content') - local new = tmpname() - os.remove(new) -- only reserve the name, file must not exist for the test scenario - local actual = exec_lua( - [[ - local old = select(1, ...) - local oldbufnr = vim.fn.bufadd(old) - local new = select(2, ...) - local newbufnr = vim.fn.bufadd(new) - vim.lsp.util.rename(old, new) - return { - buflisted = vim.bo[newbufnr].buflisted, - bufloaded = vim.api.nvim_buf_is_loaded(newbufnr) - } - ]], - old, - new - ) - - local expected = { - buflisted = false, - bufloaded = false, - } - - eq(expected, actual) - - os.remove(new) - end) - it('new buffer is listed and loaded if the old was in window before', function() - local old = tmpname() - write_file(old, 'Test content') - local new = tmpname() - os.remove(new) -- only reserve the name, file must not exist for the test scenario - local actual = exec_lua( - [[ - local win = vim.api.nvim_get_current_win() - local old = select(1, ...) - local oldbufnr = vim.fn.bufadd(old) - vim.api.nvim_win_set_buf(win, oldbufnr) - local new = select(2, ...) - vim.lsp.util.rename(old, new) - local newbufnr = vim.fn.bufadd(new) - return { - buflisted = vim.bo[newbufnr].buflisted, - bufloaded = vim.api.nvim_buf_is_loaded(newbufnr) - } - ]], - old, - new - ) - - local expected = { - buflisted = true, - bufloaded = true, - } - - eq(expected, actual) - - os.remove(new) - end) it('Can rename a directory', function() -- only reserve the name, file must not exist for the test scenario local old_dir = tmpname() @@ -2497,21 +2417,25 @@ describe('LSP', function() [[ local old_dir = select(1, ...) local new_dir = select(2, ...) - local pathsep = select(3, ...) - local oldbufnr = vim.fn.bufadd(old_dir .. pathsep .. 'file') - + local pathsep = select(3, ...) + local file = select(4, ...) + local old_bufnr = vim.fn.bufadd(old_dir .. pathsep .. file) + vim.fn.bufload(old_bufnr) vim.lsp.util.rename(old_dir, new_dir) - return vim.fn.bufloaded(oldbufnr) + -- the existing buffer is renamed in-place and its contents is kept + local new_bufnr = vim.fn.bufadd(new_dir .. pathsep .. file) + vim.fn.bufload(new_bufnr) + return (old_bufnr == new_bufnr) and vim.api.nvim_buf_get_lines(new_bufnr, 0, -1, true) ]], old_dir, new_dir, - pathsep + pathsep, + file ) - eq(0, lines) + eq({ 'Test content' }, lines) eq(false, exec_lua('return vim.uv.fs_stat(...) ~= nil', old_dir)) eq(true, exec_lua('return vim.uv.fs_stat(...) ~= nil', new_dir)) eq(true, exec_lua('return vim.uv.fs_stat(...) ~= nil', new_dir .. pathsep .. file)) - eq('Test content', read_file(new_dir .. pathsep .. file)) os.remove(new_dir) end) @@ -2609,6 +2533,11 @@ describe('LSP', function() vim.cmd.write() local undotree = vim.fn.undotree() vim.lsp.util.rename(old, new) + -- Renaming uses :saveas, which updates the "last write" information. + -- Other than that, the undotree should remain the same. + undotree.save_cur = undotree.save_cur + 1 + undotree.save_last = undotree.save_last + 1 + undotree.entries[1].save = undotree.entries[1].save + 1 return vim.deep_equal(undotree, vim.fn.undotree()) ]], old, @@ -2645,6 +2574,31 @@ describe('LSP', function() eq(true, exec_lua('return vim.uv.fs_stat(...) ~= nil', new)) eq(true, undo_kept) end) + it('Does not rename file when it conflicts with a buffer without file', function() + local old = tmpname() + write_file(old, 'Old File') + local new = tmpname() + os.remove(new) + + local lines = exec_lua( + [[ + local old = select(1, ...) + local new = select(2, ...) + local old_buf = vim.fn.bufadd(old) + vim.fn.bufload(old_buf) + local conflict_buf = vim.api.nvim_create_buf(true, false) + vim.api.nvim_buf_set_name(conflict_buf, new) + vim.api.nvim_buf_set_lines(conflict_buf, 0, -1, true, {'conflict'}) + vim.api.nvim_win_set_buf(0, conflict_buf) + vim.lsp.util.rename(old, new) + return vim.api.nvim_buf_get_lines(conflict_buf, 0, -1, true) + ]], + old, + new + ) + eq({ 'conflict' }, lines) + eq('Old File', read_file(old)) + end) it('Does override target if overwrite is true', function() local old = tmpname() write_file(old, 'Old file') |