feat: add test current method API by s1n7ax · Pull Request #31 · nvim-java/nvim-java · GitHub
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
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