Skip to content

Commit 83e25bb

Browse files
authored
feat: auto configure jdtls and dap at start up (nvim-java#2)
1 parent a202fa3 commit 83e25bb

File tree

23 files changed

+612
-170
lines changed

23 files changed

+612
-170
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
vendor/plenary.nvim
2+
.test_plugins

.vscode/tasks.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"tasks": [
3+
{
4+
"label": "Run Tests",
5+
"type": "shell",
6+
"command": "make test"
7+
}
8+
]
9+
}

README.md

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,62 @@
1-
# A Neovim Plugin Template
2-
3-
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/ellisonleao/nvim-plugin-template/lint-test.yml?branch=main&style=for-the-badge)
4-
![Lua](https://img.shields.io/badge/Made%20with%20Lua-blueviolet.svg?style=for-the-badge&logo=lua)
5-
6-
A template repository for Neovim plugins.
7-
8-
## Using it
9-
10-
Via `gh`:
11-
12-
```
13-
$ gh repo create my-plugin -p ellisonleao/nvim-plugin-template
1+
# nvim-java
2+
3+
![Neovim](https://img.shields.io/badge/NeoVim-%2357A143.svg?&style=for-the-badge&logo=neovim&logoColor=white)
4+
![Lua](https://img.shields.io/badge/lua-%232C2D72.svg?style=for-the-badge&logo=lua&logoColor=white)
5+
![Java](https://img.shields.io/badge/java-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white)
6+
![Gradle](https://img.shields.io/badge/Gradle-02303A.svg?style=for-the-badge&logo=Gradle&logoColor=white)
7+
![Apache Maven](https://img.shields.io/badge/Apache%20Maven-C71A36?style=for-the-badge&logo=Apache%20Maven&logoColor=white)
8+
9+
No need to put up with [jdtls](https://github.com/eclipse-jdtls/eclipse.jdt.ls) nonsense anymore.
10+
Just install and start writing `public static void main(String[] args)`.
11+
12+
## Features
13+
14+
- :white_check_mark: Diagnostics & Auto Completion
15+
- :white_check_mark: Automatic [DAP](https://github.com/mfussenegger/nvim-dap) debug configuration
16+
- :x: Running tests
17+
18+
## Why
19+
20+
- Uses [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) to setup `jdtls`
21+
- Realtime server settings updates is possible using [neoconf](https://github.com/folke/neoconf.nvim)
22+
- Everything necessary will be installed automatically (except JDKs)
23+
- Uses `jdtls` and auto loads `jdtls` plugins from [mason.nvim](https://github.com/williamboman/mason.nvim)
24+
- Supported plugins are,
25+
- `lombok`
26+
- `java-test`
27+
- `java-debug-adapter`
28+
- Typed & documented APIs
29+
- No callback hells I [promise](https://github.com/pyericz/promise-lua)
30+
31+
## How to Use
32+
33+
### Install the plugin
34+
35+
Using [lazy.nvim](https://github.com/folke/lazy.nvim)
36+
37+
```lua
38+
return {
39+
'nvim-java/nvim-java',
40+
dependencies = {
41+
'nvim-java/nvim-java-core',
42+
'neovim/nvim-lspconfig',
43+
'williamboman/mason.nvim',
44+
'mfussenegger/nvim-dap',
45+
},
46+
event = 'VeryLazy',
47+
opts = {},
48+
}
1449
```
1550

16-
Via github web page:
51+
### Setup JDTLS like you would usually do
1752

18-
Click on `Use this template`
19-
20-
![](https://docs.github.com/assets/cb-36544/images/help/repository/use-this-template-button.png)
21-
22-
## Features and structure
53+
```lua
54+
require('lspconfig').jdtls.setup({})
55+
```
2356

24-
- 100% Lua
25-
- Github actions for:
26-
- running tests using [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) and [busted](https://olivinelabs.com/busted/)
27-
- check for formatting errors (Stylua)
28-
- vimdocs autogeneration from README.md file
29-
- luarocks release (LUAROCKS_API_KEY secret configuration required)
57+
Yep! That's all :)
3058

31-
### Plugin structure
59+
## Projects Acknowledgement
3260

33-
```
34-
.
35-
├── lua
36-
│   ├── plugin_name
37-
│   │   └── module.lua
38-
│   └── plugin_name.lua
39-
├── Makefile
40-
├── plugin
41-
│   └── plugin_name.lua
42-
├── README.md
43-
├── tests
44-
│   ├── minimal_init.lua
45-
│   └── plugin_name
46-
│   └── plugin_name_spec.lua
47-
```
61+
[nvim-jdtls](https://github.com/mfussenegger/nvim-jdtls) is a plugin that follows "Keep it simple, stupid!" approach.
62+
If you love customizing things by yourself, then give nvim-jdtls a try.

dev/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ vim.opt.runtimepath:prepend(cwd)
44
--[[
55
-- plugin name will be used to reload the loaded modules
66
--]]
7-
local package_name = 'plugin_name'
7+
local package_name = 'java'
88

99
-- add the escape character to special characters
1010
local escape_pattern = function(text)
@@ -26,7 +26,7 @@ end
2626
local run_action = function()
2727
vim.cmd.messages('clear')
2828

29-
require(package_name).hello()
29+
require(package_name).__run()
3030
end
3131

3232
-- unload and run the function from the package

doc/my-template-docs.txt

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

lua/java.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local deps = require('java.dependencies')
2+
local java_mason = require('java.mason')
3+
local java_dap = require('java.dap')
4+
local java_lspconfig = require('java.lspconfig')
5+
local ts = require('java.treesitter')
6+
7+
local M = {}
8+
9+
function M.setup()
10+
deps.check()
11+
java_lspconfig.wrap_lspconfig_setup()
12+
java_mason.install_dependencies()
13+
java_dap.setup_dap()
14+
end
15+
16+
----------------------------------------------------------------------
17+
-- DAP APIs --
18+
----------------------------------------------------------------------
19+
M.dap = {}
20+
M.dap.config_dap = java_dap.config_dap
21+
22+
----------------------------------------------------------------------
23+
-- Test APIs --
24+
----------------------------------------------------------------------
25+
M.test = {}
26+
M.test.run_current_test_class = java_dap.run_current_test_class
27+
28+
----------------------------------------------------------------------
29+
-- Manipulate --
30+
----------------------------------------------------------------------
31+
M.manipulate = {}
32+
-- M.manipulate.organize_imports = {}
33+
34+
function M.__run()
35+
ts.find_main_method()
36+
end
37+
38+
return M

lua/java/dap/dapp.lua

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
local log = require('java-core.utils.log')
2+
3+
local Promise = require('java-core.utils.promise')
4+
local JavaCoreDap = require('java-core.dap')
5+
local JavaCoreTestHelper = require('java-core.ls.helpers.test-helper')
6+
7+
---@class JavaDap
8+
---@field client LSPClient
9+
---@field test_helper JavaCoreTestHelper
10+
---@field dap_helper JavaCoreDap
11+
local M = {}
12+
13+
---@param args { client: LSPClient }
14+
---@return JavaDap
15+
function M:new(args)
16+
local o = {
17+
client = args.client,
18+
}
19+
20+
o.test_helper = JavaCoreTestHelper:new({
21+
client = args.client,
22+
})
23+
24+
o.dap_helper = JavaCoreDap:new({
25+
client = args.client,
26+
})
27+
28+
setmetatable(o, self)
29+
self.__index = self
30+
return o
31+
end
32+
33+
function M:run_current_test_class()
34+
log.info('running the current class')
35+
36+
local buffer = vim.api.nvim_get_current_buf()
37+
38+
self.test_helper
39+
:get_test_class_by_buffer(buffer)
40+
:thenCall(function(classes)
41+
self.test_helper:run_test(classes)
42+
end)
43+
:catch(function(err)
44+
local msg = 'failed to run the current class'
45+
46+
log.error(msg, err)
47+
error(msg .. err)
48+
end)
49+
end
50+
51+
function M:config_dap()
52+
return Promise.resolve()
53+
:thenCall(function()
54+
log.debug('set dap adapter callback function')
55+
56+
-- setting java adapter
57+
require('dap').adapters.java = function(callback)
58+
self.dap_helper:get_dap_adapter():thenCall(callback):catch(function(err)
59+
local msg = 'faild to set DAP adapter'
60+
61+
error(msg, err)
62+
log.error(msg, err)
63+
end)
64+
end
65+
66+
-- setting java config
67+
return self.dap_helper:get_dap_config()
68+
end)
69+
:thenCall(function(dap_config)
70+
log.debug('set dap config: ', dap_config)
71+
72+
require('dap').configurations.java = dap_config
73+
end)
74+
:catch(function(err)
75+
local msg = 'faild to set DAP configuration'
76+
77+
log.error(msg, err)
78+
error(msg .. err)
79+
end)
80+
end
81+
82+
return M

lua/java/dap/init.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local JavaDap = require('java.dap.dapp')
2+
3+
local log = require('java-core.utils.log')
4+
local state = require('java.state')
5+
local notify = require('java-core.utils.notify')
6+
7+
local M = {}
8+
9+
---Setup dap config & adapter on jdtls attach event
10+
function M.setup_dap()
11+
log.info('add LspAttach event handlers to setup dap adapter & config')
12+
13+
vim.api.nvim_create_autocmd('LspAttach', {
14+
pattern = '*',
15+
callback = M.on_jdtls_attach,
16+
once = true,
17+
group = vim.api.nvim_create_augroup('nvim-java-dap-config', {}),
18+
})
19+
end
20+
21+
---Runs the current test class
22+
function M.run_current_test_class()
23+
state.java_dap:run_current_test_class()
24+
end
25+
26+
---Configures the dap
27+
function M.config_dap()
28+
state.java_dap:config_dap():thenCall(function()
29+
notify.info('DAP configured')
30+
end)
31+
end
32+
33+
---@private
34+
---@param ev any
35+
function M.on_jdtls_attach(ev)
36+
local client = vim.lsp.get_client_by_id(ev.data.client_id)
37+
38+
if client.name == 'jdtls' then
39+
state.java_dap = JavaDap:new({
40+
client = client,
41+
})
42+
43+
log.info('setup java dap config & adapter')
44+
45+
state.java_dap:config_dap()
46+
end
47+
end
48+
49+
return M

0 commit comments

Comments
 (0)