local puk = require 'std.puk' local project = require 'std.project' local auto = require 'std.auto' local function parse_toolchains( toolchains_path ) local runtimes = {} local file = io.open( toolchains_path, 'r' ) if not file then print( 'Could not open toolchains.xml at ' .. toolchains_path ) return runtimes end local content = file:read '*all' file:close() for toolchain in content:gmatch '(.-)' do local type = toolchain:match '(.-)' if type == 'jdk' then local version = toolchain:match '(.-)' local jdk_path = toolchain:match '(.-)' if version and jdk_path then table.insert( runtimes, { name = 'JavaSE-' .. version, path = jdk_path, } ) end end end return runtimes end local data_path = vim.fn.stdpath 'data' if type( data_path ) == 'table' then data_path = data_path[1] end local home = vim.fs.abspath '~' local jdtls_install_path = vim.fs.joinpath( home, 'Tools', 'jdt-language-server-1.47.0-202504100338' ) local toolchain_path = vim.fs.joinpath( home, '.m2', 'toolchains.xml' ) local jdtls_path = vim.fs.joinpath( jdtls_install_path, 'plugins', 'org.eclipse.equinox.launcher_1.7.0.v20250404-1055.jar' ) local create_config = function ( syntax_server_mode, path ) local root_dir = project.find_java_project_root( path ) local jdlts_config_dir if syntax_server_mode then jdlts_config_dir = vim.fn.has 'win32' and 'config_ss_win' or 'config_ss_linux' else jdlts_config_dir = vim.fn.has 'win32' and 'config_win' or 'config_linux' end local config_dir = vim.fs.joinpath( jdtls_install_path, jdlts_config_dir ) local workspace_dir = vim.fs.joinpath( data_path, 'jdtls-workspace', vim.fs.basename( root_dir ) ) local cmd = { 'java', '-Declipse.application=org.eclipse.jdt.ls.core.id1', '-Dosgi.bundles.defaultStartLevel=4', '-Declipse.product=org.eclipse.jdt.ls.core.product', '-Dlog.level=ERROR', '-Xmx8G', '-XX:+UseG1GC', '--add-modules=ALL-SYSTEM', '--add-opens', 'java.base/java.util=ALL-UNNAMED', '--add-opens', 'java.base/java.lang=ALL-UNNAMED', '-jar', jdtls_path, '-configuration', config_dir, '-data', workspace_dir, } if syntax_server_mode then table.insert( cmd, 2, '-Dsyntaxserver=true' ) end local runtimes = parse_toolchains( toolchain_path ) local config = { name = syntax_server_mode and 'jdtlsss' or 'jdtls', cmd = cmd, root_dir = root_dir, settings = { java = { signatureHelp = { enabled = true }, sources = { autobuild = { enabled = true }, signatureHelp = { enabled = true }, saveActions = { origanizeImports = true }, organizeImports = { starThreshold = 9999, staticStarThreshold = 9999, }, codeGeneration = { toString = { template = '${object.className}{${member.name()}=${member.value()}, ${otherMembers}}', }, hashCodeEquals = { useJava7Objects = true }, useBlocks = true, }, eclipse = { downloadSources = true }, }, configuration = { updateBuildConfiguration = 'interactive', runtimes = runtimes, }, maven = { downloadSources = true }, implementationsCodeLens = { enabled = true }, referencesCodeLens = { enabled = true }, references = { includeDecompiledSources = true }, inlayHints = { parameterNames = { enabled = 'all' } }, format = { enabled = true }, }, }, handlers = { ['language/status'] = function ( _, status ) if status.message == 'ServiceReady' and not syntax_server_mode then -- TODO stop syntax server end end, }, } return config end auto 'start_jdtls' { pattern = 'java', events = 'FileType', description = 'Starts jdlts (with syntax mode initially)', command = function () local path = vim.api.nvim_buf_get_name( 0 ) puk 'jdtls'.start_or_attach( create_config( true, path ) ) puk 'jdtls'.start_or_attach( create_config( false, path ) ) end, }