Prefer alt-buffer client for attachment when handling jdt:// uri (#493)

When there are multiple clients it could use the wrong one. This prioritizes clients attached to the alt-buffer. Given that `jdt://` URLs are often opened by using goto-definition it has a high chance of being the right one.

Co-authored-by: Mathias Fussenegger <f.mathias@zignar.net>
This commit is contained in:
fengwk 2023-05-24 00:31:23 +08:00 committed by GitHub
parent 355de7458f
commit 365811ecf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -63,21 +63,35 @@ M.restart = lsp_clients.restart
local function attach_to_active_buf(bufnr, client_name)
for _, buf in pairs(vim.fn.getbufinfo({bufloaded=true})) do
if api.nvim_buf_get_option(buf.bufnr, 'filetype') == 'java' then
local clients = lsp.buf_get_clients(buf.bufnr)
for _, client in ipairs(clients) do
if client.config.name == client_name then
lsp.buf_attach_client(bufnr, client.id)
return true
end
end
local function try_attach(buf)
if vim.bo[buf].filetype ~= "java" then
return false
end
local clients = vim.lsp.get_active_clients({ bufnr = buf, name = client_name })
local _, client = next(clients)
if client then
lsp.buf_attach_client(bufnr, client.id)
return true
end
return false
end
---@diagnostic disable-next-line: param-type-mismatch
local altbuf = vim.fn.bufnr("#", -1)
if altbuf and altbuf > 0 and try_attach(altbuf) then
return true
end
for _, buf in ipairs(api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(buf) and try_attach(buf) then
return true
end
end
print('No active LSP client found to use for jdt:// document')
return false
end
function M.find_root(markers, bufname)
bufname = bufname or api.nvim_buf_get_name(api.nvim_get_current_buf())
local dirname = vim.fn.fnamemodify(bufname, ':p:h')