refactor(_utils.ts): all public methods are just adapters

This commit is contained in:
Oleksandr Smirnov 2025-03-18 23:07:30 +02:00
parent df4390d666
commit 0bbe52ed81
No known key found for this signature in database

View file

@ -1,5 +1,5 @@
local ts = { local ts = {}
queries = { local queries = {
struct = [[ struct = [[
(type_spec name: (type_identifier) @_name (type_spec name: (type_identifier) @_name
type: (struct_type)) type: (struct_type))
@ -13,7 +13,6 @@ local ts = {
-- 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)]],
-- func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]], -- func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]],
},
} }
---@param parent_type string|[string] ---@param parent_type string|[string]
@ -45,7 +44,7 @@ end
---@param node TSNode ---@param node TSNode
---@param bufnr integer ---@param bufnr integer
---@return {name:string} ---@return {name:string}
local function query_and_get_captures(query, node, bufnr) local function get_captures(query, node, bufnr)
local res = {} local res = {}
for _, match, _ in query:iter_matches(node, bufnr) do for _, match, _ in query:iter_matches(node, bufnr) do
for capture_id, captured_node in pairs(match) do for capture_id, captured_node in pairs(match) do
@ -59,24 +58,28 @@ local function query_and_get_captures(query, node, bufnr)
return res return res
end end
---@class gopher.TsResult
---@field name string
---@field start_line integer
---@field end_line integer
---@param bufnr integer ---@param bufnr integer
---@return table ---@param parent_type string|[string]
function ts.get_struct_under_cursor(bufnr) ---@param query string
---@return gopher.TsResult
local function do_stuff(bufnr, parent_type, query)
local node = vim.treesitter.get_node() local node = vim.treesitter.get_node()
if not node then if not node then
error "No nodes found under cursor" error "No nodes found under cursor"
end end
--- should be both type_spec and type_declaration local parent_node = get_parrent_node(parent_type, node)
--- because in cases like `type ( T struct{}, U strict{} )`
--- i will be choosing always last struct in the list
local parent_node = get_parrent_node({ "type_spec", "type_declaration" }, node)
if not parent_node then if not parent_node then
error "No struct found under cursor" error "No struct found under cursor"
end end
local query = vim.treesitter.query.parse("go", ts.queries.struct) local q = vim.treesitter.query.parse("go", query)
local res = query_and_get_captures(query, parent_node, bufnr) local res = get_captures(q, parent_node, bufnr)
local start_row, _, end_row, _ = parent_node:range() local start_row, _, end_row, _ = parent_node:range()
res["start_line"] = start_row + 1 res["start_line"] = start_row + 1
@ -85,26 +88,18 @@ function ts.get_struct_under_cursor(bufnr)
return res return res
end end
---@param bufnr integer
---@return table
function ts.get_struct_under_cursor(bufnr)
--- should be both type_spec and type_declaration
--- because in cases like `type ( T struct{}, U strict{} )`
--- i will be choosing always last struct in the list
return do_stuff(bufnr, { "type_spec", "type_declaration" }, queries.struct)
end
---@param bufnr integer ---@param bufnr integer
function ts.get_func_under_cursor(bufnr) function ts.get_func_under_cursor(bufnr)
local node = vim.treesitter.get_node() return do_stuff(bufnr, { "function_declaration", "method_declaration" }, queries.func)
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 end
return ts return ts