mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-24 17:58:51 +02:00
feat(java): add test and debug support for Java extension (#1192)
This commit is contained in:
parent
07f183516f
commit
f6d646a325
1 changed files with 55 additions and 4 deletions
|
@ -9,6 +9,21 @@ return {
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
-- Ensure java debugger and test packages are installed
|
||||||
|
{
|
||||||
|
"mfussenegger/nvim-dap",
|
||||||
|
optional = true,
|
||||||
|
dependencies = {
|
||||||
|
{
|
||||||
|
"williamboman/mason.nvim",
|
||||||
|
opts = function(_, opts)
|
||||||
|
opts.ensure_installed = opts.ensure_installed or {}
|
||||||
|
vim.list_extend(opts.ensure_installed, { "java-test", "java-debug-adapter" })
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
-- Set up lsp with mfussenegger/nvim-jdtls instead of nvim-lspconfig.
|
-- Set up lsp with mfussenegger/nvim-jdtls instead of nvim-lspconfig.
|
||||||
{
|
{
|
||||||
"neovim/nvim-lspconfig",
|
"neovim/nvim-lspconfig",
|
||||||
|
@ -38,7 +53,25 @@ return {
|
||||||
local lsp_config = require("lspconfig.server_configurations.jdtls").default_config
|
local lsp_config = require("lspconfig.server_configurations.jdtls").default_config
|
||||||
local find_java_project_root = lsp_config.root_dir
|
local find_java_project_root = lsp_config.root_dir
|
||||||
local filetypes = lsp_config.filetypes
|
local filetypes = lsp_config.filetypes
|
||||||
|
-- lookup paths for java test and debugger package
|
||||||
|
local mason_registry = require("mason-registry")
|
||||||
|
local bundles = {}
|
||||||
|
if mason_registry.has_package("java-test") and mason_registry.has_package("java-debug-adapter") then
|
||||||
|
-- jdtls tools configuration for debugging support
|
||||||
|
local java_test_pkg = mason_registry.get_package("java-test")
|
||||||
|
local java_test_path = java_test_pkg:get_install_path()
|
||||||
|
local java_dbg_pkg = mason_registry.get_package("java-debug-adapter")
|
||||||
|
local java_dbg_path = java_dbg_pkg:get_install_path()
|
||||||
|
local jar_patterns = {
|
||||||
|
java_dbg_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar",
|
||||||
|
java_test_path .. "/extension/server/*.jar"
|
||||||
|
}
|
||||||
|
for _, jar_pattern in ipairs(jar_patterns) do
|
||||||
|
for _, bundle in ipairs(vim.split(vim.fn.glob(jar_pattern), '\n')) do
|
||||||
|
table.insert(bundles, bundle)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
-- Attach jdtls for the proper filetypes (i.e. java).
|
-- Attach jdtls for the proper filetypes (i.e. java).
|
||||||
-- Existing server will be reused if the root_dir matches.
|
-- Existing server will be reused if the root_dir matches.
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
|
@ -58,14 +91,32 @@ return {
|
||||||
vim.fs.joinpath(jdtls_cache_dir, "workspace"),
|
vim.fs.joinpath(jdtls_cache_dir, "workspace"),
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
require("jdtls").start_or_attach({
|
local jdtls_base_config = {
|
||||||
|
on_attach = require("lazyvim.util").on_attach(function(client, buffer)
|
||||||
|
if mason_registry.has_package("java-test") then
|
||||||
|
-- custom keymaps for Java test runner (not yet compatible with neotest)
|
||||||
|
vim.keymap.set("n", "<leader>tT", function() require("jdtls").pick_test({ bufnr = buffer }) end, { buffer = buffer, desc = "Run specific Test" })
|
||||||
|
vim.keymap.set("n", "<leader>tt", function() require("jdtls").test_class({ bufnr = buffer }) end, { buffer = buffer, desc = "Run File" })
|
||||||
|
vim.keymap.set("n", "<leader>tr", function() require("jdtls").test_nearest_method({ bufnr = buffer }) end, { buffer = buffer, desc = "Run nearest" })
|
||||||
|
end
|
||||||
|
if mason_registry.has_package("java-debug-adapter") then
|
||||||
|
-- custom init for Java debugger
|
||||||
|
require("jdtls").setup_dap({ hotcodereplace = "auto" })
|
||||||
|
require("jdtls.dap").setup_dap_main_class_configs()
|
||||||
|
end
|
||||||
|
require("jdtls.setup").add_commands()
|
||||||
|
end),
|
||||||
cmd = cmd,
|
cmd = cmd,
|
||||||
root_dir = root_dir,
|
root_dir = root_dir,
|
||||||
})
|
init_options = {
|
||||||
|
bundles = bundles,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local jdtls_opts = require("lazyvim.util").opts("nvim-jdtls")
|
||||||
|
require("jdtls").start_or_attach(vim.tbl_deep_extend("force", jdtls_opts or {}, jdtls_base_config))
|
||||||
require("which-key").register({ c = { x = { name = "Extract" } } }, { prefix = "<leader>" })
|
require("which-key").register({ c = { x = { name = "Extract" } } }, { prefix = "<leader>" })
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
return true -- avoid duplicate servers
|
return true -- avoid duplicate servers
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue