fix(lsp): check if the buffer is a directory before w! it (#22289)

This commit is contained in:
Eduard Baturin 2023-02-18 09:43:59 +03:00 committed by GitHub
parent 44da6a56ba
commit f43fa301c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -759,9 +759,11 @@ function M.rename(old_fname, new_fname, opts)
vim.fn.bufload(oldbuf) vim.fn.bufload(oldbuf)
-- The there may be pending changes in the buffer -- The there may be pending changes in the buffer
if vim.fn.isdirectory(old_fname) == 0 then
api.nvim_buf_call(oldbuf, function() api.nvim_buf_call(oldbuf, function()
vim.cmd('w!') vim.cmd('w!')
end) end)
end
local ok, err = os.rename(old_fname, new_fname) local ok, err = os.rename(old_fname, new_fname)
assert(ok, err) assert(ok, err)

View File

@ -2067,6 +2067,8 @@ describe('LSP', function()
end) end)
describe('lsp.util.rename', function() describe('lsp.util.rename', function()
local pathsep = helpers.get_pathsep()
it('Can rename an existing file', function() it('Can rename an existing file', function()
local old = helpers.tmpname() local old = helpers.tmpname()
write_file(old, 'Test content') write_file(old, 'Test content')
@ -2089,6 +2091,32 @@ describe('LSP', function()
eq(true, exists) eq(true, exists)
os.remove(new) os.remove(new)
end) 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() it('Does not rename file if target exists and ignoreIfExists is set or overwrite is false', function()
local old = helpers.tmpname() local old = helpers.tmpname()
write_file(old, 'Old File') write_file(old, 'Old File')