fix(java): avoid using mason if not present

This commit is contained in:
Vinicius Deolindo 2024-12-12 12:12:12 -03:00
parent a9a273d041
commit ac1d7bc106

View file

@ -85,8 +85,12 @@ return {
dependencies = { "folke/which-key.nvim" }, dependencies = { "folke/which-key.nvim" },
ft = java_filetypes, ft = java_filetypes,
opts = function() opts = function()
local java_agent = ""
if LazyVim.has("mason.nvim") then
local mason_registry = require("mason-registry") local mason_registry = require("mason-registry")
local lombok_jar = mason_registry.get_package("jdtls"):get_install_path() .. "/lombok.jar" local lombok_jar = mason_registry.get_package("jdtls"):get_install_path() .. "/lombok.jar"
java_agent = string.format("--jvm-arg=-javaagent:%s", lombok_jar)
end
return { return {
-- How to find the root dir for a given filename. The default comes from -- How to find the root dir for a given filename. The default comes from
-- lspconfig which provides a function specifically for java projects. -- lspconfig which provides a function specifically for java projects.
@ -109,7 +113,7 @@ return {
-- if the Python wrapper script doesn't suffice. -- if the Python wrapper script doesn't suffice.
cmd = { cmd = {
vim.fn.exepath("jdtls"), vim.fn.exepath("jdtls"),
string.format("--jvm-arg=-javaagent:%s", lombok_jar), java_agent,
}, },
full_cmd = function(opts) full_cmd = function(opts)
local fname = vim.api.nvim_buf_get_name(0) local fname = vim.api.nvim_buf_get_name(0)
@ -145,8 +149,9 @@ return {
config = function(_, opts) config = function(_, opts)
-- Find the extra bundles that should be passed on the jdtls command-line -- Find the extra bundles that should be passed on the jdtls command-line
-- if nvim-dap is enabled with java debug/test. -- if nvim-dap is enabled with java debug/test.
local mason_registry = require("mason-registry")
local bundles = {} ---@type string[] local bundles = {} ---@type string[]
if LazyVim.has("mason.nvim") then
local mason_registry = require("mason-registry")
if opts.dap and LazyVim.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then if opts.dap and LazyVim.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then
local java_dbg_pkg = mason_registry.get_package("java-debug-adapter") local java_dbg_pkg = mason_registry.get_package("java-debug-adapter")
local java_dbg_path = java_dbg_pkg:get_install_path() local java_dbg_path = java_dbg_pkg:get_install_path()
@ -167,6 +172,7 @@ return {
end end
end end
end end
end
local function attach_jdtls() local function attach_jdtls()
local fname = vim.api.nvim_buf_get_name(0) local fname = vim.api.nvim_buf_get_name(0)
@ -239,6 +245,8 @@ return {
}, },
}) })
if LazyVim.has("mason.nvim") then
local mason_registry = require("mason-registry")
if opts.dap and LazyVim.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then if opts.dap and LazyVim.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then
-- custom init for Java debugger -- custom init for Java debugger
require("jdtls").setup_dap(opts.dap) require("jdtls").setup_dap(opts.dap)
@ -275,6 +283,7 @@ return {
}) })
end end
end end
end
-- User can set additional keymaps in opts.on_attach -- User can set additional keymaps in opts.on_attach
if opts.on_attach then if opts.on_attach then