Skip to content

Commit 4cefbc1

Browse files
committed
fix(position-parser): use latest treesitter methods
A lot of this is stolen directly from neotest-golang, specifically the buffer creation which was a neat shorthand. Also getting the content rather than using the row and col to parse it out just using treesitter to get the content.
1 parent 6137fc4 commit 4cefbc1

File tree

1 file changed

+38
-52
lines changed

1 file changed

+38
-52
lines changed

lua/neotest-kotlin/src/position-parser.lua

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,60 @@ local lib = require("neotest.lib")
33
local M = {}
44

55
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+
})
1111

12-
return positions
12+
return positions
1313
end
1414

1515
M.get_all_matches_as_string = function(path, query)
16-
local results = {}
16+
local language = "kotlin"
1717

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 })
1922

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
2327

24-
local code = file:read("*all")
28+
local tree = parser:parse()[1]
2529

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 = {}
2833

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)
3038

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
3247

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 })
3649

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
6551
end
6652

6753
-- This will take in a path to a file, run a treesitter query on it, and return the first match as a string.
6854
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
7460
end
7561

7662
return M

0 commit comments

Comments
 (0)