Return client_id? from start_or_attach

Variant of https://github.com/mfussenegger/nvim-jdtls/pull/544 that
doesn't change behavior

Co-authored-by: y1rn <y1rn66@gmail.com>
This commit is contained in:
Mathias Fussenegger 2023-09-14 14:49:42 +02:00 committed by Mathias Fußenegger
parent be1634aa57
commit f7cc47a42b
3 changed files with 30 additions and 15 deletions

View file

@ -1,9 +1,13 @@
==============================================================================
LSP extensions for Neovim and eclipse.jdt.ls *jdtls*
M.start_or_attach() *jdtls.start_or_attach*
M.start_or_attach({config}) *jdtls.start_or_attach*
Start the language server (if not started), and attach the current buffer.
@param config table configuration. See |vim.lsp.start_client|
Parameters: ~
{config} (table) configuration. See |vim.lsp.start_client|
@return integer? client_id
M.organize_imports() *jdtls.organize_imports*

View file

@ -30,9 +30,11 @@ local M = {
}
--- Start the language server (if not started), and attach the current buffer.
--- @param config table configuration. See |vim.lsp.start_client|
---
---@param config table configuration. See |vim.lsp.start_client|
---@return integer? client_id
function M.start_or_attach(config)
setup.start_or_attach(config)
return setup.start_or_attach(config)
end

View file

@ -39,32 +39,39 @@ local function may_jdtls_buf(bufnr)
return vim.endswith(fname, "build.gradle") or vim.endswith(fname, "pom.xml")
end
---@return integer? client_id
local function attach_to_active_buf(bufnr, client_name)
local function try_attach(buf)
if not may_jdtls_buf(buf) then
return false
return nil
end
local clients = get_clients({ bufnr = buf, name = client_name })
local _, client = next(clients)
if client then
lsp.buf_attach_client(bufnr, client.id)
return true
return client.id
end
return false
return nil
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
if altbuf and altbuf > 0 then
local client_id = try_attach(altbuf)
if client_id then
return client_id
end
end
for _, buf in ipairs(api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(buf) and try_attach(buf) then
return true
if api.nvim_buf_is_loaded(buf) then
local client_id = try_attach(buf)
if client_id then
return client_id
end
end
end
print('No active LSP client found to use for jdt:// document')
return false
return nil
end
@ -244,6 +251,7 @@ end
---@param opts? jdtls.start.opts
---@return integer? client_id
function M.start_or_attach(config, opts)
opts = opts or {}
assert(config, 'config is required')
@ -266,8 +274,9 @@ function M.start_or_attach(config, opts)
-- Won't be able to get the correct root path for jdt:// URIs
-- So need to connect to an existing client
if vim.startswith(bufname, 'jdt://') then
if attach_to_active_buf(bufnr, config.name) then
return
local client_id = attach_to_active_buf(bufnr, config.name)
if client_id then
return client_id
end
end
@ -306,7 +315,7 @@ function M.start_or_attach(config, opts)
}
})
maybe_implicit_save()
vim.lsp.start(config)
return vim.lsp.start(config)
end