Skip to content

Commit f7c80ca

Browse files
authored
refactor!: let jdtls wrapper script handle launching the server (#20)
1 parent 10fcd5a commit f7c80ca

File tree

9 files changed

+146
-233
lines changed

9 files changed

+146
-233
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local M = {}
2+
3+
function M.get_config()
4+
return {
5+
init_options = {
6+
extendedClientCapabilities = {
7+
classFileContentsSupport = true,
8+
generateToStringPromptSupport = true,
9+
hashCodeEqualsPromptSupport = true,
10+
advancedExtractRefactoringSupport = true,
11+
advancedOrganizeImportsSupport = true,
12+
generateConstructorsPromptSupport = true,
13+
generateDelegateMethodsPromptSupport = true,
14+
moveRefactoringSupport = true,
15+
overrideMethodsPromptSupport = true,
16+
executeClientCommandSupport = true,
17+
inferSelectionSupport = {
18+
'extractMethod',
19+
'extractVariable',
20+
'extractConstant',
21+
'extractVariableAllOccurrence',
22+
},
23+
},
24+
},
25+
26+
handlers = {
27+
--@TODO
28+
--overriding '$/progress' is necessary because by default it's using the
29+
--lspconfig progress handler which prints the wrong value in the latest
30+
--jdtls version (tested on 1.29.0).
31+
--https://github.com/neovim/nvim-lspconfig/issues/2897
32+
['$/progress'] = vim.lsp.handlers['$/progress'],
33+
},
34+
}
35+
end
36+
37+
return M
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
local util = require('lspconfig.util')
2+
local path = require('java-core.utils.path')
3+
local mason = require('java-core.utils.mason')
4+
local plugins = require('java-core.ls.servers.jdtls.plugins')
5+
local log = require('java-core.utils.log')
6+
local workspace = require('java-core.ls.servers.jdtls.workspace')
7+
local config = require('java-core.ls.servers.jdtls.config')
8+
9+
local M = {}
10+
11+
---@class JavaCoreGetConfigOptions
12+
---@field root_markers string[] list of files to find the root dir of a project
13+
---Ex:- { 'pom.xml', 'build.gradle', '.git' }
14+
---@field jdtls_plugins string[] list of jdtls plugins to load on start up
15+
---Ex:- { 'java-test', 'java-debug-adapter' }
16+
17+
---Returns a configuration for jdtls that you can pass into the setup of nvim-lspconfig
18+
---@param opts JavaCoreGetConfigOptions
19+
---@return LSPSetupConfig # jdtls setup configuration
20+
function M.get_config(opts)
21+
log.debug('generating jdtls config')
22+
23+
local jdtls_path = mason.get_pkg_path('jdtls')
24+
local lombok_path = path.join(jdtls_path, 'lombok.jar')
25+
local jdtls_cache_path = path.join(vim.fn.stdpath('cache'), 'jdtls')
26+
local plugin_paths =
27+
plugins.get_plugin_paths({ 'java-test', 'java-debug-adapter' })
28+
29+
local base_config = config.get_config()
30+
31+
base_config.cmd = {
32+
'jdtls',
33+
'-configuration',
34+
jdtls_cache_path,
35+
'-data',
36+
workspace.get_default_workspace(),
37+
'-javaagent:' .. lombok_path,
38+
}
39+
40+
base_config.root_dir = M.get_root_finder(opts.root_markers)
41+
base_config.init_options.bundles = plugin_paths
42+
base_config.init_options.workspace = workspace.get_default_workspace()
43+
44+
log.debug('generated jdtls setup config: ', base_config)
45+
46+
return base_config
47+
end
48+
49+
---Returns a function that finds the java project root
50+
---@private
51+
---@param root_markers string[] list of files to find the root dir of a project
52+
---@return function
53+
function M.get_root_finder(root_markers)
54+
return function(file_name)
55+
log.info('finding the root_dir')
56+
log.debug('root_markers: ', root_markers)
57+
58+
local root = util.root_pattern(unpack(root_markers))(file_name)
59+
60+
if root then
61+
log.fmt_debug('root of: %s is: %s', file_name, root)
62+
return root
63+
end
64+
end
65+
end
66+
67+
return M
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local mason = require('java-core.utils.mason')
2+
local path = require('java-core.utils.path')
3+
local file = require('java-core.utils.file')
4+
5+
local List = require('java-core.utils.list')
6+
7+
local M = {}
8+
9+
local plugin_to_jar_path_map = {
10+
['java-test'] = '*.jar',
11+
['java-debug-adapter'] = '*.jar',
12+
}
13+
14+
---Returns a list of .jar file paths for given list of jdtls plugins
15+
---@param plugins string[]
16+
---@return string[] # list of .jar file paths
17+
function M.get_plugin_paths(plugins)
18+
local plugin_paths = List:new()
19+
20+
for _, plugin in ipairs(plugins) do
21+
local relative_path = plugin_to_jar_path_map[plugin]
22+
local plugin_shared_path = mason.get_shared_path(plugin)
23+
local full_path = path.join(plugin_shared_path, relative_path)
24+
local resolved_paths = file.get_file_list(full_path)
25+
26+
plugin_paths:push(resolved_paths)
27+
end
28+
29+
return plugin_paths:flatten()
30+
end
31+
32+
return M
File renamed without changes.

lua/java-core/server.lua

Lines changed: 0 additions & 123 deletions
This file was deleted.

lua/java-core/settings.lua

Lines changed: 0 additions & 27 deletions
This file was deleted.

lua/java-core/utils/path.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local M = {}
2+
3+
---Join a given list of paths to one path
4+
---@param ... string paths to join
5+
---@return string # joined path
6+
function M.join(...)
7+
return table.concat({ ... }, '/')
8+
end
9+
10+
return M

lua/java-core/utils/plugin.lua

Lines changed: 0 additions & 39 deletions
This file was deleted.

lua/java-core/utils/table.lua

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)