fix(gotests): use new api

This commit is contained in:
Oleksandr Smirnov 2025-03-18 21:47:44 +02:00
parent 59a0e961c4
commit 3642c247e5
No known key found for this signature in database
5 changed files with 72 additions and 38 deletions

View file

@ -1,11 +1,14 @@
local ts = { local ts = {
queries = { queries = {
struct = [[ struct = [[
(type_spec name: (type_identifier) @_name (type_spec name: (type_identifier) @_name
type: (struct_type)) type: (struct_type))
]], ]],
func = [[
[(function_declaration name: (identifier) @_name)
(method_declaration name: (field_identifier) @_name)]
]],
-- struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]],
-- package = [[(package_clause (package_identifier)@package.name)@package.clause]], -- package = [[(package_clause (package_identifier)@package.name)@package.clause]],
-- interface = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]], -- interface = [[((type_declaration (type_spec name:(type_identifier) @interface.name type:(interface_type)))@interface.declaration)]],
-- method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]], -- method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]],
@ -13,16 +16,22 @@ local ts = {
}, },
} }
---@param parent_type string ---@param parent_type string|[string]
---@param node TSNode ---@param node TSNode
---@return TSNode? ---@return TSNode?
local function get_parrent_node(parent_type, node) local function get_parrent_node(parent_type, node)
---@type TSNode? ---@type TSNode?
local current = node local current = node
while current do while current do
if type(parent_type) == "string" then
if current:type() == parent_type then if current:type() == parent_type then
break break
end end
elseif type(parent_type) == "table" then
if vim.tbl_contains(parent_type, current:type()) then
break
end
end
current = current:parent() current = current:parent()
if current == nil then if current == nil then
@ -32,30 +41,13 @@ local function get_parrent_node(parent_type, node)
return current return current
end end
---@param bufnr string ---@param query vim.treesitter.Query
---@return table|nil ---@param node TSNode
function ts.get_struct_node_at_pos(bufnr) ---@param bufnr integer
vim.validate { ---@return {name:string}
bufnr = { bufnr, "number" }, local function query_and_get_captures(query, node, bufnr)
}
local node = vim.treesitter.get_node()
if not node then
error "No nodes found under cursor"
end
local res = {} local res = {}
local r = get_parrent_node("type_spec", node) for _, match, _ in query:iter_matches(node, bufnr) do
if not r then
error "No struct found under cursor"
end
local start_row, _, end_row, _ = r:range()
res["start_line"] = start_row + 1
res["end_line"] = end_row + 1
local query = vim.treesitter.query.parse("go", ts.queries.struct)
for _, match, _ in query:iter_matches(r, bufnr) do
for capture_id, captured_node in pairs(match) do for capture_id, captured_node in pairs(match) do
local capture_name = query.captures[capture_id] local capture_name = query.captures[capture_id]
if capture_name == "_name" then if capture_name == "_name" then
@ -67,4 +59,49 @@ function ts.get_struct_node_at_pos(bufnr)
return res return res
end end
---@param bufnr integer
---@return table
function ts.get_struct_under_cursor(bufnr)
local node = vim.treesitter.get_node()
if not node then
error "No nodes found under cursor"
end
local parent_node = get_parrent_node("type_spec", node)
if not parent_node then
error "No struct found under cursor"
end
local query = vim.treesitter.query.parse("go", ts.queries.struct)
local res = query_and_get_captures(query, parent_node, bufnr)
local start_row, _, end_row, _ = parent_node:range()
res["start_line"] = start_row + 1
res["end_line"] = end_row + 1
return res
end
---@param bufnr integer
function ts.get_func_under_cursor(bufnr)
local node = vim.treesitter.get_node()
if not node then
error "No nodes found under cursor"
end
local parent_node = get_parrent_node({ "function_declaration", "method_declaration" }, node)
if not parent_node then
error "No struct found under cursor"
end
local query = vim.treesitter.query.parse("go", ts.queries.func)
local res = query_and_get_captures(query, parent_node, bufnr)
local start_row, _, end_row, _ = parent_node:range()
res["start_line"] = start_row + 1
res["end_line"] = end_row + 1
return res
end
return ts return ts

View file

@ -15,7 +15,7 @@ local function generate(row, col)
return comment, ns return comment, ns
end end
ns = ts_utils.get_struct_node_at_pos(row, col, nil) ns = ts_utils.get_struct_under_cursor(row, col, nil)
if ns ~= nil then if ns ~= nil then
comment = "// " .. ns.name .. " " .. ns.type .. " " comment = "// " .. ns.name .. " " .. ns.type .. " "
return comment, ns return comment, ns

View file

@ -77,13 +77,10 @@ end
-- generate unit test for one function -- generate unit test for one function
function gotests.func_test() function gotests.func_test()
local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local bufnr = vim.api.nvim_get_current_buf()
if ns == nil or ns.name == nil then local func = ts_utils.get_func_under_cursor(bufnr)
u.notify("cursor on func/method and execute the command again", vim.log.levels.WARN)
return
end
add_test { "-only", ns.name } add_test { "-only", func.name }
end end
-- generate unit tests for all functions in current file -- generate unit tests for all functions in current file

View file

@ -41,7 +41,7 @@ local impl = {}
---@return string ---@return string
---@private ---@private
local function get_struct() local function get_struct()
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_struct_under_cursor(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then if ns == nil then
u.notify "put cursor on a struct or specify a receiver" u.notify "put cursor on a struct or specify a receiver"
return "" return ""

View file

@ -34,7 +34,7 @@ local struct_tags = {}
local function modify(...) local function modify(...)
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local struct = ts_utils.get_struct_node_at_pos(bufnr) local struct = ts_utils.get_struct_under_cursor(bufnr)
-- set user args for cmd -- set user args for cmd
local cmd_args = {} local cmd_args = {}