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
This commit is contained in:
Toby She
2025-11-12 23:43:25 -05:00
committed by GitHub
parent 4143bcbd37
commit 4dd9137215
2 changed files with 8 additions and 3 deletions

View File

@@ -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

View File

@@ -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)