Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions lua/java-core/ls/servers/jdtls/cmd.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local List = require('java-core.utils.list')
local path = require('java-core.utils.path')
local Manager = require('pkgm.manager')
local conf = require('java.config')
local system = require('java-core.utils.system')
local log = require('java-core.utils.log2')
local err = require('java-core.utils.errors')
Expand All @@ -10,50 +9,50 @@ local lsp_utils = require('java-core.utils.lsp')

local M = {}

local jdtls_root = Manager:get_install_dir('jdtls', conf.jdtls.version)

--- Returns a function that returns the command to start jdtls
---@param opts { use_lombok: boolean }
function M.get_cmd(opts)
---@param config java.Config
function M.get_cmd(config)
---@param dispatchers? vim.lsp.rpc.Dispatchers
---@param config vim.lsp.ClientConfig
return function(dispatchers, config)
local cmd = M.get_jvm_args(opts):concat(M.get_jar_args())
---@param lsp_config vim.lsp.ClientConfig
return function(dispatchers, lsp_config)
local cmd = M.get_jvm_args(config):concat(M.get_jar_args(config))

-- NOTE: eventhough we are setting the PATH env var, due to a bug, it's not
-- working on Windows. So just lanching 'java' will result in executing the
-- system java. So as a workaround, we use the absolute path to java instead
-- So following check is not needed when we have auto_install set to true
-- @see https://github.com/neovim/neovim/issues/36818
if not conf.jdk.auto_install then
M.validate_java_version(config.cmd_env)
if not config.jdk.auto_install then
M.validate_java_version(config, lsp_config.cmd_env)
end

log.debug('Starting jdtls with cmd', cmd)

local result = vim.lsp.rpc.start(cmd, dispatchers, {
cwd = config.cmd_cwd,
env = config.cmd_env,
detached = config.detached,
cwd = lsp_config.cmd_cwd,
env = lsp_config.cmd_env,
detached = lsp_config.detached,
})

return result
end
end

---@private
---@param opts { use_lombok: boolean }
---@param config java.Config
---@return java-core.List
function M.get_jvm_args(opts)
function M.get_jvm_args(config)
local use_lombok = config.lombok.enable
local jdtls_root = Manager:get_install_dir('jdtls', config.jdtls.version)
local jdtls_config = path.join(jdtls_root, system.get_config_suffix())

local java_exe = 'java'

-- NOTE: eventhough we are setting the PATH env var, due to a bug, it's not
-- working on Windows. So we are using the absolute path to java instead
-- @see https://github.com/neovim/neovim/issues/36818
if conf.jdk.auto_install then
local jdk_root = Manager:get_install_dir('openjdk', conf.jdk.version)
if config.jdk.auto_install then
local jdk_root = Manager:get_install_dir('openjdk', config.jdk.version)
local java_home
if system.get_os() == 'mac' then
java_home = vim.fn.glob(path.join(jdk_root, 'jdk-*', 'Contents', 'Home'))
Expand Down Expand Up @@ -84,8 +83,8 @@ function M.get_jvm_args(opts)
})

-- Adding lombok
if opts.use_lombok then
local lombok_root = Manager:get_install_dir('lombok', conf.lombok.version)
if use_lombok then
local lombok_root = Manager:get_install_dir('lombok', config.lombok.version)
local lombok_path = vim.fn.glob(path.join(lombok_root, 'lombok*.jar'))
jvm_args:push('-javaagent:' .. lombok_path)
end
Expand All @@ -94,9 +93,11 @@ function M.get_jvm_args(opts)
end

---@private
---@param config java.Config
---@param cwd? string
---@return java-core.List
function M.get_jar_args(cwd)
function M.get_jar_args(config, cwd)
local jdtls_root = Manager:get_install_dir('jdtls', config.jdtls.version)
cwd = cwd or vim.fn.getcwd()

local launcher_reg = path.join(jdtls_root, 'plugins', 'org.eclipse.equinox.launcher_*.jar')
Expand All @@ -121,15 +122,16 @@ function M.get_jar_args(cwd)
end

---@private
---@param config java.Config
---@param env table
function M.validate_java_version(env)
function M.validate_java_version(config, env)
local curr_ver = M.get_java_major_version(env)
local exp_ver = java_version_map[conf.jdtls.version]
local exp_ver = java_version_map[config.jdtls.version]

