+24
-4
lua/gopher/_utils/ts.lua
+24
-4
lua/gopher/_utils/ts.lua
···
1
local ts = {}
2
local queries = {
3
struct = [[
4
-
(type_spec name: (type_identifier) @_name
5
-
type: (struct_type))
6
]],
7
func = [[
8
[(function_declaration name: (identifier) @_name)
···
40
---@param query vim.treesitter.Query
41
---@param node TSNode
42
---@param bufnr integer
43
-
---@return {name:string}
44
local function get_captures(query, node, bufnr)
45
local res = {}
46
for _, match, _ in query:iter_matches(node, bufnr) do
···
49
if capture_name == "_name" then
50
res["name"] = vim.treesitter.get_node_text(captured_node, bufnr)
51
end
52
end
53
end
54
···
59
---@field name string
60
---@field start_line integer
61
---@field end_line integer
62
63
---@param bufnr integer
64
---@param parent_type string[]
···
93
--- should be both type_spec and type_declaration
94
--- because in cases like `type ( T struct{}, U strict{} )`
95
--- i will be choosing always last struct in the list
96
-
return do_stuff(bufnr, { "type_spec", "type_declaration" }, queries.struct)
97
end
98
99
---@param bufnr integer
···
1
local ts = {}
2
local queries = {
3
struct = [[
4
+
[(type_spec name: (type_identifier) @_name
5
+
type: (struct_type))
6
+
(var_declaration (var_spec
7
+
name: (identifier) @_name @_var
8
+
type: (struct_type)))
9
+
(short_var_declaration
10
+
left: (expression_list (identifier) @_name @_var)
11
+
right: (expression_list (composite_literal
12
+
type: (struct_type))))]
13
]],
14
func = [[
15
[(function_declaration name: (identifier) @_name)
···
47
---@param query vim.treesitter.Query
48
---@param node TSNode
49
---@param bufnr integer
50
+
---@return {name:string, is_varstruct:boolean}
51
local function get_captures(query, node, bufnr)
52
local res = {}
53
for _, match, _ in query:iter_matches(node, bufnr) do
···
56
if capture_name == "_name" then
57
res["name"] = vim.treesitter.get_node_text(captured_node, bufnr)
58
end
59
+
60
+
if capture_name == "_var" then
61
+
res["is_varstruct"] = true
62
+
end
63
end
64
end
65
···
70
---@field name string
71
---@field start_line integer
72
---@field end_line integer
73
+
---@field is_varstruct boolean
74
75
---@param bufnr integer
76
---@param parent_type string[]
···
105
--- should be both type_spec and type_declaration
106
--- because in cases like `type ( T struct{}, U strict{} )`
107
--- i will be choosing always last struct in the list
108
+
---
109
+
--- var_declaration is for cases like `var x struct{}`
110
+
--- short_var_declaration is for cases like `x := struct{}{}`
111
+
return do_stuff(bufnr, {
112
+
"type_spec",
113
+
"type_declaration",
114
+
"var_declaration",
115
+
"short_var_declaration",
116
+
}, queries.struct)
117
end
118
119
---@param bufnr integer