all repos

gopher.nvim @ 969db908f8304aa18fc127a1fb73f1c8de8a5fd0

Minimalistic plugin for Go development
7 files changed, 96 insertions(+), 6 deletions(-)
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
Author: Smirnov Oleksandr ss2316544@gmail.com
Committed by: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2025-03-22 21:19:59 +0200
Parent: a993ece
M lua/gopher/_utils/ts.lua

@@ -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 @@

---@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 capture_name = query.captures[capture_id]

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 @@ ---@class gopher.TsResult

---@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
M lua/gopher/struct_tags.lua

@@ -44,10 +44,17 @@ local cmd = {

c.commands.gomodifytags, "-transform", c.gotag.transform, "-format", "json", - "-struct", st.name, "-file", fpath, "-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 table.insert(cmd, v)

@@ -60,7 +67,6 @@ error("failed to set tags " .. rs.stderr)

end local res = vim.json.decode(rs.stdout) - if res["errors"] then log.error("tags: got an error " .. vim.inspect(res)) error("failed to set tags " .. vim.inspect(res["errors"]))
A spec/fixtures/tags/svar_input.go

@@ -0,0 +1,11 @@

+package main + +func main() { + s := struct { + API string + Key string + }{ + API: "api.com", + Key: "key", + } +}
A spec/fixtures/tags/svar_output.go

@@ -0,0 +1,11 @@

+package main + +func main() { + s := struct { + API string `xml:"api"` + Key string `xml:"key"` + }{ + API: "api.com", + Key: "key", + } +}
A spec/fixtures/tags/var_input.go

@@ -0,0 +1,8 @@

+package main + +func main() { + var a struct { + TestField1 string + TestField2 string + } +}
A spec/fixtures/tags/var_output.go

@@ -0,0 +1,8 @@

+package main + +func main() { + var a struct { + TestField1 string `yaml:"test_field_1"` + TestField2 string `yaml:"test_field_2"` + } +}
M spec/integration/struct_tags_test.lua

@@ -82,4 +82,30 @@

t.eq(t.readfile(tmp), fixtures.output) end +T["struct_tags"]["should add tags on var"] = function() + local tmp = t.tmpfile() + local fixtures = t.get_fixtures "tags/var" + t.writefile(tmp, fixtures.input) + + child.cmd("silent edit " .. tmp) + child.fn.setpos(".", { child.fn.bufnr(tmp), 5, 3 }) + child.cmd "GoTagAdd yaml" + child.cmd "write" + + t.eq(t.readfile(tmp), fixtures.output) +end + +T["struct_tags"]["should add tags on short declr var"] = function() + local tmp = t.tmpfile() + local fixtures = t.get_fixtures "tags/svar" + t.writefile(tmp, fixtures.input) + + child.cmd("silent edit " .. tmp) + child.fn.setpos(".", { child.fn.bufnr(tmp), 4, 3 }) + child.cmd "GoTagAdd xml" + child.cmd "write" + + t.eq(t.readfile(tmp), fixtures.output) +end + return T