diff --git a/bin/java-lsp b/bin/java-lsp index 5bd33ed..5b54ee0 100755 --- a/bin/java-lsp +++ b/bin/java-lsp @@ -2,22 +2,64 @@ set -eE -if [ $# -ne 3 ]; then - echo "Usage: $0 {ROOT_PATH} {CFG_PATH} {WORKSPACE_PATH}" - exit 1 -fi +#=== FUNCTION ================================================================ +# NAME: usage +# DESCRIPTION: Display usage information. +#=============================================================================== +function usage () +{ + echo "Usage : $0 [options] [--] + + Options: + -j path to JAVA_HOME + -r ROOT DIRECTORY + -c JDTLS config path + -d JDTLS workspace path + -h|help Display this message" + +} # ---------- end of function usage ---------- + +#----------------------------------------------------------------------- +# Handle command line arguments +#----------------------------------------------------------------------- + +JAVA_EXE=java +JAVA_HOME= +_ROOT_DIR= +_CFG_PATH= +_WORKSPACE_DIR= +while getopts ":hj:r:c:d:" opt +do + case $opt in + + h) usage; exit 0 ;; + + j) JAVA_HOME=$OPTARG ;; -# assume _ROOT_DIR, _CFG_PATH, _WORKSPACE_DIR all absolute path -_ROOT_DIR=$1 -_CFG_PATH=$2 -_WORKSPACE_DIR=$3 + r) _ROOT_DIR=$OPTARG ;; + + c) _CFG_PATH=$OPTARG ;; + + d) _WORKSPACE_DIR=$OPTARG ;; + + * ) echo -e "\n Option does not exist : $OPTARG\n" + usage; exit 1 ;; + + esac # --- end of case --- +done +shift $((OPTIND-1)) pushd "$_ROOT_DIR" JAR="$_ROOT_DIR/plugins/org.eclipse.equinox.launcher_*.jar" +if [ ! -d "$JAVA_HOME" ] ; then + echo "JAVA HOME $JAVA_HOME not exist" + exit 1 +fi +JAVA_EXE="$JAVA_HOME/bin/java" + _DEBUG_OPTS= -JAVA_PRG=$(command -v java || echo java) if [[ -n "$DEBUG" ]]; then # note jdtls will fail if other precess runs on port 1044 _DEBUG_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044 @@ -35,7 +77,7 @@ fi # 1. https://github.com/mfussenegger/nvim-jdtls#configuration # 2. https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line # shellcheck disable=SC2086 -exec $JAVA_PRG $_DEBUG_OPTS $_LOMBOK_OPT \ +exec $JAVA_EXE $_DEBUG_OPTS $_LOMBOK_OPT \ -Declipse.application=org.eclipse.jdt.ls.core.id1 \ -Dosgi.bundles.defaultStartLevel=4 \ -Declipse.product=org.eclipse.jdt.ls.core.product \ diff --git a/lua/aceforeverd/lsp/jdtls.lua b/lua/aceforeverd/lsp/jdtls.lua index 1ef97fd..aeb4ac0 100644 --- a/lua/aceforeverd/lsp/jdtls.lua +++ b/lua/aceforeverd/lsp/jdtls.lua @@ -15,6 +15,26 @@ local M = {} + +local function search_jdk_runtimes() + local runtimes = {} + local sdkman_java_candidates = '~/.sdkman/candidates/java/' + local jdk_versions = { '8', '11', '17', '21', '22', '18' } + for _, version in ipairs(jdk_versions) do + local path = vim.fn.glob(sdkman_java_candidates .. version .. '.*', true, true) + if #path > 0 then + table.insert(runtimes, { name = 'JDK' .. version, path = path[1] }) + end + end + + local system_default = vim.fn.system({ 'java-config', '-O' }) + if vim.v.shell_error == 0 then + table.insert(runtimes, { name = 'System Default', path = string.gsub(system_default, "%s+$", '') }) + end + + return runtimes +end + function M.jdtls() local mason_registery = require('mason-registry') local server = mason_registery.get_package('jdtls') @@ -34,7 +54,16 @@ function M.jdtls() local extendedClientCapabilities = jdtls.extendedClientCapabilities extendedClientCapabilities.resolveAdditionalTextEditsSupport = true - -- TODO: check java version, jdtls requires JAVA >= 17 + -- Finding supported Java in sdkman + -- or u can search in more locations + local javas = vim.fn.glob('~/.sdkman/candidates/java/{21,20,17,22,18,19}*', true, true) + local java_home + if #javas > 0 then + java_home = javas[1] + else + java_home = os.getenv('JAVA_HOME') + end + -- TODO: build a JDK table dynamically to 'settings.java.configuration' ? local cfg_file if vim.fn.has('mac') == 1 then @@ -54,7 +83,13 @@ function M.jdtls() local workspace_dir = data_path .. '/jdtls-ws/' .. prj_name local config = vim.tbl_deep_extend('force', { - cmd = { config_path .. '/bin/java-lsp', dir, dir .. '/' .. cfg_file, workspace_dir }, + cmd = { + config_path .. '/bin/java-lsp', + '-r', dir, + '-c', dir .. '/' .. cfg_file, + '-d', workspace_dir, + '-j', java_home, + }, settings = { -- https://github.com/mfussenegger/dotfiles/blob/master/vim/.config/nvim/ftplugin/java.lua java = { @@ -103,8 +138,9 @@ function M.jdtls() }, useBlocks = true, }, - -- runtimes = { - -- }, + configuration = { + runtimes = search_jdk_runtimes(), + } }, }, flags = {