Skip to content

Commit 10010a9

Browse files
NickHackmancodymikol
authored andcommitted
feat: remove appending " should" and " when"
1 parent 279ab22 commit 10010a9

File tree

1 file changed

+76
-33
lines changed

1 file changed

+76
-33
lines changed

lua/neotest-kotlin/treesitter/init.lua

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ local package_query = require("neotest-kotlin.treesitter.package-query")
66

77
local M = {}
88

9+
---@enum neotest.PositionType
10+
M.PositionType = {
11+
dir = "dir",
12+
file = "file",
13+
namespace = "namespace",
14+
test = "test",
15+
}
16+
17+
---@class neotest.Position
18+
---@field id string
19+
---@field type neotest.PositionType
20+
---@field name string
21+
---@field path string
22+
---@field range integer[]
23+
24+
---@class Position : neotest.Position
25+
---@field custom_id string a custom id that differs from the name
26+
927
---Get all matches for the query perform on the path
1028
---@param path string
1129
---@param query string
@@ -62,46 +80,71 @@ function M.java_package(file)
6280
return get_all_matches_as_string(file, package_query)[1]
6381
end
6482

83+
---Creates a neotest id of the form 'path::namespace::test' using the Position.custom_id if it exists otherwise name.
84+
---@param position Position
85+
---@param parents Position[]
86+
---@return string
87+
local function position_id(position, parents)
88+
return table.concat(
89+
vim
90+
.iter({
91+
position.path,
92+
---@param pos Position
93+
vim.tbl_map(function(pos)
94+
return pos.custom_id or pos.name
95+
end, parents),
96+
position.custom_id or position.name,
97+
})
98+
:flatten(math.huge)
99+
:totable(),
100+
"::"
101+
)
102+
end
103+
104+
---builds a neotest.Position from a treesitter query
105+
---@param file_path string
106+
---@param source string
107+
---@param captured_nodes table<string, userdata>
108+
---@param metadata table<string, vim.treesitter.query.TSMetadata>
109+
---@return Position
110+
local function build_position(file_path, source, captured_nodes, metadata)
111+
---@param captured_nodes table<string, userdata>
112+
local function get_match_type(captured_nodes)
113+
if captured_nodes["test.name"] then
114+
return "test"
115+
end
116+
if captured_nodes["namespace.name"] then
117+
return "namespace"
118+
end
119+
end
120+
121+
local match_type = get_match_type(captured_nodes)
122+
if match_type then
123+
local node_name = match_type .. ".name"
124+
---@type string
125+
local name = vim.treesitter.get_node_text(captured_nodes[node_name], source)
126+
127+
local definition = captured_nodes[match_type .. ".definition"]
128+
129+
return {
130+
type = match_type,
131+
path = file_path,
132+
name = name,
133+
custom_id = metadata[node_name] and metadata[node_name].text,
134+
range = { definition:range() },
135+
}
136+
end
137+
end
138+
65139
---Uses neotest.treeistter.parse_positions to discover all namespaces/tests in a file.
66140
---@param file string
67141
---@return neotest.Tree?
68142
function M.parse_positions(file)
69143
return neotest.treesitter.parse_positions(file, kotest_query, {
70144
nested_namespaces = true,
71145
nested_tests = false,
72-
---@type fun(file_path: string, source: string, captured_nodes: table<string, userdata>, metadata: table<string, vim.treesitter.query.TSMetadata>): neotest.Position|neotest.Position[]|nil Builds one or more positions from the captured nodes from a query match.
73-
build_position = function(file_path, source, captured_nodes, metadata)
74-
---@param captured_nodes table<string, userdata>
75-
local function get_match_type(captured_nodes)
76-
if captured_nodes["test.name"] then
77-
return "test"
78-
end
79-
if captured_nodes["namespace.name"] then
80-
return "namespace"
81-
end
82-
end
83-
84-
local match_type = get_match_type(captured_nodes)
85-
if match_type then
86-
local node_name = match_type .. ".name"
87-
---@type string
88-
local name =
89-
vim.treesitter.get_node_text(captured_nodes[node_name], source)
90-
91-
if metadata[node_name] ~= nil and metadata[node_name].text ~= nil then
92-
name = metadata[node_name].text
93-
end
94-
95-
local definition = captured_nodes[match_type .. ".definition"]
96-
97-
return {
98-
type = match_type,
99-
path = file_path,
100-
name = name,
101-
range = { definition:range() },
102-
}
103-
end
104-
end,
146+
build_position = build_position,
147+
position_id = position_id,
105148
})
106149
end
107150

0 commit comments

Comments
 (0)