if not (curr_ver >= exp_ver.to and curr_ver <= exp_ver.from) then
local msg = string.format(
'Java version mismatch: JDTLS %s requires Java %d <= java >= %d, but found Java %d',
conf.jdtls.version,
config.jdtls.version,
exp_ver.from,
exp_ver.to,
curr_ver
Expand Down
12 changes: 4 additions & 8 deletions lua/java-core/ls/servers/jdtls/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ local Manager = require('pkgm.manager')
local log = require('java-core.utils.log2')
local system = require('java-core.utils.system')

--- @TODO: importing stuff from java main package feels wrong.
--- We should fix this in the future
local config = require('java.config')

local M = {}

--- @param opts { use_jdk: boolean }
function M.get_env(opts)
if not opts.use_jdk then
log.debug('use_jdk disabled, returning empty env')
--- @param config java.Config
function M.get_env(config)
if not config.jdk.auto_install then
log.debug('config.jdk.auto_install disabled, returning empty env')
return {}
end

Expand Down
8 changes: 4 additions & 4 deletions lua/java-core/ls/servers/jdtls/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

--- Returns jdtls config
---@param opts { use_jdk: boolean, use_lombok: boolean, plugins: string[] }
---@param opts { plugins: string[], config: java.Config }
function M.get_config(opts)
local conf = require('java-core.ls.servers.jdtls.conf')
local plugins = require('java-core.ls.servers.jdtls.plugins')
Expand All @@ -15,9 +15,9 @@ function M.get_config(opts)

local base_conf = vim.deepcopy(conf, true)

base_conf.cmd = cmd.get_cmd(opts)
base_conf.cmd_env = env.get_env(opts)
base_conf.init_options.bundles = plugins.get_plugins(opts)
base_conf.cmd = cmd.get_cmd(opts.config)
base_conf.cmd_env = env.get_env(opts.config)
base_conf.init_options.bundles = plugins.get_plugins(opts.config, opts.plugins)
base_conf.root_markers = root.get_root_markers()
base_conf.filetypes = filetype.get_filetypes()

Expand Down
25 changes: 13 additions & 12 deletions lua/java-core/ls/servers/jdtls/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ local List = require('java-core.utils.list')
local Manager = require('pkgm.manager')
local log = require('java-core.utils.log2')

--- @TODO: importing stuff from java main package feels wrong.
--- We should fix this in the future
local config = require('java.config')

local M = {}

local plug_jar_map = {
Expand Down Expand Up @@ -35,17 +31,22 @@ local plug_jar_map = {
['spring-boot-tools'] = { 'extension/jars/*.jar' },
}

local plugin_version_map = {
['java-test'] = config.java_test.version,
['java-debug'] = config.java_debug_adapter.version,
['spring-boot-tools'] = config.spring_boot_tools.version,
}
function M.get_plugin_version_map(config)
return {
['java-test'] = config.java_test.version,
['java-debug'] = config.java_debug_adapter.version,
['spring-boot-tools'] = config.spring_boot_tools.version,
}
end

---Returns a list of .jar file paths for given list of jdtls plugins
---@param opts { plugins: string[] }
---@param config java.Config
---@param plugins string[]
---@return string[] # list of .jar file paths
function M.get_plugins(opts)
return List:new(opts.plugins)
function M.get_plugins(config, plugins)
local plugin_version_map = M.get_plugin_version_map(config)

return List:new(plugins)
:map(function(plugin_name)
local version = plugin_version_map[plugin_name]
local root = Manager:get_install_dir(plugin_name, version)
Expand Down
3 changes: 1 addition & 2 deletions lua/java/startup/lsp_setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ function M.setup(config)
end

local default_config = server.get_config({
config = config,
plugins = jdtls_plugins,
use_jdk = config.jdk.auto_install,
use_lombok = config.lombok.enable,
})

vim.lsp.config('jdtls', default_config)
Expand Down
2 changes: 0 additions & 2 deletions lua/java/ui/profile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local notify = require('java-core.utils.notify')
local profile_config = require('java.api.profile_config')
local class = require('java-core.utils.class')
local dap_api = require('java-dap')
local log = require('java-core.utils.log2')
local lsp_utils = require('java-core.utils.lsp')
local ui = require('java.ui.utils')

Expand Down Expand Up @@ -184,7 +183,6 @@ function ProfileUI:get_and_fill_popup(title, key, target_profile, enter, keymaps
win_options = self.win_options,
})

log.error(vim.inspect(popup.border))
-- fill the popup with the config value
-- if target_profile is nil, it's a new profile
if target_profile then
Expand Down