Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this check whether 'buftype' is set on the old buffer? E.g. could the old buffer be a :terminal or other non-file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far as I know, LSP can rename only regular files and directories.

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