fix(java): only use mason-registry if mason installed (#4991)

## Description
The current config breaks when mason is not installed (e.g. for Nix
users). This PR makes the mason-related functionality (which, as of now,
consists only of finding `lombok`) dependent on mason being installed.
As noted in the commit, this does make it impossible to use lombok
without mason, but I'm unaware of any other method of finding it (which
is why mason-registry was used in the first place, I assume)

Please note that I'm not very experienced with Lua and the code might be
not the most idiomatic one.

## Related Issue(s)

<!--
  If this PR fixes any issues, please link to the issue here.
  - Fixes #<issue_number>
-->

## Screenshots

<!-- Add screenshots of the changes if applicable. -->

## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
Ughur Alakbarov 2024-12-12 18:15:53 +01:00 committed by GitHub
parent 8b8e322b68
commit 21b02f056d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -85,8 +85,12 @@ return {
dependencies = { "folke/which-key.nvim" },
ft = java_filetypes,
opts = function()
local cmd = { vim.fn.exepath("jdtls") }
if LazyVim.has("mason.nvim") then
local mason_registry = require("mason-registry")
local lombok_jar = mason_registry.get_package("jdtls"):get_install_path() .. "/lombok.jar"
table.insert(cmd, string.format("--jvm-arg=-javaagent:%s", lombok_jar))
end
return {
-- How to find the root dir for a given filename. The default comes from
-- lspconfig which provides a function specifically for java projects.
@ -107,10 +111,7 @@ return {
-- How to run jdtls. This can be overridden to a full java command-line
-- if the Python wrapper script doesn't suffice.
cmd = {
vim.fn.exepath("jdtls"),
string.format("--jvm-arg=-javaagent:%s", lombok_jar),
},
cmd = cmd,
full_cmd = function(opts)
local fname = vim.api.nvim_buf_get_name(0)
local root_dir = opts.root_dir(fname)