Skip to content

Commit f74562a

Browse files
committed
fix: route java/* requests to the jdtls client only
vim.lsp.buf_request broadcasts to every client attached to the buffer, so non-jdtls clients (spring-boot LS, ...) answered custom java/* methods with MethodNotFound (-32601) errors on every mapping. Add lsp.jdtls_request/jdtls_notify and use them for all jdtls calls; send workspace/didChangeConfiguration as a notification as the spec demands.
1 parent bdde213 commit f74562a

2 files changed

Lines changed: 33 additions & 15 deletions

File tree

lua/jc/jdtls.lua

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ local function choose_imports(params, _)
4545
end
4646

4747
local function set_configuration(settings)
48-
vim.lsp.buf_request(0, "workspace/didChangeConfiguration", {
49-
settings = settings,
50-
}, function() end)
48+
-- didChangeConfiguration is a notification, and only jdtls should get it
49+
lsp.jdtls_notify("workspace/didChangeConfiguration", { settings = settings })
5150
end
5251

5352
local client_commands = {
@@ -63,7 +62,7 @@ vim.lsp.handlers["workspace/executeClientCommand"] = function(_, params, ctx)
6362
end
6463

6564
local function document_symbols(callback)
66-
vim.lsp.buf_request(
65+
lsp.jdtls_request(
6766
0,
6867
"textDocument/documentSymbol",
6968
{ textDocument = vim.lsp.util.make_text_document_params() },
@@ -98,7 +97,7 @@ function M.generate_accessors(fields)
9897
if not fields then
9998
local params = make_range_params()
10099
params.kind = 2
101-
vim.lsp.buf_request(0, "java/resolveUnimplementedAccessors", params, function(err, resp)
100+
lsp.jdtls_request(0, "java/resolveUnimplementedAccessors", params, function(err, resp)
102101
if resp then
103102
vim.fn["generators#GenerateAccessors"](resp)
104103
else
@@ -110,7 +109,7 @@ function M.generate_accessors(fields)
110109
["java.codeGeneration.insertionLocation"] = "lastMember",
111110
})
112111

113-
vim.lsp.buf_request(0, "java/generateAccessors", {
112+
lsp.jdtls_request(0, "java/generateAccessors", {
114113
context = make_range_params(),
115114
accessors = fields,
116115
}, apply_edit)
@@ -159,7 +158,7 @@ function M.generate_abstractMethods()
159158
line = line,
160159
},
161160
}
162-
vim.lsp.buf_request(curbuf, "textDocument/codeAction", params, function(err, actions)
161+
lsp.jdtls_request(curbuf, "textDocument/codeAction", params, function(err, actions)
163162
if actions then
164163
local add_method_action = nil
165164
for _, action in ipairs(actions) do
@@ -169,7 +168,7 @@ function M.generate_abstractMethods()
169168
end
170169
end
171170
if add_method_action then
172-
vim.lsp.buf_request(curbuf, "codeAction/resolve", add_method_action, apply_edit)
171+
lsp.jdtls_request(curbuf, "codeAction/resolve", add_method_action, apply_edit)
173172
else
174173
vim.notify("No action found", vim.log.levels.INFO)
175174
end
@@ -182,7 +181,7 @@ end
182181

183182
function M.generate_constructor(fields, params, opts)
184183
if fields == nil then
185-
vim.lsp.buf_request(0, "java/checkConstructorsStatus", make_range_params(), function(err, resp)
184+
lsp.jdtls_request(0, "java/checkConstructorsStatus", make_range_params(), function(err, resp)
186185
if resp then
187186
vim.fn["generators#GenerateConstructor"](resp.fields, resp.constructors, opts)
188187
else
@@ -202,7 +201,7 @@ function M.generate_constructor(fields, params, opts)
202201
diagnostics = {},
203202
only = nil,
204203
}
205-
vim.lsp.buf_request(0, "java/generateConstructors", {
204+
lsp.jdtls_request(0, "java/generateConstructors", {
206205
context = context,
207206
fields = fields,
208207
constructors = params.constructors,
@@ -212,7 +211,7 @@ end
212211

213212
function M.generate_hashCodeAndEquals(fields)
214213
if not fields then
215-
vim.lsp.buf_request(0, "java/checkHashCodeEqualsStatus", make_range_params(), function(err, resp)
214+
lsp.jdtls_request(0, "java/checkHashCodeEqualsStatus", make_range_params(), function(err, resp)
216215
if resp then
217216
vim.fn["generators#GenerateHashCodeAndEquals"](resp.fields)
218217
else
@@ -224,7 +223,7 @@ function M.generate_hashCodeAndEquals(fields)
224223
["java.codeGeneration.insertionLocation"] = "lastMember",
225224
})
226225

227-
vim.lsp.buf_request(0, "java/generateHashCodeEquals", {
226+
lsp.jdtls_request(0, "java/generateHashCodeEquals", {
228227
context = make_range_params(),
229228
fields = fields,
230229
regenerate = true,
@@ -234,7 +233,7 @@ end
234233

235234
function M.generate_toString(fields, params)
236235
if not fields then
237-
vim.lsp.buf_request(0, "java/checkToStringStatus", make_range_params(), function(err, resp)
236+
lsp.jdtls_request(0, "java/checkToStringStatus", make_range_params(), function(err, resp)
238237
if resp then
239238
vim.fn["generators#GenerateToString"](resp.fields)
240239
else
@@ -247,7 +246,7 @@ function M.generate_toString(fields, params)
247246
["java.codeGeneration.insertionLocation"] = "lastMember",
248247
})
249248

250-
vim.lsp.buf_request(0, "java/generateToString", {
249+
lsp.jdtls_request(0, "java/generateToString", {
251250
context = make_range_params(),
252251
fields = fields,
253252
}, apply_edit)
@@ -256,7 +255,7 @@ end
256255

257256
function M.organize_imports(bn, smart)
258257
M.organize_imports_smart = smart
259-
vim.lsp.buf_request(bn, "java/organizeImports", make_range_params(), apply_edit)
258+
lsp.jdtls_request(bn, "java/organizeImports", make_range_params(), apply_edit)
260259
end
261260

262261
-- jdt.ls declares java/projectConfigurationUpdate as a JsonNotification:

lua/jc/lsp.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ function M.executeCommand(command, callback, on_failure)
4444
end
4545
end
4646

47+
-- send a request to the jdtls client only — vim.lsp.buf_request would
48+
-- broadcast java/* methods to every client attached to the buffer and
49+
-- non-jdtls ones (spring-boot LS, ...) answer with MethodNotFound errors
50+
function M.jdtls_request(bufnr, method, params, handler)
51+
local client = M.get_jdtls_client()
52+
if not client then
53+
vim.notify("jc: no jdtls client attached", vim.log.levels.ERROR)
54+
return
55+
end
56+
client:request(method, params, handler, bufnr)
57+
end
58+
59+
function M.jdtls_notify(method, params)
60+
local client = M.get_jdtls_client()
61+
if client then
62+
client:notify(method, params)
63+
end
64+
end
65+
4766
function M.get_jdtls_client()
4867
local clients = vim.lsp.get_clients()
4968
for _, client in ipairs(clients) do

0 commit comments

Comments
 (0)