mirror of
https://github.com/mfussenegger/nvim-jdtls
synced 2024-09-16 14:34:13 +02:00
Add a function to jump to tests & reverse
This commit is contained in:
parent
6e6352f671
commit
96e3978c3f
3 changed files with 88 additions and 2 deletions
|
@ -39,6 +39,7 @@ alternative:
|
|||
- [x] Debugger support via [nvim-dap][5]
|
||||
- [x] Optional vscode-java-test extensions
|
||||
- [x] Generate tests via `require("jdtls.tests").generate()`
|
||||
- [x] Jump to tests or subjects via `require("jdtls.tests").goto_subjects()`
|
||||
|
||||
Take a look at [a demo](https://github.com/mfussenegger/nvim-jdtls/issues/3) to
|
||||
see some of the functionality in action.
|
||||
|
|
|
@ -255,6 +255,17 @@ Functions which require vscode-java-test *jdtls.tests*
|
|||
|
||||
M.generate() *jdtls.tests.generate*
|
||||
Generate tests for the current class
|
||||
@param opts? {bufnr: integer}
|
||||
|
||||
|
||||
M.goto_subjects() *jdtls.tests.goto_subjects*
|
||||
Go to the related subjects
|
||||
If in a test file, this will jump to classes the test might cover
|
||||
If in a non-test file, this will jump to related tests.
|
||||
|
||||
If no candidates are found, this calls `generate()`
|
||||
|
||||
@param opts? {goto_tests: boolean}
|
||||
|
||||
|
||||
vim:tw=78:ts=8:noet:ft=help:norl:
|
||||
|
|
|
@ -5,8 +5,10 @@ local api = vim.api
|
|||
local M = {}
|
||||
|
||||
--- Generate tests for the current class
|
||||
function M.generate()
|
||||
local bufnr = api.nvim_get_current_buf()
|
||||
--- @param opts? {bufnr: integer}
|
||||
function M.generate(opts)
|
||||
opts = opts or {}
|
||||
local bufnr = opts.bufnr or api.nvim_get_current_buf()
|
||||
local win = api.nvim_get_current_win()
|
||||
local cursor = api.nvim_win_get_cursor(win) -- (1, 0) indexed
|
||||
local lnum = cursor[1]
|
||||
|
@ -53,6 +55,78 @@ function M.generate()
|
|||
end
|
||||
|
||||
|
||||
--- Go to the related subjects
|
||||
--- If in a test file, this will jump to classes the test might cover
|
||||
--- If in a non-test file, this will jump to related tests.
|
||||
---
|
||||
--- If no candidates are found, this calls `generate()`
|
||||
---
|
||||
--- @param opts? {goto_tests: boolean}
|
||||
function M.goto_subjects(opts)
|
||||
opts = opts or {}
|
||||
local win = api.nvim_get_current_win()
|
||||
local bufnr = api.nvim_get_current_buf()
|
||||
local function on_result(err, result)
|
||||
assert(not err, err)
|
||||
if api.nvim_get_current_win() ~= win then
|
||||
return
|
||||
end
|
||||
|
||||
result = result or {}
|
||||
local items = result and (result.items or {})
|
||||
if not next(items) then
|
||||
M.generate({ bufnr = bufnr })
|
||||
elseif #items == 1 then
|
||||
local test_buf = vim.uri_to_bufnr(items[1].uri)
|
||||
api.nvim_win_set_buf(win, test_buf)
|
||||
else
|
||||
local function label(x)
|
||||
return x.simpleName
|
||||
end
|
||||
table.sort(items, function(x, y)
|
||||
if x.outOfBelongingProject and not y.outOfBelongingProject then
|
||||
return false
|
||||
elseif not x.outOfBelongingProject and y.outOfBelongingProject then
|
||||
return true
|
||||
else
|
||||
if x.relevance == y.relevance then
|
||||
return x.simpleName < y.simpleName
|
||||
end
|
||||
return x.relevance < y.relevance
|
||||
end
|
||||
end)
|
||||
require("jdtls.ui").pick_one_async(items, "Goto: ", label, function(choice)
|
||||
if choice then
|
||||
local test_buf = vim.uri_to_bufnr(choice.uri)
|
||||
api.nvim_win_set_buf(win, test_buf)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
local util = require("jdtls.util")
|
||||
if opts.goto_tests == nil then
|
||||
local is_testfile_cmd = {
|
||||
command = "java.project.isTestFile",
|
||||
arguments = { vim.uri_from_bufnr(bufnr) }
|
||||
}
|
||||
util.execute_command(is_testfile_cmd, function(err, is_testfile)
|
||||
assert(not err, err)
|
||||
local command = {
|
||||
command = "vscode.java.test.navigateToTestOrTarget",
|
||||
arguments = { vim.uri_from_bufnr(bufnr), not is_testfile }
|
||||
}
|
||||
require("jdtls.util").execute_command(command, on_result, bufnr)
|
||||
end, bufnr)
|
||||
else
|
||||
local command = {
|
||||
command = "vscode.java.test.navigateToTestOrTarget",
|
||||
arguments = { vim.uri_from_bufnr(bufnr), opts.goto_tests }
|
||||
}
|
||||
require("jdtls.util").execute_command(command, on_result, bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---@private
|
||||
function M._ask_client_for_choice(prompt, choices, pick_many)
|
||||
local label = function(x)
|
||||
|
|
Loading…
Reference in a new issue