Skip to content

Commit

Permalink
fix(lsp): check if the buffer is a directory before w! it (neovim#22289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trard authored and yesean committed Mar 25, 2023
1 parent 49bb5ab commit 48ee947
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 5 additions & 3 deletions runtime/lua/vim/lsp/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions test/functional/plugin/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 48ee947

Please sign in to comment.