11local config = require (' java-core.ls.servers.jdtls.config' )
22local log = require (' java-core.utils.log' )
33local mason = require (' java-core.utils.mason' )
4+ local mason_reg = require (' mason-registry' )
45local path = require (' java-core.utils.path' )
56local plugins = require (' java-core.ls.servers.jdtls.plugins' )
67local util = require (' lspconfig.util' )
@@ -13,6 +14,7 @@ local M = {}
1314--- Ex:- { 'pom.xml', 'build.gradle', '.git' }
1415--- @field jdtls_plugins string[] list of jdtls plugins to load on start up
1516--- Ex:- { 'java-test', 'java-debug-adapter' }
17+ --- @field use_mason_jdk boolean whether to use mason jdk to load jdtls or not
1618
1719--- Returns a configuration for jdtls that you can pass into the setup of nvim-lspconfig
1820--- @param opts JavaCoreGetConfigOptions
@@ -36,6 +38,21 @@ function M.get_config(opts)
3638 ' -javaagent:' .. lombok_path ,
3739 }
3840
41+ if opts .use_mason_jdk then
42+ local jdk = mason_reg .get_package (' openjdk-17' )
43+
44+ if jdk :is_installed () then
45+ local java_home =
46+ vim .fn .glob (path .join (jdk :get_install_path (), ' /jdk-17*' ))
47+ local java_bin = path .join (java_home , ' /bin' )
48+
49+ base_config .cmd_env = {
50+ [' PATH' ] = vim .fn .getenv (' PATH' ) .. ' :' .. java_bin ,
51+ [' JAVA_HOME' ] = java_home ,
52+ }
53+ end
54+ end
55+
3956 base_config .root_dir = M .get_root_finder (opts .root_markers )
4057 base_config .init_options .bundles = plugin_paths
4158 base_config .init_options .workspace = workspace .get_default_workspace ()
0 commit comments