Skip to content

Commit 21483b5

Browse files
committed
boot_ls 执行命令添加等待逻辑
如果 `jdtls` 在 `spring-boot` 之前启动,可能导致无法找到 `spring-boot` LSP Client
1 parent c4673ce commit 21483b5

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

lua/spring_boot/launch.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ M.setup = function(_)
142142
return
143143
end
144144
ls_config.init_options.workspaceFolders = ls_config.root_dir
145+
local on_init = ls_config.on_init
146+
ls_config.on_init = function(client, ctx)
147+
util.boot_ls_init(client, ctx)
148+
if on_init then
149+
on_init(client, ctx)
150+
end
151+
end
145152
local group = vim.api.nvim_create_augroup("spring_boot_ls", { clear = true })
146153
vim.api.nvim_create_autocmd({ "FileType" }, {
147154
group = group,

lua/spring_boot/util.lua

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,45 @@ M.get_client = function(name)
3737
end
3838

3939
M.get_spring_boot_client = function()
40-
return M.get_client("spring-boot")
40+
local clients = vim.lsp.get_clients({ name = "spring-boot" })
41+
if clients and #clients > 0 then
42+
return clients[1]
43+
end
44+
return nil
4145
end
42-
M.boot_execute_command = function(command, param, callback)
43-
local err, resp = M.execute_command(M.get_spring_boot_client(), command, param, callback)
46+
47+
M._boot_command_co = {}
48+
local function boot_client_execute_command(client, command, param, callback)
49+
local err, resp = M.execute_command(client, command, param, callback)
4450
if err then
4551
print("Error executeCommand: " .. command .. "\n" .. vim.inspect(err))
4652
end
4753
return resp
4854
end
4955

56+
M.boot_ls_init = function(_, _)
57+
for _, co in ipairs(M._boot_command_co) do
58+
coroutine.resume(co)
59+
end
60+
M._boot_command_co = {}
61+
end
62+
63+
M.boot_execute_command = function(command, param, callback)
64+
local client = M.get_spring_boot_client()
65+
if client then
66+
return boot_client_execute_command(client, command, param, callback)
67+
end
68+
vim.notify("Spring Boot LS is not ready, waiting for start", vim.log.levels.INFO)
69+
local pco = coroutine.running()
70+
local co = coroutine.create(function()
71+
local resp = boot_client_execute_command(M.get_spring_boot_client(), command, param, callback)
72+
coroutine.resume(pco, resp)
73+
return resp
74+
end)
75+
table.insert(M._boot_command_co, co)
76+
return coroutine.yield()
77+
end
78+
5079
M.execute_command = function(client, command, param, callback)
5180
local co
5281
if not callback then

0 commit comments

Comments
 (0)