diff --git a/vim/ftplugin/java.lua b/vim/ftplugin/java.lua index 050f241..53a93bd 100644 --- a/vim/ftplugin/java.lua +++ b/vim/ftplugin/java.lua @@ -1,6 +1,11 @@ local home = os.getenv('HOME') local nvim_dir = home .. "/.local/share/nvim/" local jdtls_dir = nvim_dir .. "mason/packages/jdtls/" +local jdtls_plugins_dir = jdtls_dir .. "plugins/" +local work = home .. "/.work.lua" +if vim.fn.filereadable(work) then + package.path = package.path .. work +end local function trim(s) return s:gsub("^%s*(.-)%s*$", "%1") @@ -33,18 +38,24 @@ local function get_equinox_version(plugins_dir) return trim(equinox_version) end -local plugins_dir = jdtls_dir .. "plugins/" -local jar = plugins_dir .. get_equinox_version(plugins_dir) +local function get_jdtls_jar_location() + return jdtls_plugins_dir .. get_equinox_version(jdtls_plugins_dir) +end local on_attach = function(client, bufnr) + if vim.fn.filereadable(work) then + require("work").add_ws_folders() + end MapLspCommands(client, bufnr) require('jdtls').setup_dap({ hotcodereplace = 'auto' }) end -local bundles = { - vim.fn.glob(nvim_dir .. "mason/packages/java-debug-adapter/extension/server/com.microsoft.java.debug.plugin-*.jar", 1) -} -bundles = vim.list_extend(bundles, vim.split(vim.fn.glob(nvim_dir .. "mason/packages/java-test/extension/server/*.jar", 1), "\n")) +local function get_bundles() + local bundles = { + vim.fn.glob(nvim_dir .. "mason/packages/java-debug-adapter/extension/server/com.microsoft.java.debug.plugin-*.jar", 1) + } + return vim.list_extend(bundles, vim.split(vim.fn.glob(nvim_dir .. "mason/packages/java-test/extension/server/*.jar", 1), "\n")) +end local function setJavaKeyMaps() local java_maps = { @@ -66,18 +77,26 @@ local function setJavaKeyMaps() vim.api.nvim_set_keymap(keymap[1], keymap[2], command, { noremap = true }) end end -setJavaKeyMaps() --- local root_dir = require("jdtls.setup").find_root({"packageInfo"}, "Config") -package.path = package.path .. ";" .. home .. "/.work.lua" -local root_dir = require("work").find_root_work() -local ws_folders_jdtls = {} -if root_dir then - ws_folders_jdtls = require("work").add_ws_root_folders(root_dir) -else - root_dir = require('jdtls.setup').find_root({'.git', 'mvnw', 'gradlew'}) +local function get_root_dir() + if vim.fn.filereadable(work) then + return require("work").get_root_dir() + end + return vim.fs.root(0, {".git", "mvnw", "gradlew"}) +end + +local function get_ws_folders() + if vim.fn.filereadable(work) then + return require("work").add_ws_folders() + end + return {} +end + +local root_dir = get_root_dir() + +local function get_eclipse_workspace(root_dir) + return home .. "/.local/share/eclipse/" .. vim.fn.fnamemodify(root_dir, ":p:h:t") end -local eclipse_workspace = home .. "/.local/share/eclipse/" .. vim.fn.fnamemodify(root_dir, ":p:h:t") local config = { on_attach = on_attach, @@ -92,18 +111,20 @@ local config = { '--add-modules=ALL-SYSTEM', '--add-opens', 'java.base/java.util=ALL-UNNAMED', '--add-opens', 'java.base/java.lang=ALL-UNNAMED', - '-jar', jar, + '-jar', get_jdtls_jar_location(), '-configuration', jdtls_dir .. "/config_" .. get_os(), '--jvm-arg=-javaagent:' .. home .. "/Developer/lombok.jar", - '-data', eclipse_workspace + '-data', get_eclipse_workspace(root_dir) }, root_dir = root_dir, settings = { java = {} }, init_options = { - bundles = bundles, - workspace_folders = ws_folders_jdtls + bundles = get_bundles(), + workspace_folders = get_ws_folders() }, } + +setJavaKeyMaps() require('jdtls').start_or_attach(config)