From 4f7879dff0f0dc22ddf4cb2a2095b88605a3bab0 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 14 Mar 2023 13:08:37 +0100 Subject: fix(lsp): kill buffers after renaming a directory #22618 Problem: When LSP client renames a directory, opened buffers in the edfitor are not renamed or closed. Then `:wall` shows errors. https://github.com/neovim/neovim/blob/master/runtime/lua/vim/lsp/util.lua#L776 works correctly if you try to rename a single file, but doesn't delete old buffers with `old_fname` is a dir. Solution: Update the logic in runtime/lua/vim/lsp/util.lua:rename() Fixes #22617 --- test/functional/plugin/lsp_spec.lua | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'test/functional/plugin') diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index ddbeef6d84..8b5e7d56ac 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -2200,7 +2200,22 @@ describe('LSP', function() eq(true, exists) os.remove(new) end) - it('Can rename a direcory', function() + it("Kills old buffer after renaming an existing file", function() + local old = helpers.tmpname() + write_file(old, 'Test content') + local new = helpers.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('Can rename a directory', function() -- only reserve the name, file must not exist for the test scenario local old_dir = helpers.tmpname() local new_dir = helpers.tmpname() @@ -2209,16 +2224,19 @@ describe('LSP', function() helpers.mkdir_p(old_dir) - local file = "file" + local file = 'file.txt' write_file(old_dir .. pathsep .. file, 'Test content') - exec_lua([[ + local lines = exec_lua([[ local old_dir = select(1, ...) local new_dir = select(2, ...) + local pathsep = select(3, ...) + local oldbufnr = vim.fn.bufadd(old_dir .. pathsep .. 'file') vim.lsp.util.rename(old_dir, new_dir) - ]], old_dir, new_dir) - + return vim.fn.bufloaded(oldbufnr) + ]], old_dir, new_dir, pathsep) + eq(0, lines) eq(false, exec_lua('return vim.loop.fs_stat(...) ~= nil', old_dir)) eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', new_dir)) eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', new_dir .. pathsep .. file)) -- cgit