fix(struct_tags): edge case with structs declared as var (#99)

* fix(struct_tags): edge case with structs declared as var

* test: test it and fix it

* fixup! test: test it and fix it

* fixup! fix(struct_tags): edge case with structs declared as var

* fixup! test: test it and fix it
This commit is contained in:
Smirnov Oleksandr 2025-03-22 13:21:15 +02:00 committed by GitHub
parent 7b91eb1cb6
commit e7a440acff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 96 additions and 6 deletions

View file

@ -1,8 +1,15 @@
local ts = {}
local queries = {
struct = [[
(type_spec name: (type_identifier) @_name
type: (struct_type))
[(type_spec name: (type_identifier) @_name
type: (struct_type))
(var_declaration (var_spec
name: (identifier) @_name @_var
type: (struct_type)))
(short_var_declaration
left: (expression_list (identifier) @_name @_var)
right: (expression_list (composite_literal
type: (struct_type))))]
]],
func = [[
[(function_declaration name: (identifier) @_name)
@ -40,7 +47,7 @@ end
---@param query vim.treesitter.Query
---@param node TSNode
---@param bufnr integer
---@return {name:string}
---@return {name:string, is_varstruct:boolean}
local function get_captures(query, node, bufnr)
local res = {}
for _, match, _ in query:iter_matches(node, bufnr) do
@ -49,6 +56,10 @@ local function get_captures(query, node, bufnr)
if capture_name == "_name" then
res["name"] = vim.treesitter.get_node_text(captured_node, bufnr)
end
if capture_name == "_var" then
res["is_varstruct"] = true
end
end
end
@ -59,6 +70,7 @@ end
---@field name string
---@field start_line integer
---@field end_line integer
---@field is_varstruct boolean
---@param bufnr integer
---@param parent_type string[]
@ -93,7 +105,15 @@ 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)
---
--- var_declaration is for cases like `var x struct{}`
--- short_var_declaration is for cases like `x := struct{}{}`
return do_stuff(bufnr, {
"type_spec",
"type_declaration",
"var_declaration",
"short_var_declaration",
}, queries.struct)
end
---@param bufnr integer