From 365811ecf97a08d0e2055fba210d65017344fd15 Mon Sep 17 00:00:00 2001 From: fengwk <759543714@qq.com> Date: Wed, 24 May 2023 00:31:23 +0800 Subject: [PATCH] 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 --- lua/jdtls/setup.lua | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lua/jdtls/setup.lua b/lua/jdtls/setup.lua index a900cba..0b6b393 100644 --- a/lua/jdtls/setup.lua +++ b/lua/jdtls/setup.lua @@ -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')