Skip to content

Commit fe450df

Browse files
committed
better jdtls handle
1 parent 7cc6806 commit fe450df

File tree

2 files changed

+92
-14
lines changed

2 files changed

+92
-14
lines changed

bin/java-lsp

+52-10
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,64 @@
22

33
set -eE
44

5-
if [ $# -ne 3 ]; then
6-
echo "Usage: $0 {ROOT_PATH} {CFG_PATH} {WORKSPACE_PATH}"
7-
exit 1
8-
fi
5+
#=== FUNCTION ================================================================
6+
# NAME: usage
7+
# DESCRIPTION: Display usage information.
8+
#===============================================================================
9+
function usage ()
10+
{
11+
echo "Usage : $0 [options] [--]
12+
13+
Options:
14+
-j path to JAVA_HOME
15+
-r ROOT DIRECTORY
16+
-c JDTLS config path
17+
-d JDTLS workspace path
18+
-h|help Display this message"
19+
20+
} # ---------- end of function usage ----------
21+
22+
#-----------------------------------------------------------------------
23+
# Handle command line arguments
24+
#-----------------------------------------------------------------------
25+
26+
JAVA_EXE=java
27+
JAVA_HOME=
28+
_ROOT_DIR=
29+
_CFG_PATH=
30+
_WORKSPACE_DIR=
31+
while getopts ":hj:r:c:d:" opt
32+
do
33+
case $opt in
34+
35+
h) usage; exit 0 ;;
36+
37+
j) JAVA_HOME=$OPTARG ;;
938

10-
# assume _ROOT_DIR, _CFG_PATH, _WORKSPACE_DIR all absolute path
11-
_ROOT_DIR=$1
12-
_CFG_PATH=$2
13-
_WORKSPACE_DIR=$3
39+
r) _ROOT_DIR=$OPTARG ;;
40+
41+
c) _CFG_PATH=$OPTARG ;;
42+
43+
d) _WORKSPACE_DIR=$OPTARG ;;
44+
45+
* ) echo -e "\n Option does not exist : $OPTARG\n"
46+
usage; exit 1 ;;
47+
48+
esac # --- end of case ---
49+
done
50+
shift $((OPTIND-1))
1451

1552
pushd "$_ROOT_DIR"
1653

1754
JAR="$_ROOT_DIR/plugins/org.eclipse.equinox.launcher_*.jar"
1855

56+
if [ ! -d "$JAVA_HOME" ] ; then
57+
echo "JAVA HOME $JAVA_HOME not exist"
58+
exit 1
59+
fi
60+
JAVA_EXE="$JAVA_HOME/bin/java"
61+
1962
_DEBUG_OPTS=
20-
JAVA_PRG=$(command -v java || echo java)
2163
if [[ -n "$DEBUG" ]]; then
2264
# note jdtls will fail if other precess runs on port 1044
2365
_DEBUG_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044
@@ -35,7 +77,7 @@ fi
3577
# 1. https://github.com/mfussenegger/nvim-jdtls#configuration
3678
# 2. https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line
3779
# shellcheck disable=SC2086
38-
exec $JAVA_PRG $_DEBUG_OPTS $_LOMBOK_OPT \
80+
exec $JAVA_EXE $_DEBUG_OPTS $_LOMBOK_OPT \
3981
-Declipse.application=org.eclipse.jdt.ls.core.id1 \
4082
-Dosgi.bundles.defaultStartLevel=4 \
4183
-Declipse.product=org.eclipse.jdt.ls.core.product \

lua/aceforeverd/lsp/jdtls.lua

+40-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@
1515

1616
local M = {}
1717

18+
19+
local function search_jdk_runtimes()
20+
local runtimes = {}
21+
local sdkman_java_candidates = '~/.sdkman/candidates/java/'
22+
local jdk_versions = { '8', '11', '17', '21', '22', '18' }
23+
for _, version in ipairs(jdk_versions) do
24+
local path = vim.fn.glob(sdkman_java_candidates .. version .. '.*', true, true)
25+
if #path > 0 then
26+
table.insert(runtimes, { name = 'JDK' .. version, path = path[1] })
27+
end
28+
end
29+
30+
local system_default = vim.fn.system({ 'java-config', '-O' })
31+
if vim.v.shell_error == 0 then
32+
table.insert(runtimes, { name = 'System Default', path = string.gsub(system_default, "%s+$", '') })
33+
end
34+
35+
return runtimes
36+
end
37+
1838
function M.jdtls()
1939
local mason_registery = require('mason-registry')
2040
local server = mason_registery.get_package('jdtls')
@@ -34,7 +54,16 @@ function M.jdtls()
3454
local extendedClientCapabilities = jdtls.extendedClientCapabilities
3555
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true
3656

37-
-- TODO: check java version, jdtls requires JAVA >= 17
57+
-- Finding supported Java in sdkman
58+
-- or u can search in more locations
59+
local javas = vim.fn.glob('~/.sdkman/candidates/java/{21,20,17,22,18,19}*', true, true)
60+
local java_home
61+
if #javas > 0 then
62+
java_home = javas[1]
63+
else
64+
java_home = os.getenv('JAVA_HOME')
65+
end
66+
-- TODO: build a JDK table dynamically to 'settings.java.configuration' ?
3867

3968
local cfg_file
4069
if vim.fn.has('mac') == 1 then
@@ -54,7 +83,13 @@ function M.jdtls()
5483
local workspace_dir = data_path .. '/jdtls-ws/' .. prj_name
5584

5685
local config = vim.tbl_deep_extend('force', {
57-
cmd = { config_path .. '/bin/java-lsp', dir, dir .. '/' .. cfg_file, workspace_dir },
86+
cmd = {
87+
config_path .. '/bin/java-lsp',
88+
'-r', dir,
89+
'-c', dir .. '/' .. cfg_file,
90+
'-d', workspace_dir,
91+
'-j', java_home,
92+
},
5893
settings = {
5994
-- https://github.com/mfussenegger/dotfiles/blob/master/vim/.config/nvim/ftplugin/java.lua
6095
java = {
@@ -103,8 +138,9 @@ function M.jdtls()
103138
},
104139
useBlocks = true,
105140
},
106-
-- runtimes = {
107-
-- },
141+
configuration = {
142+
runtimes = search_jdk_runtimes(),
143+
}
108144
},
109145
},
110146
flags = {

0 commit comments

Comments
 (0)