aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduard Baturin <trardone@gmail.com>2023-02-18 09:43:59 +0300
committerGitHub <noreply@github.com>2023-02-18 07:43:59 +0100
commitf43fa301c1a2817239e046a242902af65b7cac71 (patch)
treee6f5726770c297e2e4de71546d30f804b6708656
parent44da6a56ba228a97fa917e1ae76a46299b8db125 (diff)
downloadrneovim-f43fa301c1a2817239e046a242902af65b7cac71.tar.gz
rneovim-f43fa301c1a2817239e046a242902af65b7cac71.tar.bz2
rneovim-f43fa301c1a2817239e046a242902af65b7cac71.zip
fix(lsp): check if the buffer is a directory before w! it (#22289)
-rw-r--r--runtime/lua/vim/lsp/util.lua8
-rw-r--r--test/functional/plugin/lsp_spec.lua28
2 files changed, 33 insertions, 3 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 38051e6410..4beb4fc367 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -759,9 +759,11 @@ function M.rename(old_fname, new_fname, opts)
vim.fn.bufload(oldbuf)
-- The there may be pending changes in the buffer
- api.nvim_buf_call(oldbuf, function()
- vim.cmd('w!')
- end)
+ if vim.fn.isdirectory(old_fname) == 0 then
+ api.nvim_buf_call(oldbuf, function()
+ vim.cmd('w!')
+ end)
+ end
local ok, err = os.rename(old_fname, new_fname)
assert(ok, err)
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua
index fd162961ff..f1aad08140 100644
--- a/test/functional/plugin/lsp_spec.lua
+++ b/test/functional/plugin/lsp_spec.lua
@@ -2067,6 +2067,8 @@ describe('LSP', function()
end)
describe('lsp.util.rename', function()
+ local pathsep = helpers.get_pathsep()
+
it('Can rename an existing file', function()
local old = helpers.tmpname()
write_file(old, 'Test content')
@@ -2089,6 +2091,32 @@ describe('LSP', function()
eq(true, exists)
os.remove(new)
end)
+ it('Can rename a direcory', function()
+ -- only reserve the name, file must not exist for the test scenario
+ local old_dir = helpers.tmpname()
+ local new_dir = helpers.tmpname()
+ os.remove(old_dir)
+ os.remove(new_dir)
+
+ helpers.mkdir_p(old_dir)
+
+ local file = "file"
+ write_file(old_dir .. pathsep .. file, 'Test content')
+
+ exec_lua([[
+ local old_dir = select(1, ...)
+ local new_dir = select(2, ...)
+
+ vim.lsp.util.rename(old_dir, new_dir)
+ ]], old_dir, new_dir)
+
+ 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))
+ eq('Test content', read_file(new_dir .. pathsep .. file))
+
+ os.remove(new_dir)
+ end)
it('Does not rename file if target exists and ignoreIfExists is set or overwrite is false', function()
local old = helpers.tmpname()
write_file(old, 'Old File')