Skip to content

Commit

Permalink
Improve support for switching buffer right after triggering test
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Jan 22, 2025
1 parent 7758410 commit 922dca1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
24 changes: 18 additions & 6 deletions lua/jdtls/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ local function fetch_candidates(context, on_candidates)
local params = {
arguments = { context.uri };
}
for _, c in ipairs(get_clients({ bufnr = context.bufnr })) do
local clients = get_clients({ bufnr = context.bufnr })
if not next(clients) then
clients = get_clients({ name = "jdtls" })
end
for _, c in ipairs(clients) do
local command_provider = c.server_capabilities.executeCommandProvider
local commands = type(command_provider) == 'table' and command_provider.commands or {}
if vim.tbl_contains(commands, cmd_codelens) then
Expand Down Expand Up @@ -238,8 +242,8 @@ end
local function fetch_launch_args(lens, context, on_launch_args)
local req_arguments = make_request_args(lens, context.uri)
local cmd_junit_args = {
command = 'vscode.java.test.junit.argument';
arguments = { vim.fn.json_encode(req_arguments) };
command = 'vscode.java.test.junit.argument',
arguments = { vim.fn.json_encode(req_arguments) },
}
util.execute_command(cmd_junit_args, function(err, launch_args)
if err then
Expand All @@ -263,11 +267,19 @@ local function fetch_launch_args(lens, context, on_launch_args)
-- That is why `java.project.getClasspaths` is used as well.
local options = vim.fn.json_encode({ scope = 'test'; })
local cmd = {
command = 'java.project.getClasspaths';
arguments = { vim.uri_from_bufnr(0), options };
command = 'java.project.getClasspaths',
arguments = { vim.uri_from_bufnr(context.bufnr), options },
}
util.execute_command(cmd, function(err1, resp)
assert(not err1, vim.inspect(err1))
if err1 then
local msg = string.format(
"%s bufnr=%d fname=%s",
err1.message,
context.bufnr,
api.nvim_buf_get_name(context.bufnr)
)
error(msg)
end
launch_args.classpath = merge_unique(launch_args.classpath, resp.classpaths)
on_launch_args(launch_args)
end, context.bufnr)
Expand Down
14 changes: 6 additions & 8 deletions lua/jdtls/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ local get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients
function M.execute_command(command, callback, bufnr)
local clients = {}
local candidates = get_clients({ bufnr = bufnr })
if not next(candidates) then
candidates = get_clients({ name = "jdtls" })
end
for _, c in pairs(candidates) do
local command_provider = c.server_capabilities.executeCommandProvider
local commands = type(command_provider) == 'table' and command_provider.commands or {}
Expand All @@ -17,13 +20,8 @@ function M.execute_command(command, callback, bufnr)
end
local num_clients = vim.tbl_count(clients)
if num_clients == 0 then
if bufnr then
-- User could've switched buffer to non-java file, try all clients
return M.execute_command(command, callback, nil)
else
vim.notify('No LSP client found that supports ' .. command.command, vim.log.levels.ERROR)
return
end
vim.notify('No LSP client found that supports ' .. command.command, vim.log.levels.ERROR)
return
end

if num_clients > 1 then
Expand Down Expand Up @@ -61,7 +59,7 @@ function M.with_java_executable(mainclass, project, fn, bufnr)

local client = get_clients({ name = "jdtls", bufnr = bufnr, method = "workspace/executeCommand" })[1]
if not client then
print("No jdtls client found")
vim.notify("No jdtls client found for bufnr=" .. bufnr, vim.log.levels.INFO)
return
end

Expand Down

0 comments on commit 922dca1

Please sign in to comment.