Skip to content

Commit 29e6318

Browse files
committed
feat: add config option to install jdk17 via mason
* now the jdk-17 will be auto installed unless configured otherwise
1 parent cff4448 commit 29e6318

File tree

7 files changed

+108
-47
lines changed

7 files changed

+108
-47
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Just install and start writing `public static void main(String[] args)`.
2323

2424
- Uses [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) to setup `jdtls`
2525
- Realtime server settings updates is possible using [neoconf](https://github.com/folke/neoconf.nvim)
26-
- Everything necessary will be installed automatically (except JDKs)
26+
- Everything necessary will be installed automatically
2727
- Uses `jdtls` and auto loads `jdtls` plugins from [mason.nvim](https://github.com/williamboman/mason.nvim)
2828
- Supported plugins are,
2929
- `lombok`
@@ -38,9 +38,8 @@ Just install and start writing `public static void main(String[] args)`.
3838
<summary>:pushpin: details</summary>
3939

4040
- Pre-requisites
41-
- [JDK 17](https://jdk.java.net/archive/) - jdtls is running on JDK 17
41+
4242
- [Python 3.9](https://www.python.org/downloads/) - for running `jdtls` wrapper launch script
43-
- [Node 16](https://nodejs.org/dist/v16.9.1/) - for building `vscode-java-test`
4443

4544
- Install the plugin
4645

lua/java.lua

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
local deps = require('java.utils.dependencies')
2-
local mason = require('java.utils.mason')
3-
local lspconfig = require('java.utils.lspconfig')
1+
local decomple_watch = require('java.startup.decompile-watcher')
2+
local mason_dep = require('java.startup.mason-dep')
3+
local nvim_dep = require('java.startup.nvim-dep')
4+
local setup_wrap = require('java.startup.lspconfig-setup-wrap')
45

56
local test = require('java.api.test')
67
local dap = require('java.api.dap')
7-
-- local ts = require('java.treesitter')
8+
9+
local global_config = require('java.config')
810

911
local M = {}
1012

11-
function M.setup()
12-
deps.check()
13-
mason.install_dependencies()
14-
lspconfig.wrap_lspconfig_setup()
15-
lspconfig.register_class_file_decomplier()
13+
function M.setup(custom_config)
14+
local config = vim.tbl_deep_extend('force', global_config, custom_config)
15+
16+
nvim_dep.check()
17+
mason_dep.install(config)
18+
19+
setup_wrap.setup(config)
20+
decomple_watch.setup()
21+
1622
dap.setup_dap_on_lsp_attach()
1723
end
1824

lua/java/config.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---@class java.Config
2+
---@field root_markers string[] list of file that exists in root of the project
3+
---@field jdtls_plugins string[] what plugins to load
4+
---@field java_test { enable: boolean }
5+
---@field java_debug_adapter { enable: boolean }
6+
---@field jdk { auto_install: boolean }
7+
8+
local config = {
9+
root_markers = {
10+
'settings.gradle',
11+
'settings.gradle.kts',
12+
'pom.xml',
13+
'build.gradle',
14+
'mvnw',
15+
'gradlew',
16+
'build.gradle',
17+
'build.gradle.kts',
18+
'.git',
19+
},
20+
java_test = {
21+
enable = true,
22+
},
23+
java_debug_adapter = {
24+
enable = true,
25+
},
26+
jdk = {
27+
auto_install = false,
28+
},
29+
}
30+
31+
return config
Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,20 @@
1-
local lspconfig = require('lspconfig')
2-
3-
local log = require('java.utils.log')
41
local jdtls = require('java.utils.jdtls')
52
local get_error_handler = require('java.handlers.error')
63

7-
local server = require('java-core.ls.servers.jdtls')
84
local async = require('java-core.utils.async').sync
95

106
local JavaCoreJdtlsClient = require('java-core.ls.clients.jdtls-client')
117

128
local M = {}
139

14-
function M.wrap_lspconfig_setup()
15-
log.info('wrap lspconfig.java.setup function to inject a custom java config')
16-
---@type fun(config: LspSetupConfig)
17-
local org_setup = lspconfig.jdtls.setup
18-
19-
lspconfig.jdtls.setup = function(user_config)
20-
local config = server.get_config({
21-
root_markers = {
22-
'settings.gradle',
23-
'settings.gradle.kts',
24-
'pom.xml',
25-
'build.gradle',
26-
'mvnw',
27-
'gradlew',
28-
'build.gradle',
29-
'build.gradle.kts',
30-
'.git',
31-
},
32-
jdtls_plugins = { 'java-test', 'java-debug-adapter' },
33-
})
34-
35-
config = vim.tbl_deep_extend('force', user_config, config)
36-
37-
org_setup(config)
38-
end
39-
end
40-
4110
---@class BufReadCmdCallbackArgs
4211
---@field buf integer buffer number
4312
---@field event string name of the event
4413
---@field file string name of the file
4514
---@field id integer event id?
4615
---@field match string matched pattern in autocmd match
4716

48-
function M.register_class_file_decomplier()
17+
function M.setup()
4918
vim.api.nvim_create_autocmd('BufReadCmd', {
5019
pattern = 'jdt://*',
5120
---@param opts BufReadCmdCallbackArgs
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local lspconfig = require('lspconfig')
2+
local log = require('java.utils.log')
3+
4+
local server = require('java-core.ls.servers.jdtls')
5+
6+
local M = {}
7+
8+
---comment
9+
---@param config java.Config
10+
function M.setup(config)
11+
log.info('wrap lspconfig.java.setup function to inject a custom java config')
12+
---@type fun(config: LspSetupConfig)
13+
local org_setup = lspconfig.jdtls.setup
14+
15+
lspconfig.jdtls.setup = function(user_config)
16+
local jdtls_plugins = {}
17+
18+
if config.java_test.enable then
19+
table.insert(jdtls_plugins, 'java-test')
20+
end
21+
22+
if config.java_debug_adapter.enable then
23+
table.insert(jdtls_plugins, 'java-debug-adapter')
24+
end
25+
26+
local default_config = server.get_config({
27+
root_markers = config.root_markers,
28+
jdtls_plugins = jdtls_plugins,
29+
use_mason_jdk = config.jdk.auto_install,
30+
})
31+
32+
org_setup(vim.tbl_extend('force', user_config, default_config))
33+
end
34+
end
35+
36+
return M
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
local log = require('java.utils.log')
22
local mason_reg = require('mason-registry')
3+
local mason_ui = require('mason.ui')
4+
local notify = require('java-core.utils.notify')
35

46
local M = {}
57

68
local dependecies = {
79
{ name = 'jdtls', version = 'v1.29.0' },
8-
{ name = 'java-test', version = '0.40.2' },
10+
{ name = 'java-test', version = '0.40.1' },
911
{ name = 'java-debug-adapter', version = '0.52.0' },
1012
}
1113

1214
---Install mason package dependencies for nvim-java
13-
function M.install_dependencies()
15+
---@param config java.Config
16+
function M.install(config)
1417
log.info('check mason dependecies')
1518

19+
local is_installing = false
20+
21+
if config.jdk.auto_install then
22+
table.insert(dependecies, { name = 'openjdk-17', version = '17.0.2' })
23+
end
24+
1625
for _, dep in ipairs(dependecies) do
1726
if not M.is_installed(dep.name, dep.version) then
18-
log.fmt_info('installing mason pkg: %s', dep.name)
27+
log.info('installing mason pkg: ' .. tostring(dep.name))
28+
29+
is_installing = true
1930

2031
local pkg = mason_reg.get_package(dep.name)
2132

@@ -25,9 +36,17 @@ function M.install_dependencies()
2536
})
2637
end
2738
end
39+
40+
if is_installing then
41+
mason_ui.open()
42+
notify.warn(
43+
'Please restart the editor after dependency installation is done'
44+
)
45+
end
2846
end
2947

3048
---Returns true if the package and its expected version is already installed
49+
---@private
3150
---@param pkg_name string name of the package
3251
---@param expc_version string expected version of the package
3352
---@return boolean true if the package and its version is already installed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function M.check()
2525
M.neovim_plugin_check()
2626
end
2727

28+
---@private
2829
function M.neovim_plugin_check()
2930
for _, pkg in ipairs(pkgs) do
3031
local ok, _ = pcall(require, pkg.name)

0 commit comments

Comments
 (0)