@@ -3,74 +3,60 @@ local lib = require("neotest.lib")
3
3
local M = {}
4
4
5
5
M .parse = function (path , query )
6
- local positions = lib .treesitter .parse_positions (path , query , {
7
- nested_namespaces = true ,
8
- nested_tests = false ,
9
- fast = false ,
10
- })
6
+ local positions = lib .treesitter .parse_positions (path , query , {
7
+ nested_namespaces = true ,
8
+ nested_tests = false ,
9
+ fast = false ,
10
+ })
11
11
12
- return positions
12
+ return positions
13
13
end
14
14
15
15
M .get_all_matches_as_string = function (path , query )
16
- local results = {}
16
+ local language = " kotlin "
17
17
18
- local file = io.open (path )
18
+ local bufnr = vim .api .nvim_create_buf (false , true )
19
+ local content = vim .fn .readfile (path )
20
+ vim .api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , content )
21
+ vim .api .nvim_set_option_value (" filetype" , language , { buf = bufnr })
19
22
20
- if file == nil then
21
- error (" File not found at path: " .. path )
22
- end
23
+ local parser = vim .treesitter .get_parser (bufnr , language , {})
24
+ if not parser then
25
+ error (" Kotlin parser is not available. Please ensure it's installed." )
26
+ end
23
27
24
- local code = file : read ( " *all " )
28
+ local tree = parser : parse ()[ 1 ]
25
29
26
- local new_buffer_number = vim .api .nvim_create_buf (false , true )
27
- vim .api .nvim_buf_set_lines (new_buffer_number , 1 , - 1 , false , vim .split (code , " \n " ))
30
+ --- @type vim.treesitter.Query
31
+ local treesitter_query = vim .treesitter .query .parse (language , query )
32
+ local results = {}
28
33
29
- file :close ()
34
+ for _ , match , _ in treesitter_query :iter_matches (tree :root (), bufnr , 0 , - 1 ) do
35
+ for _ , nodes in pairs (match ) do
36
+ for _ , node in ipairs (nodes ) do
37
+ local text = vim .treesitter .get_node_text (node , bufnr )
30
38
31
- local language = " kotlin"
39
+ if type (text ) == " table" then
40
+ table.insert (results , table.concat (text , " \n " ))
41
+ else
42
+ table.insert (results , text )
43
+ end
44
+ end
45
+ end
46
+ end
32
47
33
- local parser = vim .treesitter .get_string_parser (code , language )
34
- local tree = parser :parse ()
35
- local root = tree [1 ]:root ()
48
+ vim .api .nvim_buf_delete (bufnr , { force = true })
36
49
37
- local query = vim .treesitter .query .parse (language , query )
38
-
39
- for _ , match , _ in query :iter_matches (root , new_buffer_number , root :start (), root :end_ (), {}) do
40
- for _ , node in pairs (match ) do
41
- local start_row , start_col = node :start ()
42
- local end_row , end_col = node :end_ ()
43
-
44
- local row_lines = vim .api .nvim_buf_get_lines (new_buffer_number , start_row + 1 , end_row + 2 , false )
45
-
46
- if # row_lines == 0 then
47
- print (" Error: position parser could not match the passed query." )
48
- return " "
49
- end
50
-
51
- if # row_lines > 1 then
52
- print (" Error: position parser currently only supports single line results." )
53
- return " "
54
- end
55
-
56
- local found_line = row_lines [1 ]
57
-
58
- local result = found_line :sub (start_col + 1 , end_col )
59
-
60
- results [# results + 1 ] = result
61
- end
62
- end
63
-
64
- return results
50
+ return results
65
51
end
66
52
67
53
-- This will take in a path to a file, run a treesitter query on it, and return the first match as a string.
68
54
M .get_first_match_string = function (path , query )
69
- local results = M .get_all_matches_as_string (path , query )
70
- if # results > 0 then
71
- return results [1 ]
72
- end
73
- return nil
55
+ local results = M .get_all_matches_as_string (path , query )
56
+ if # results > 0 then
57
+ return results [1 ]
58
+ end
59
+ return nil
74
60
end
75
61
76
62
return M
0 commit comments