Skip to content

Commit 8695b99

Browse files
authored
feat: add capability to lsp actions in .class files (#11)
1 parent 83e25bb commit 8695b99

File tree

8 files changed

+137
-19
lines changed

8 files changed

+137
-19
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ Just install and start writing `public static void main(String[] args)`.
2828
- Typed & documented APIs
2929
- No callback hells I [promise](https://github.com/pyericz/promise-lua)
3030

31+
### APIs
32+
33+
### DAP
34+
35+
- `config_dap` - DAP is autoconfigured on start up, but in case you want to force configure it again, you can use this API
36+
37+
```lua
38+
require('java').dap.config_dap()
39+
```
40+
41+
### Test
42+
43+
- `run_current_test_class` - Run the test class in the active buffer
44+
45+
```lua
46+
require('java').test.run_current_test_class()
47+
```
48+
3149
## How to Use
3250

3351
### Install the plugin
@@ -59,4 +77,5 @@ Yep! That's all :)
5977
## Projects Acknowledgement
6078

6179
[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.
80+
If you love customizing things by yourself, then give nvim-jdtls a try. I may or may not have copied some code ;-)
81+
Open source is beautiful!

lua/java.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ local M = {}
88

99
function M.setup()
1010
deps.check()
11-
java_lspconfig.wrap_lspconfig_setup()
1211
java_mason.install_dependencies()
13-
java_dap.setup_dap()
12+
java_lspconfig.wrap_lspconfig_setup()
13+
java_lspconfig.register_class_file_decomplier()
14+
java_dap.setup_dap_on_lsp_attach()
1415
end
1516

1617
----------------------------------------------------------------------

lua/java/dap/init.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
local JavaDap = require('java.dap.dapp')
22

33
local log = require('java-core.utils.log')
4-
local state = require('java.state')
54
local notify = require('java-core.utils.notify')
5+
local jdtls = require('java.jdtls')
66

77
local M = {}
88

99
---Setup dap config & adapter on jdtls attach event
10-
function M.setup_dap()
10+
function M.setup_dap_on_lsp_attach()
1111
log.info('add LspAttach event handlers to setup dap adapter & config')
1212

1313
vim.api.nvim_create_autocmd('LspAttach', {
@@ -20,14 +20,19 @@ end
2020

2121
---Runs the current test class
2222
function M.run_current_test_class()
23-
state.java_dap:run_current_test_class()
23+
return JavaDap:new(jdtls()):run_current_test_class()
2424
end
2525

2626
---Configures the dap
2727
function M.config_dap()
28-
state.java_dap:config_dap():thenCall(function()
29-
notify.info('DAP configured')
30-
end)
28+
return JavaDap:new(jdtls())
29+
:config_dap()
30+
:thenCall(function()
31+
notify.info('DAP configured')
32+
end)
33+
:catch(function(err)
34+
notify.error('Failed to configure DAP', err)
35+
end)
3136
end
3237

3338
---@private
@@ -36,13 +41,9 @@ function M.on_jdtls_attach(ev)
3641
local client = vim.lsp.get_client_by_id(ev.data.client_id)
3742

3843
if client.name == 'jdtls' then
39-
state.java_dap = JavaDap:new({
40-
client = client,
41-
})
42-
4344
log.info('setup java dap config & adapter')
4445

45-
state.java_dap:config_dap()
46+
M.config_dap()
4647
end
4748
end
4849

lua/java/handlers/error.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
local notify = require('java-core.utils.notify')
2+
local log = require('java-core.utils.log')
3+
4+
local function table_tostring(tbl)
5+
local str = ''
6+
for _, v in ipairs(tbl) do
7+
str = str .. '\n' .. tostring(v)
8+
end
9+
10+
return str
11+
end
12+
13+
---Returns a error handler
14+
---@param msg string messages to show in the error
15+
---@param ...? any values for place holders in the message
16+
---@return fun(err: any) # function that log and notify the error
17+
local function get_error_handler(msg, ...)
18+
msg = string.format(msg, ...)
19+
20+
return function(err)
21+
local trace = debug.traceback()
22+
23+
local log_obj = { msg }
24+
table.insert(log_obj, err)
25+
table.insert(log_obj, trace)
26+
27+
local log_str = table_tostring(log_obj)
28+
29+
log.error(table.unpack(log_obj))
30+
notify.error(log_str)
31+
error(log_str)
32+
end
33+
end
34+
35+
return get_error_handler

lua/java/jdtls.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local get_error_handler = require('java.handlers.error')
2+
3+
---Returns an active jdtls client
4+
---@return { client: LSPClient }
5+
local function get_jdtls()
6+
local clients = vim.lsp.get_active_clients({ name = 'jdtls' })
7+
8+
if #clients == 0 then
9+
get_error_handler('could not find an active jdtls client')()
10+
end
11+
12+
return {
13+
client = clients[1],
14+
}
15+
end
16+
17+
return get_jdtls

lua/java/lspconfig.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
local log = require('java-core.utils.log')
22
local lspconfig = require('lspconfig')
33
local server = require('java-core.server')
4+
local jdtls = require('java.jdtls')
5+
local get_error_handler = require('java.handlers.error')
6+
7+
local JavaCoreJdtlsClient = require('java-core.ls.clients.jdtls-client')
48

59
local M = {}
610

@@ -30,4 +34,49 @@ function M.wrap_lspconfig_setup()
3034
end
3135
end
3236

37+
---@class BufReadCmdCallbackArgs
38+
---@field buf integer buffer number
39+
---@field event string name of the event
40+
---@field file string name of the file
41+
---@field id integer event id?
42+
---@field match string matched pattern in autocmd match
43+
function M.register_class_file_decomplier()
44+
vim.api.nvim_create_autocmd('BufReadCmd', {
45+
pattern = 'jdt://*',
46+
---@param opts BufReadCmdCallbackArgs
47+
callback = function(opts)
48+
---@type boolean
49+
local done = false
50+
local client_obj = jdtls()
51+
local buffer = opts.buf
52+
53+
local function handle_file_content(text)
54+
local lines = vim.split(text, '\n')
55+
vim.api.nvim_buf_set_lines(buffer, 0, -1, true, lines)
56+
57+
vim.bo[buffer].swapfile = false
58+
vim.bo[buffer].filetype = 'java'
59+
vim.bo[buffer].modifiable = false
60+
61+
if not vim.lsp.buf_is_attached(buffer, client_obj.client.id) then
62+
vim.lsp.buf_attach_client(buffer, client_obj.client.id)
63+
end
64+
65+
done = true
66+
end
67+
68+
JavaCoreJdtlsClient:new(client_obj)
69+
:java_decompile(opts.file)
70+
:thenCall(handle_file_content)
71+
:catch(
72+
get_error_handler('failed to decompile the class at %s', opts.file)
73+
)
74+
75+
vim.wait(10000, function()
76+
return done
77+
end)
78+
end,
79+
})
80+
end
81+
3382
return M

lua/java/state.lua

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

lua/java/utils/log.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local default_config = {
1616
plugin = 'nvim-java',
1717

1818
-- Should print the output to neovim while running
19-
use_console = true,
19+
use_console = false,
2020

2121
-- Should highlighting be used in console (using echohl)
2222
highlights = true,

0 commit comments

Comments
 (0)