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

This commit is contained in:
Oleksandr Smirnov 2025-03-22 12:46:13 +02:00
parent 4a56f4666d
commit 46c093dbae
No known key found for this signature in database
2 changed files with 30 additions and 5 deletions

View file

@ -1,8 +1,14 @@
local ts = {} local ts = {}
local queries = { local queries = {
struct = [[ struct = [[
(type_spec name: (type_identifier) @_name [(type_spec name: (type_identifier) @_name
type: (struct_type)) type: (struct_type))
(var_spec name: (identifier) @_name
type: (struct_type))
(short_var_declaration
left: (expression_list (identifier) @_name @_var)
right: (expression_list (composite_literal
type: (struct_type))))]
]], ]],
func = [[ func = [[
[(function_declaration name: (identifier) @_name) [(function_declaration name: (identifier) @_name)
@ -49,6 +55,10 @@ local function get_captures(query, node, bufnr)
if capture_name == "_name" then if capture_name == "_name" then
res["name"] = vim.treesitter.get_node_text(captured_node, bufnr) res["name"] = vim.treesitter.get_node_text(captured_node, bufnr)
end end
if capture_name == "_var" then
res["is_varstruct"] = true
end
end end
end end
@ -59,6 +69,7 @@ end
---@field name string ---@field name string
---@field start_line integer ---@field start_line integer
---@field end_line integer ---@field end_line integer
---@field is_varstruct boolean
---@param bufnr integer ---@param bufnr integer
---@param parent_type string[] ---@param parent_type string[]
@ -93,7 +104,15 @@ function ts.get_struct_under_cursor(bufnr)
--- should be both type_spec and type_declaration --- should be both type_spec and type_declaration
--- because in cases like `type ( T struct{}, U strict{} )` --- because in cases like `type ( T struct{}, U strict{} )`
--- i will be choosing always last struct in the list --- i will be choosing always last struct in the list
return do_stuff(bufnr, { "type_spec", "type_declaration" }, queries.struct) ---
--- var_spec 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_spec",
"short_var_declaration",
}, queries.struct)
end end
---@param bufnr integer ---@param bufnr integer

View file

@ -44,11 +44,18 @@ local function handle_tags(fpath, bufnr, user_args)
c.commands.gomodifytags, c.commands.gomodifytags,
"-transform", c.gotag.transform, "-transform", c.gotag.transform,
"-format", "json", "-format", "json",
"-struct", st.name,
"-file", fpath, "-file", fpath,
"-w", "-w",
} }
if st.is_varstruct then
table.insert(cmd, "-line")
table.insert(cmd, string.format("%d,%d", st.start_line, st.end_line))
else
table.insert(cmd, "-struct")
table.insert(cmd, st.name)
end
for _, v in ipairs(user_args) do for _, v in ipairs(user_args) do
table.insert(cmd, v) table.insert(cmd, v)
end end
@ -60,7 +67,6 @@ local function handle_tags(fpath, bufnr, user_args)
end end
local res = vim.json.decode(rs.stdout) local res = vim.json.decode(rs.stdout)
if res["errors"] then if res["errors"] then
log.error("tags: got an error " .. vim.inspect(res)) log.error("tags: got an error " .. vim.inspect(res))
error("failed to set tags " .. vim.inspect(res["errors"])) error("failed to set tags " .. vim.inspect(res["errors"]))