Skip to content

Commit dfe87a6

Browse files
committed
refactor: use regex instead of treesitter to be compatible with neovim nightly
1 parent 90caf70 commit dfe87a6

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed
Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
11
local read_file = require("neotest-java.util.read_file")
22

3-
local PACKAGE_QUERY = [[
4-
((package_declaration (scoped_identifier) @package.name))
5-
((package_declaration (identifier) @package.name))
6-
]]
7-
83
---Resolve the Java package name from a file.
94
---Returns "" if no package declaration is present.
105
---@param filename string
116
---@return string
127
local function resolve_package_name(filename)
13-
local function find_in_text(raw_query, content)
14-
local query = vim.treesitter.query.parse("java", raw_query)
15-
local lang_tree = vim.treesitter.get_string_parser(content, "java")
16-
local root = lang_tree:parse()[1]:root()
17-
18-
local result = {}
19-
for _, node in query:iter_captures(root, content, 0, -1) do
20-
result[#result + 1] = vim.treesitter.get_node_text(node, content)
21-
end
22-
return result
23-
end
24-
258
local ok, content = pcall(function()
269
return read_file(filename)
2710
end)
2811
if not ok then
2912
error(string.format("file does not exist: %s", filename))
3013
end
3114

32-
local package_lines = find_in_text(PACKAGE_QUERY, content)
33-
local package_name = (package_lines and package_lines[1]) or ""
34-
35-
return package_name
15+
-- Match: package com.example.foo;
16+
-- capture com.example.foo into %1
17+
local pkg = content:match("[\r\n]^%s*package%s+([%w_%.]+)%s*;%s*[\r\n]")
18+
or content:match("^%s*package%s+([%w_%.]+)%s*;") -- in case it's on the very first line
19+
or content:match("\n%s*package%s+([%w_%.]+)%s*;") -- general fallback
20+
return pkg or ""
3621
end
3722

3823
return resolve_package_name

0 commit comments

Comments
 (0)