Skip to content

Commit

Permalink
better jdtls handle
Browse files Browse the repository at this point in the history
  • Loading branch information
aceforeverd committed Nov 23, 2023
1 parent 7cc6806 commit fe450df
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
62 changes: 52 additions & 10 deletions bin/java-lsp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand Down
44 changes: 40 additions & 4 deletions lua/aceforeverd/lsp/jdtls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -103,8 +138,9 @@ function M.jdtls()
},
useBlocks = true,
},
-- runtimes = {
-- },
configuration = {
runtimes = search_jdk_runtimes(),
}
},
},
flags = {
Expand Down

0 comments on commit fe450df

Please sign in to comment.