From 21b02f056d924d0a3cb3513b163e3dbdfa9c15ec Mon Sep 17 00:00:00 2001 From: Ughur Alakbarov <58857108+ugur-a@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:15:53 +0100 Subject: [PATCH] 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) ## Screenshots ## Checklist - [x] I've read the [CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md) guidelines. Co-authored-by: Folke Lemaitre --- lua/lazyvim/plugins/extras/lang/java.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lua/lazyvim/plugins/extras/lang/java.lua b/lua/lazyvim/plugins/extras/lang/java.lua index f4c07320..25b99f97 100644 --- a/lua/lazyvim/plugins/extras/lang/java.lua +++ b/lua/lazyvim/plugins/extras/lang/java.lua @@ -85,8 +85,12 @@ return { dependencies = { "folke/which-key.nvim" }, ft = java_filetypes, opts = function() - local mason_registry = require("mason-registry") - local lombok_jar = mason_registry.get_package("jdtls"):get_install_path() .. "/lombok.jar" + 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)