aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/util.lua
diff options
context:
space:
mode:
authorMathias Fußenegger <mfussenegger@users.noreply.github.com>2021-11-14 12:55:16 +0100
committerGitHub <noreply@github.com>2021-11-14 12:55:16 +0100
commitee3a58d42e7fce666eef570db6f2944c29303d98 (patch)
tree9421bec34f8700525c77f1a931643583b5d6aa7f /runtime/lua/vim/lsp/util.lua
parent2ef9d2a663db35c73b93606dbe882ca697072cc3 (diff)
downloadrneovim-ee3a58d42e7fce666eef570db6f2944c29303d98.tar.gz
rneovim-ee3a58d42e7fce666eef570db6f2944c29303d98.tar.bz2
rneovim-ee3a58d42e7fce666eef570db6f2944c29303d98.zip
fix(lsp): ensure buffers are re-attached on rename (#16266)
If a LSP server sent a workspace edit containing a rename the buffers file name changed without the server receiving a close notification for the old buffer and without the client properly re-attaching on the new file. This affected `Move` code-actions in nvim-jdtls, but also `vim.lsp.buf.rename` on a class level.
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r--runtime/lua/vim/lsp/util.lua21
1 files changed, 16 insertions, 5 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 27210dc385..a4b7b9922b 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -549,18 +549,29 @@ end
-- ignoreIfExists? bool
function M.rename(old_fname, new_fname, opts)
opts = opts or {}
- local bufnr = vim.fn.bufadd(old_fname)
- vim.fn.bufload(bufnr)
local target_exists = vim.loop.fs_stat(new_fname) ~= nil
if target_exists and not opts.overwrite or opts.ignoreIfExists then
vim.notify('Rename target already exists. Skipping rename.')
return
end
+ local oldbuf = vim.fn.bufadd(old_fname)
+ vim.fn.bufload(oldbuf)
+
+ -- The there may be pending changes in the buffer
+ api.nvim_buf_call(oldbuf, function()
+ vim.cmd('w!')
+ end)
+
local ok, err = os.rename(old_fname, new_fname)
assert(ok, err)
- api.nvim_buf_call(bufnr, function()
- vim.cmd('saveas! ' .. vim.fn.fnameescape(new_fname))
- end)
+
+ local newbuf = vim.fn.bufadd(new_fname)
+ for _, win in pairs(api.nvim_list_wins()) do
+ if api.nvim_win_get_buf(win) == oldbuf then
+ api.nvim_win_set_buf(win, newbuf)
+ end
+ end
+ api.nvim_buf_delete(oldbuf, { force = true })
end