Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ Just install and start writing `public static void main(String[] args)`.
- Typed & documented APIs
- No callback hells I [promise](https://github.com/pyericz/promise-lua)

### APIs

### DAP

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

```lua
require('java').dap.config_dap()
```

### Test

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

```lua
require('java').test.run_current_test_class()
```

## How to Use

### Install the plugin
Expand Down Expand Up @@ -59,4 +77,5 @@ Yep! That's all :)
## Projects Acknowledgement

[nvim-jdtls](https://github.com/mfussenegger/nvim-jdtls) is a plugin that follows "Keep it simple, stupid!" approach.
If you love customizing things by yourself, then give nvim-jdtls a try.
If you love customizing things by yourself, then give nvim-jdtls a try. I may or may not have copied some code ;-)
Open source is beautiful!
5 changes: 3 additions & 2 deletions lua/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ local M = {}

function M.setup()
deps.check()
java_lspconfig.wrap_lspconfig_setup()
java_mason.install_dependencies()
java_dap.setup_dap()
java_lspconfig.wrap_lspconfig_setup()
java_lspconfig.register_class_file_decomplier()
java_dap.setup_dap_on_lsp_attach()
end

----------------------------------------------------------------------
Expand Down
23 changes: 12 additions & 11 deletions lua/java/dap/init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local JavaDap = require('java.dap.dapp')

local log = require('java-core.utils.log')
local state = require('java.state')
local notify = require('java-core.utils.notify')
local jdtls = require('java.jdtls')

local M = {}

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

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

---Runs the current test class
function M.run_current_test_class()
state.java_dap:run_current_test_class()
return JavaDap:new(jdtls()):run_current_test_class()
end

---Configures the dap
function M.config_dap()
state.java_dap:config_dap():thenCall(function()
notify.info('DAP configured')
end)
return JavaDap:new(jdtls())
:config_dap()
:thenCall(function()
notify.info('DAP configured')
end)
:catch(function(err)
notify.error('Failed to configure DAP', err)
end)
end

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

if client.name == 'jdtls' then
state.java_dap = JavaDap:new({
client = client,
})

log.info('setup java dap config & adapter')

state.java_dap:config_dap()
M.config_dap()
end
end

Expand Down
35 changes: 35 additions & 0 deletions lua/java/handlers/error.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local notify = require('java-core.utils.notify')
local log = require('java-core.utils.log')

local function table_tostring(tbl)
local str = ''
for _, v in ipairs(tbl) do
str = str .. '\n' .. tostring(v)
end

return str
end

---Returns a error handler
---@param msg string messages to show in the error
---@param ...? any values for place holders in the message
---@return fun(err: any) # function that log and notify the error
local function get_error_handler(msg, ...)
msg = string.format(msg, ...)

return function(err)
local trace = debug.traceback()

local log_obj = { msg }
table.insert(log_obj, err)
table.insert(log_obj, trace)

local log_str = table_tostring(log_obj)

log.error(table.unpack(log_obj))
notify.error(log_str)
error(log_str)
end
end

return get_error_handler
17 changes: 17 additions & 0 deletions lua/java/jdtls.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local get_error_handler = require('java.handlers.error')

---Returns an active jdtls client
---@return { client: LSPClient }
local function get_jdtls()
local clients = vim.lsp.get_active_clients({ name = 'jdtls' })

if #clients == 0 then
get_error_handler('could not find an active jdtls client')()
end

return {
client = clients[1],
}
end

return get_jdtls
49 changes: 49 additions & 0 deletions lua/java/lspconfig.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local log = require('java-core.utils.log')
local lspconfig = require('lspconfig')
local server = require('java-core.server')
local jdtls = require('java.jdtls')
local get_error_handler = require('java.handlers.error')

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

local M = {}

Expand Down Expand Up @@ -30,4 +34,49 @@ function M.wrap_lspconfig_setup()
end
end

---@class BufReadCmdCallbackArgs
---@field buf integer buffer number
---@field event string name of the event
---@field file string name of the file
---@field id integer event id?
---@field match string matched pattern in autocmd match
function M.register_class_file_decomplier()
vim.api.nvim_create_autocmd('BufReadCmd', {
pattern = 'jdt://*',
---@param opts BufReadCmdCallbackArgs
callback = function(opts)
---@type boolean
local done = false
local client_obj = jdtls()
local buffer = opts.buf

local function handle_file_content(text)
local lines = vim.split(text, '\n')
vim.api.nvim_buf_set_lines(buffer, 0, -1, true, lines)

vim.bo[buffer].swapfile = false
vim.bo[buffer].filetype = 'java'
vim.bo[buffer].modifiable = false

if not vim.lsp.buf_is_attached(buffer, client_obj.client.id) then
vim.lsp.buf_attach_client(buffer, client_obj.client.id)
end

done = true
end

JavaCoreJdtlsClient:new(client_obj)
:java_decompile(opts.file)
:thenCall(handle_file_content)
:catch(
get_error_handler('failed to decompile the class at %s', opts.file)
)

vim.wait(10000, function()
return done
end)
end,
})
end

return M
4 changes: 0 additions & 4 deletions lua/java/state.lua

This file was deleted.

2 changes: 1 addition & 1 deletion lua/java/utils/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local default_config = {
plugin = 'nvim-java',

-- Should print the output to neovim while running
use_console = true,
use_console = false,

-- Should highlighting be used in console (using echohl)
highlights = true,
Expand Down