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
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# nvim-java
# :coffee: nvim-java

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

## Features
## :dizzy: Features

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

## Why
## :bulb: Why

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

## How to Use
## :hammer: How to Install

## Pre-requisites
<details>

<summary>:pushpin: details</summary>

**Pre-requisites**

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

### Install the plugin
**Install the plugin**

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

Expand All @@ -52,25 +55,31 @@ return {
}
```

### Setup JDTLS like you would usually do
**Setup jdtls like you would usually do**

```lua
require('lspconfig').jdtls.setup({})
```

Yep! That's all :)

## APIs
</details>

## :computer: APIs

### DAP
<details>

<summary>:pushpin: details</summary>

**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
**Test**

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

Expand All @@ -84,8 +93,10 @@ require('java').test.run_current_test_class()
require('java').test.debug_current_test_class()
```

## Projects Acknowledgement
</details>

## :bookmark_tabs: 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. I may or may not have copied some code ;-)
Open source is beautiful!
If you love customizing things by yourself, then give nvim-jdtls a try. I may or may not have copied some code :wink:
Beauty of Open source!
11 changes: 6 additions & 5 deletions lua/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ local deps = require('java.utils.dependencies')
local mason = require('java.utils.mason')
local lspconfig = require('java.utils.lspconfig')

local dap = require('java.dap.api')
local ts = require('java.treesitter')
local test = require('java.api.test')
local dap = require('java.api.dap')
-- local ts = require('java.treesitter')

local M = {}

Expand All @@ -25,8 +26,8 @@ M.dap.config_dap = dap.config_dap
-- Test APIs --
----------------------------------------------------------------------
M.test = {}
M.test.run_current_test_class = dap.run_current_test_class
M.test.debug_current_test_class = dap.debug_current_test_class
M.test.run_current_test_class = test.run_current_test_class
M.test.debug_current_test_class = test.debug_current_test_class

----------------------------------------------------------------------
-- Manipulate --
Expand All @@ -35,7 +36,7 @@ M.manipulate = {}
-- M.manipulate.organize_imports = {}

function M.__run()
ts.find_main_method()
test.debug_current_method()
end

return M
20 changes: 0 additions & 20 deletions lua/java/dap/api.lua → lua/java/api/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,6 @@ function M.setup_dap_on_lsp_attach()
})
end

function M.run_current_test_class()
log.info('run current test class')

return async(function()
return JavaDap:new(jdtls()):execute_current_test_class({ noDebug = true })
end)
.catch(get_error_handler('failed to run the current test class'))
.run()
end

function M.debug_current_test_class()
log.info('debug current test class')

return async(function()
return JavaDap:new(jdtls()):execute_current_test_class({})
end)
.catch(get_error_handler('failed to debug the current test class'))
.run()
end

function M.config_dap()
log.info('configuring dap')

Expand Down
63 changes: 63 additions & 0 deletions lua/java/api/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local JavaDap = require('java.dap')

local log = require('java.utils.log')
local get_error_handler = require('java.handlers.error')
local jdtls = require('java.utils.jdtls')
local async = require('java-core.utils.async').sync

local M = {}

---Setup dap config & adapter on jdtls attach event

function M.run_current_test_class()
log.info('run current test class')

return async(function()
return JavaDap:new(jdtls()):execute_current_test_class({ noDebug = true })
end)
.catch(get_error_handler('failed to run the current test class'))
.run()
end

function M.debug_current_test_class()
log.info('debug current test class')

return async(function()
JavaDap:new(jdtls()):execute_current_test_class({})
end)
.catch(get_error_handler('failed to debug the current test class'))
.run()
end

function M.debug_current_method()
log.info('debug current test method')

return async(function()
return JavaDap:new(jdtls()):execute_current_test_method()
end)
.catch(get_error_handler('failed to run the current test method'))
.run()
end

function M.run_current_method()
log.info('run current test method')

return async(function()
return JavaDap:new(jdtls())
:execute_current_test_method({ noDebug = true })
end)
.catch(get_error_handler('failed to run the current test method'))
.run()
end

function M.config_dap()
log.info('configuring dap')

return async(function()
JavaDap:new(jdtls()):config_dap()
end)
.catch(get_error_handler('dap configuration failed'))
.run()
end

return M
40 changes: 38 additions & 2 deletions lua/java/dap/init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
local log = require('java.utils.log')
local async = require('java-core.utils.async').sync
local buf_util = require('java.utils.buffer')
local win_util = require('java.utils.window')
local notify = require('java-core.utils.notify')

local JavaCoreDap = require('java-core.dap')
local JavaCoreTestApi = require('java-core.api.test')
local JavaCoreTestClient = require('java-core.ls.clients.java-test-client')

---@class JavaDap
---@field private client LspClient
---@field private dap JavaCoreDap
---@field private test_api JavaCoreTestApi
---@field private test_client java_core.TestClient
local M = {}

---@param args { client: LspClient }
Expand All @@ -21,6 +26,10 @@ function M:new(args)
client = args.client,
})

o.test_client = JavaCoreTestClient:new({
client = args.client,
})

o.dap = JavaCoreDap:new({
client = args.client,
})
Expand All @@ -35,9 +44,36 @@ end
function M:execute_current_test_class(config)
log.debug('running the current class')

local buffer = vim.api.nvim_get_current_buf()
return self.test_api:run_class_by_buffer(buf_util.get_curr_buf(), config)
end

function M:execute_current_test_method(config)
log.debug('running the current method')

local method = self:find_current_test_method()

if not method then
notify.warn('cursor is not on a test method')
return
end

self.test_api:run_test({ method }, config)
end

function M:find_current_test_method()
log.debug('finding the current test method')

return self.test_api:run_class_by_buffer(buffer, config)
local cursor = win_util.get_cursor()
local methods = self.test_api:get_test_methods(buf_util.get_curr_uri())

for _, method in ipairs(methods) do
local line_start = method.range.start.line
local line_end = method.range['end'].line

if cursor.line >= line_start and cursor.line <= line_end then
return method
end
end
end

function M:config_dap()
Expand Down
13 changes: 13 additions & 0 deletions lua/java/utils/buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local M = {}

function M.get_curr_buf()
return vim.api.nvim_get_current_buf()
end

function M.get_curr_uri()
local buffer = M.get_curr_buf()

return vim.uri_from_bufnr(buffer)
end

return M
13 changes: 13 additions & 0 deletions lua/java/utils/window.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local M = {}

function M.get_cursor()
local cursor = vim.api.nvim_win_get_cursor(0)

return {
-- apparently the index is not 0 based
line = cursor[1] - 1,
column = cursor[2],
}
end

return M