Skip to content

Commit b8f0d68

Browse files
committed
fix: pass config to jdtls init instead of global import
1 parent 03d0d03 commit b8f0d68

5 files changed

Lines changed: 47 additions & 49 deletions

File tree

lua/java-core/ls/servers/jdtls/cmd.lua

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local List = require('java-core.utils.list')
22
local path = require('java-core.utils.path')
33
local Manager = require('pkgm.manager')
4-
local conf = require('java.config')
54
local system = require('java-core.utils.system')
65
local log = require('java-core.utils.log2')
76
local err = require('java-core.utils.errors')
@@ -10,50 +9,50 @@ local lsp_utils = require('java-core.utils.lsp')
109

1110
local M = {}
1211

13-
local jdtls_root = Manager:get_install_dir('jdtls', conf.jdtls.version)
14-
1512
--- Returns a function that returns the command to start jdtls
16-
---@param opts { use_lombok: boolean }
17-
function M.get_cmd(opts)
13+
---@param config java.Config
14+
function M.get_cmd(config)
1815
---@param dispatchers? vim.lsp.rpc.Dispatchers
19-
---@param config vim.lsp.ClientConfig
20-
return function(dispatchers, config)
21-
local cmd = M.get_jvm_args(opts):concat(M.get_jar_args())
16+
---@param lsp_config vim.lsp.ClientConfig
17+
return function(dispatchers, lsp_config)
18+
local cmd = M.get_jvm_args(config):concat(M.get_jar_args(config))
2219

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

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

3431
local result = vim.lsp.rpc.start(cmd, dispatchers, {
35-
cwd = config.cmd_cwd,
36-
env = config.cmd_env,
37-
detached = config.detached,
32+
cwd = lsp_config.cmd_cwd,
33+
env = lsp_config.cmd_env,
34+
detached = lsp_config.detached,
3835
})
3936

4037
return result
4138
end
4239
end
4340

4441
---@private
45-
---@param opts { use_lombok: boolean }
42+
---@param config java.Config
4643
---@return java-core.List
47-
function M.get_jvm_args(opts)
44+
function M.get_jvm_args(config)
45+
local use_lombok = config.lombok.enable
46+
local jdtls_root = Manager:get_install_dir('jdtls', config.jdtls.version)
4847
local jdtls_config = path.join(jdtls_root, system.get_config_suffix())
4948

5049
local java_exe = 'java'
5150

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

8685
-- Adding lombok
87-
if opts.use_lombok then
88-
local lombok_root = Manager:get_install_dir('lombok', conf.lombok.version)
86+
if use_lombok then
87+
local lombok_root = Manager:get_install_dir('lombok', config.lombok.version)
8988
local lombok_path = vim.fn.glob(path.join(lombok_root, 'lombok*.jar'))
9089
jvm_args:push('-javaagent:' .. lombok_path)
9190
end
@@ -94,9 +93,11 @@ function M.get_jvm_args(opts)
9493
end
9594

9695
---@private
96+
---@param config java.Config
9797
---@param cwd? string
9898
---@return java-core.List
99-
function M.get_jar_args(cwd)
99+
function M.get_jar_args(config, cwd)
100+
local jdtls_root = Manager:get_install_dir('jdtls', config.jdtls.version)
100101
cwd = cwd or vim.fn.getcwd()
101102

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

123124
---@private
125+
---@param config java.Config
124126
---@param env table
125-
function M.validate_java_version(env)
127+
function M.validate_java_version(config, env)
126128
local curr_ver = M.get_java_major_version(env)
127-
local exp_ver = java_version_map[conf.jdtls.version]
129+
local exp_ver = java_version_map[config.jdtls.version]
128130

129131
if not (curr_ver >= exp_ver.to and curr_ver <= exp_ver.from) then
130132
local msg = string.format(
131133
'Java version mismatch: JDTLS %s requires Java %d <= java >= %d, but found Java %d',
132-
conf.jdtls.version,
134+
config.jdtls.version,
133135
exp_ver.from,
134136
exp_ver.to,
135137
curr_ver

lua/java-core/ls/servers/jdtls/env.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ local Manager = require('pkgm.manager')
33
local log = require('java-core.utils.log2')
44
local system = require('java-core.utils.system')
55

6-
--- @TODO: importing stuff from java main package feels wrong.
7-
--- We should fix this in the future
8-
local config = require('java.config')
9-
106
local M = {}
117

12-
--- @param opts { use_jdk: boolean }
13-
function M.get_env(opts)
14-
if not opts.use_jdk then
15-
log.debug('use_jdk disabled, returning empty env')
8+
--- @param config java.Config
9+
function M.get_env(config)
10+
if not config.jdk.auto_install then
11+
log.debug('config.jdk.auto_install disabled, returning empty env')
1612
return {}
1713
end
1814

lua/java-core/ls/servers/jdtls/init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local M = {}
22

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

1616
local base_conf = vim.deepcopy(conf, true)
1717

18-
base_conf.cmd = cmd.get_cmd(opts)
19-
base_conf.cmd_env = env.get_env(opts)
20-
base_conf.init_options.bundles = plugins.get_plugins(opts)
18+
base_conf.cmd = cmd.get_cmd(opts.config)
19+
base_conf.cmd_env = env.get_env(opts.config)
20+
base_conf.init_options.bundles = plugins.get_plugins(opts.config, opts.plugins)
2121
base_conf.root_markers = root.get_root_markers()
2222
base_conf.filetypes = filetype.get_filetypes()
2323

lua/java-core/ls/servers/jdtls/plugins.lua

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ local List = require('java-core.utils.list')
33
local Manager = require('pkgm.manager')
44
local log = require('java-core.utils.log2')
55

6-
--- @TODO: importing stuff from java main package feels wrong.
7-
--- We should fix this in the future
8-
local config = require('java.config')
9-
106
local M = {}
117

128
local plug_jar_map = {
@@ -35,17 +31,22 @@ local plug_jar_map = {
3531
['spring-boot-tools'] = { 'extension/jars/*.jar' },
3632
}
3733

38-
local plugin_version_map = {
39-
['java-test'] = config.java_test.version,
40-
['java-debug'] = config.java_debug_adapter.version,
41-
['spring-boot-tools'] = config.spring_boot_tools.version,
42-
}
34+
function M.get_plugin_version_map(config)
35+
return {
36+
['java-test'] = config.java_test.version,
37+
['java-debug'] = config.java_debug_adapter.version,
38+
['spring-boot-tools'] = config.spring_boot_tools.version,
39+
}
40+
end
4341

4442
---Returns a list of .jar file paths for given list of jdtls plugins
45-
---@param opts { plugins: string[] }
43+
---@param config java.Config
44+
---@param plugins string[]
4645
---@return string[] # list of .jar file paths
47-
function M.get_plugins(opts)
48-
return List:new(opts.plugins)
46+
function M.get_plugins(config, plugins)
47+
local plugin_version_map = M.get_plugin_version_map(config)
48+
49+
return List:new(plugins)
4950
:map(function(plugin_name)
5051
local version = plugin_version_map[plugin_name]
5152
local root = Manager:get_install_dir(plugin_name, version)

lua/java/startup/lsp_setup.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ function M.setup(config)
3232
end
3333

3434
local default_config = server.get_config({
35+
config = config,
3536
plugins = jdtls_plugins,
36-
use_jdk = config.jdk.auto_install,
37-
use_lombok = config.lombok.enable,
3837
})
3938

4039
vim.lsp.config('jdtls', default_config)

0 commit comments

Comments
 (0)