commit 4dd9137215f88bb6eabe386a7e474c59783a6486
parent 4143bcbd37572bc40e242867ab60675a33cfad0f
Author: Toby She <dr.tobyshe@gmail.com>
Date: Wed, 12 Nov 2025 23:43:25 -0500
fix(lsp): reuse_win prioritizes windows/tabs currently displayed #36486
Problem: reuse_win will always jump to the first window containing the
target buffer rather even if the buffer is displayed in the current
window/tab
Solution: check to see if the buffer is already displayed in the
current window or any window of the current buffer
Diffstat:
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
@@ -243,8 +243,9 @@ local function get_locations(method, opts)
vim.bo[b].buflisted = true
local w = win
- if opts.reuse_win then
- w = vim.fn.win_findbuf(b)[1] or w
+ if opts.reuse_win and api.nvim_win_get_buf(w) ~= b then
+ w = vim.fn.bufwinid(b)
+ w = w >= 0 and w or vim.fn.win_findbuf(b)[1] or win
if w ~= win then
api.nvim_set_current_win(w)
end
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua
@@ -5526,11 +5526,15 @@ describe('LSP', function()
eq(3, result.tagstack.items[1].from[2])
eq(7, result.tagstack.items[1].from[3])
+ local result_bufnr = api.nvim_get_current_buf()
+ n.feed(':tabe<CR>')
+ api.nvim_win_set_buf(0, result_bufnr)
+ local displayed_result_win = api.nvim_get_current_win()
n.feed(':vnew<CR>')
api.nvim_win_set_buf(0, result.bufnr)
api.nvim_win_set_cursor(0, { 3, 6 })
n.feed(':=vim.lsp.buf.definition({ reuse_win = true })<CR>')
- eq(result.win, api.nvim_get_current_win())
+ eq(displayed_result_win, api.nvim_get_current_win())
exec_lua(function()
vim.lsp.get_client_by_id(result.client_id):stop()
end)