fix(lsp): remove unnecessary file load/write when renaming (#27621)

Previously rename would unconditionally read the to-be-renamed file from the
disk and write it to the disk. This is redundant in some cases

If the file is not already loaded, it's not attached to lsp client, so nvim
doesn't need to care about this file.
If the file is loaded but has no change, it doesn't need to be written.
This commit is contained in:
Jaehwang Jung 2024-02-29 01:32:25 +09:00 committed by GitHub
parent 2f85bbe615
commit 7311958e12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 6 deletions

View File

@ -710,11 +710,12 @@ function M.rename(old_fname, new_fname, opts)
end
for _, b in ipairs(oldbufs) do
vim.fn.bufload(b)
-- The there may be pending changes in the buffer
api.nvim_buf_call(b, function()
vim.cmd('w!')
end)
-- There may be pending changes in the buffer
if api.nvim_buf_is_loaded(b) then
api.nvim_buf_call(b, function()
vim.cmd('update!')
end)
end
end
local newdir = assert(vim.fs.dirname(new_fname))

View File

@ -2611,7 +2611,7 @@ describe('LSP', function()
eq(false, exec_lua('return vim.uv.fs_stat(...) ~= nil', old))
eq(true, exec_lua('return vim.uv.fs_stat(...) ~= nil', new))
eq('Old file\n', read_file(new))
eq('Old file', read_file(new))
end)
end)