Skip to content

Commit cd371ca

Browse files
authored
refactor: clean up init module (#144)
1 parent 235f8b1 commit cd371ca

File tree

2 files changed

+39
-73
lines changed

2 files changed

+39
-73
lines changed

.luarc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"diagnostics.globals": [
3+
"vim"
4+
]
5+
}

lua/neotest-java/init.lua

+34-73
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
---@diagnostic disable: undefined-doc-name, duplicate-doc-field, duplicate-set-field
2-
31
local File = require("neotest.lib.file")
42

53
local file_checker = require("neotest-java.core.file_checker")
@@ -12,7 +10,6 @@ local log = require("neotest-java.logger")
1210
local ch = require("neotest-java.context_holder")
1311
local lib = require("neotest.lib")
1412
local timer = require("neotest-java.util.timer")
15-
local nio = require("nio")
1613

1714
local detect_project_type = require("neotest-java.util.detect_project_type")
1815

@@ -28,86 +25,50 @@ local check_junit_jar = function(filepath)
2825
)
2926
end
3027

31-
---@class neotest.Adapter
32-
NeotestJavaAdapter = {
33-
name = "neotest-java",
34-
}
35-
36-
---Find the project root directory given a current directory to work from.
37-
---Should no root be found, the adapter can still be used in a non-project context if a test file matches.
38-
---@async
39-
---@param dir string @Directory to treat as cwd
40-
---@return string | nil @Absolute root dir of test suite
41-
function NeotestJavaAdapter.root(dir)
42-
local root = root_finder.find_root(dir)
43-
if root then
44-
ch.set_root(root)
45-
end
46-
return root
47-
end
48-
49-
---Filter directories when searching for test files
50-
---@async
51-
---@param name string Name of directory
52-
---@param rel_path string Path to directory, relative to root
53-
---@param root string Root directory of project
54-
---@return boolean
55-
function NeotestJavaAdapter.filter_dir(name, rel_path, root)
56-
return dir_filter.filter_dir(name, rel_path, root)
57-
end
58-
59-
---@async
60-
---@param file_path string
61-
---@return boolean
62-
function NeotestJavaAdapter.is_test_file(file_path)
63-
return file_checker.is_test_file(file_path)
64-
end
65-
66-
---Given a file path, parse all the tests within it.
67-
---@async
68-
---@param file_path string Absolute file path
69-
---@return neotest.Tree | nil
70-
function NeotestJavaAdapter.discover_positions(file_path)
71-
return position_discoverer.discover_positions(file_path)
72-
end
73-
7428
---@type neotest-java.Timer
7529
local test_timer = nil
7630

77-
---@param args neotest.RunArgs
78-
---@return nil | neotest.RunSpec | neotest.RunSpec[]
79-
function NeotestJavaAdapter.build_spec(args)
80-
test_timer = timer:start()
81-
local self = NeotestJavaAdapter
82-
check_junit_jar(ch.get_context().config.junit_jar)
31+
---@class neotest.Adapter
32+
NeotestJavaAdapter = {
33+
name = "neotest-java",
34+
root = function(dir)
35+
local root = root_finder.find_root(dir)
36+
if root then
37+
ch.set_root(root)
38+
end
39+
return root
40+
end,
41+
filter_dir = dir_filter.filter_dir,
42+
is_test_file = file_checker.is_test_file,
43+
discover_positions = position_discoverer.discover_positions,
8344

84-
-- TODO: find a way to avoid to make this steps every time
45+
build_spec = function(args)
46+
test_timer = timer:start()
47+
local self = NeotestJavaAdapter
48+
check_junit_jar(ch.get_context().config.junit_jar)
8549

86-
-- find root
87-
local root = ch.get_context().root or self.root(vim.fn.getcwd())
88-
assert(root, "root directory not found")
50+
-- TODO: find a way to avoid to make this steps every time
8951

90-
-- detect project type
91-
local project_type = detect_project_type(root)
52+
-- find root
53+
local root = ch.get_context().root or self.root(vim.fn.getcwd())
54+
assert(root, "root directory not found")
9255

93-
-- build spec
94-
return spec_builder.build_spec(args, project_type, ch.get_context().config)
95-
end
56+
-- detect project type
57+
local project_type = detect_project_type(root)
9658

97-
---@async
98-
---@param spec neotest.RunSpec
99-
---@param result neotest.StrategyResult
100-
---@param tree neotest.Tree
101-
---@return table<string, neotest.Result>
102-
function NeotestJavaAdapter.results(spec, result, tree)
103-
local results = result_builder.build_results(spec, result, tree)
59+
-- build spec
60+
return spec_builder.build_spec(args, project_type, ch.get_context().config)
61+
end,
62+
results = function(spec, result, tree)
63+
local results = result_builder.build_results(spec, result, tree)
10464

105-
if test_timer then
106-
lib.notify("Tests lasted " .. test_timer:stop() .. " ms.")
107-
end
65+
if test_timer then
66+
lib.notify("Tests lasted " .. test_timer:stop() .. " ms.")
67+
end
10868

109-
return results
110-
end
69+
return results
70+
end,
71+
};
11172

11273
-- on init
11374
(function()

0 commit comments

Comments
 (0)