Skip to content

Commit a5e5adb

Browse files
authored
feat: add test current method API (nvim-java#31)
1 parent 737792d commit a5e5adb

File tree

7 files changed

+159
-42
lines changed

7 files changed

+159
-42
lines changed

README.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# nvim-java
1+
# :coffee: nvim-java
22

33
![Neovim](https://img.shields.io/badge/NeoVim-%2357A143.svg?&style=for-the-badge&logo=neovim&logoColor=white)
44
![Lua](https://img.shields.io/badge/lua-%232C2D72.svg?style=for-the-badge&logo=lua&logoColor=white)
@@ -9,13 +9,13 @@
99
No need to put up with [jdtls](https://github.com/eclipse-jdtls/eclipse.jdt.ls) nonsense anymore.
1010
Just install and start writing `public static void main(String[] args)`.
1111

12-
## Features
12+
## :dizzy: Features
1313

1414
- :white_check_mark: Diagnostics & Auto Completion
1515
- :white_check_mark: Automatic [DAP](https://github.com/mfussenegger/nvim-dap) debug configuration
16-
- :x: Running tests
16+
- :white_check_mark: Running tests
1717

18-
## Why
18+
## :bulb: Why
1919

2020
- Uses [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) to setup `jdtls`
2121
- Realtime server settings updates is possible using [neoconf](https://github.com/folke/neoconf.nvim)
@@ -26,15 +26,18 @@ Just install and start writing `public static void main(String[] args)`.
2626
- `java-test`
2727
- `java-debug-adapter`
2828
- Typed & documented APIs
29-
- No callback hells I [promise](https://github.com/pyericz/promise-lua)
3029

31-
## How to Use
30+
## :hammer: How to Install
3231

33-
## Pre-requisites
32+
<details>
33+
34+
<summary>:pushpin: details</summary>
35+
36+
**Pre-requisites**
3437

3538
- [Python 3.9](https://www.python.org/downloads/) - for running `jdtls` wrapper launch script
3639

37-
### Install the plugin
40+
**Install the plugin**
3841

3942
Using [lazy.nvim](https://github.com/folke/lazy.nvim)
4043

@@ -52,25 +55,31 @@ return {
5255
}
5356
```
5457

55-
### Setup JDTLS like you would usually do
58+
**Setup jdtls like you would usually do**
5659

5760
```lua
5861
require('lspconfig').jdtls.setup({})
5962
```
6063

6164
Yep! That's all :)
6265

63-
## APIs
66+
</details>
67+
68+
## :computer: APIs
6469

65-
### DAP
70+
<details>
71+
72+
<summary>:pushpin: details</summary>
73+
74+
**DAP**
6675

6776
- `config_dap` - DAP is autoconfigured on start up, but in case you want to force configure it again, you can use this API
6877

6978
```lua
7079
require('java').dap.config_dap()
7180
```
7281

73-
### Test
82+
**Test**
7483

7584
- `run_current_test_class` - Run the test class in the active buffer
7685

@@ -84,8 +93,10 @@ require('java').test.run_current_test_class()
8493
require('java').test.debug_current_test_class()
8594
```
8695

87-
## Projects Acknowledgement
96+
</details>
97+
98+
## :bookmark_tabs: Projects Acknowledgement
8899

89100
[nvim-jdtls](https://github.com/mfussenegger/nvim-jdtls) is a plugin that follows "Keep it simple, stupid!" approach.
90-
If you love customizing things by yourself, then give nvim-jdtls a try. I may or may not have copied some code ;-)
91-
Open source is beautiful!
101+
If you love customizing things by yourself, then give nvim-jdtls a try. I may or may not have copied some code :wink:
102+
Beauty of Open source!

lua/java.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ local deps = require('java.utils.dependencies')
22
local mason = require('java.utils.mason')
33
local lspconfig = require('java.utils.lspconfig')
44

5-
local dap = require('java.dap.api')
6-
local ts = require('java.treesitter')
5+
local test = require('java.api.test')
6+
local dap = require('java.api.dap')
7+
-- local ts = require('java.treesitter')
78

89
local M = {}
910

@@ -25,8 +26,8 @@ M.dap.config_dap = dap.config_dap
2526
-- Test APIs --
2627
----------------------------------------------------------------------
2728
M.test = {}
28-
M.test.run_current_test_class = dap.run_current_test_class
29-
M.test.debug_current_test_class = dap.debug_current_test_class
29+
M.test.run_current_test_class = test.run_current_test_class
30+
M.test.debug_current_test_class = test.debug_current_test_class
3031

3132
----------------------------------------------------------------------
3233
-- Manipulate --
@@ -35,7 +36,7 @@ M.manipulate = {}
3536
-- M.manipulate.organize_imports = {}
3637

3738
function M.__run()
38-
ts.find_main_method()
39+
test.debug_current_method()
3940
end
4041

4142
return M
Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,6 @@ function M.setup_dap_on_lsp_attach()
1919
})
2020
end
2121

22-
function M.run_current_test_class()
23-
log.info('run current test class')
24-
25-
return async(function()
26-
return JavaDap:new(jdtls()):execute_current_test_class({ noDebug = true })
27-
end)
28-
.catch(get_error_handler('failed to run the current test class'))
29-
.run()
30-
end
31-
32-
function M.debug_current_test_class()
33-
log.info('debug current test class')
34-
35-
return async(function()
36-
return JavaDap:new(jdtls()):execute_current_test_class({})
37-
end)
38-
.catch(get_error_handler('failed to debug the current test class'))
39-
.run()
40-
end
41-
4222
function M.config_dap()
4323
log.info('configuring dap')
4424

lua/java/api/test.lua

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
local JavaDap = require('java.dap')
2+
3+
local log = require('java.utils.log')
4+
local get_error_handler = require('java.handlers.error')
5+
local jdtls = require('java.utils.jdtls')
6+
local async = require('java-core.utils.async').sync
7+
8+
local M = {}
9+
10+
---Setup dap config & adapter on jdtls attach event
11+
12+
function M.run_current_test_class()
13+
log.info('run current test class')
14+
15+
return async(function()
16+
return JavaDap:new(jdtls()):execute_current_test_class({ noDebug = true })
17+
end)
18+
.catch(get_error_handler('failed to run the current test class'))
19+
.run()
20+
end
21+
22+
function M.debug_current_test_class()
23+
log.info('debug current test class')
24+
25+
return async(function()
26+
JavaDap:new(jdtls()):execute_current_test_class({})
27+
end)
28+
.catch(get_error_handler('failed to debug the current test class'))
29+
.run()
30+
end
31+
32+
function M.debug_current_method()
33+
log.info('debug current test method')
34+
35+
return async(function()
36+
return JavaDap:new(jdtls()):execute_current_test_method()
37+
end)
38+
.catch(get_error_handler('failed to run the current test method'))
39+
.run()
40+
end
41+
42+
function M.run_current_method()
43+
log.info('run current test method')
44+
45+
return async(function()
46+
return JavaDap:new(jdtls())
47+
:execute_current_test_method({ noDebug = true })
48+
end)
49+
.catch(get_error_handler('failed to run the current test method'))
50+
.run()
51+
end
52+
53+
function M.config_dap()
54+
log.info('configuring dap')
55+
56+
return async(function()
57+
JavaDap:new(jdtls()):config_dap()
58+
end)
59+
.catch(get_error_handler('dap configuration failed'))
60+
.run()
61+
end
62+
63+
return M

lua/java/dap/init.lua

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
local log = require('java.utils.log')
22
local async = require('java-core.utils.async').sync
3+
local buf_util = require('java.utils.buffer')
4+
local win_util = require('java.utils.window')
5+
local notify = require('java-core.utils.notify')
36

47
local JavaCoreDap = require('java-core.dap')
58
local JavaCoreTestApi = require('java-core.api.test')
9+
local JavaCoreTestClient = require('java-core.ls.clients.java-test-client')
610

711
---@class JavaDap
812
---@field private client LspClient
913
---@field private dap JavaCoreDap
1014
---@field private test_api JavaCoreTestApi
15+
---@field private test_client java_core.TestClient
1116
local M = {}
1217

1318
---@param args { client: LspClient }
@@ -21,6 +26,10 @@ function M:new(args)
2126
client = args.client,
2227
})
2328

29+
o.test_client = JavaCoreTestClient:new({
30+
client = args.client,
31+
})
32+
2433
o.dap = JavaCoreDap:new({
2534
client = args.client,
2635
})
@@ -35,9 +44,36 @@ end
3544
function M:execute_current_test_class(config)
3645
log.debug('running the current class')
3746

38-
local buffer = vim.api.nvim_get_current_buf()
47+
return self.test_api:run_class_by_buffer(buf_util.get_curr_buf(), config)
48+
end
49+
50+
function M:execute_current_test_method(config)
51+
log.debug('running the current method')
52+
53+
local method = self:find_current_test_method()
54+
55+
if not method then
56+
notify.warn('cursor is not on a test method')
57+
return
58+
end
59+
60+
self.test_api:run_test({ method }, config)
61+
end
62+
63+
function M:find_current_test_method()
64+
log.debug('finding the current test method')
3965

40-
return self.test_api:run_class_by_buffer(buffer, config)
66+
local cursor = win_util.get_cursor()
67+
local methods = self.test_api:get_test_methods(buf_util.get_curr_uri())
68+
69+
for _, method in ipairs(methods) do
70+
local line_start = method.range.start.line
71+
local line_end = method.range['end'].line
72+
73+
if cursor.line >= line_start and cursor.line <= line_end then
74+
return method
75+
end
76+
end
4177
end
4278

4379
function M:config_dap()

lua/java/utils/buffer.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local M = {}
2+
3+
function M.get_curr_buf()
4+
return vim.api.nvim_get_current_buf()
5+
end
6+
7+
function M.get_curr_uri()
8+
local buffer = M.get_curr_buf()
9+
10+
return vim.uri_from_bufnr(buffer)
11+
end
12+
13+
return M

lua/java/utils/window.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local M = {}
2+
3+
function M.get_cursor()
4+
local cursor = vim.api.nvim_win_get_cursor(0)
5+
6+
return {
7+
-- apparently the index is not 0 based
8+
line = cursor[1] - 1,
9+
column = cursor[2],
10+
}
11+
end
12+
13+
return M

0 commit comments

Comments
 (0)