@@ -6,6 +6,24 @@ local package_query = require("neotest-kotlin.treesitter.package-query")
6
6
7
7
local M = {}
8
8
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
+
9
27
--- Get all matches for the query perform on the path
10
28
--- @param path string
11
29
--- @param query string
@@ -62,46 +80,71 @@ function M.java_package(file)
62
80
return get_all_matches_as_string (file , package_query )[1 ]
63
81
end
64
82
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
+
65
139
--- Uses neotest.treeistter.parse_positions to discover all namespaces/tests in a file.
66
140
--- @param file string
67
141
--- @return neotest.Tree ?
68
142
function M .parse_positions (file )
69
143
return neotest .treesitter .parse_positions (file , kotest_query , {
70
144
nested_namespaces = true ,
71
145
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 ,
105
148
})
106
149
end
107
150
0 commit comments