diff --git a/lua/gopher/_utils/ts.lua b/lua/gopher/_utils/ts.lua index bf59ab5..4aed4c0 100644 --- a/lua/gopher/_utils/ts.lua +++ b/lua/gopher/_utils/ts.lua @@ -3,8 +3,9 @@ local queries = { struct = [[ [(type_spec name: (type_identifier) @_name type: (struct_type)) - (var_spec name: (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 @@ -46,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 @@ -110,7 +111,7 @@ function ts.get_struct_under_cursor(bufnr) return do_stuff(bufnr, { "type_spec", "type_declaration", - "var_spec", + "var_declaration", "short_var_declaration", }, queries.struct) end diff --git a/spec/fixtures/tags/svar_input.go b/spec/fixtures/tags/svar_input.go new file mode 100644 index 0000000..7831d01 --- /dev/null +++ b/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", + } +} diff --git a/spec/fixtures/tags/svar_output.go b/spec/fixtures/tags/svar_output.go new file mode 100644 index 0000000..f320eb2 --- /dev/null +++ b/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", + } +} diff --git a/spec/fixtures/tags/var_input.go b/spec/fixtures/tags/var_input.go new file mode 100644 index 0000000..97b4bc3 --- /dev/null +++ b/spec/fixtures/tags/var_input.go @@ -0,0 +1,8 @@ +package main + +func main() { + var a struct { + TestField1 string + TestField2 string + } +} diff --git a/spec/fixtures/tags/var_output.go b/spec/fixtures/tags/var_output.go new file mode 100644 index 0000000..e158a3a --- /dev/null +++ b/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"` + } +} diff --git a/spec/integration/struct_tags_test.lua b/spec/integration/struct_tags_test.lua index 4ac245b..e5e773d 100644 --- a/spec/integration/struct_tags_test.lua +++ b/spec/integration/struct_tags_test.lua @@ -21,6 +21,7 @@ T["struct_tags"]["should add tag"] = function() child.cmd "write" t.eq(t.readfile(tmp), fixtures.output) + t.deletefile(tmp) end T["struct_tags"]["should remove tag"] = function() @@ -34,6 +35,7 @@ T["struct_tags"]["should remove tag"] = function() child.cmd "write" t.eq(t.readfile(tmp), fixtures.output) + t.deletefile(tmp) end T["struct_tags"]["should be able to handle many structs"] = function() @@ -47,6 +49,7 @@ T["struct_tags"]["should be able to handle many structs"] = function() child.cmd "write" t.eq(t.readfile(tmp), fixtures.output) + t.deletefile(tmp) end T["struct_tags"]["should clear struct"] = function() @@ -60,6 +63,7 @@ T["struct_tags"]["should clear struct"] = function() child.cmd "write" t.eq(t.readfile(tmp), fixtures.output) + t.deletefile(tmp) end T["struct_tags"]["should add more than one tag"] = function() @@ -80,6 +84,35 @@ T["struct_tags"]["should add more than one tag"] = function() child.cmd "write" t.eq(t.readfile(tmp), fixtures.output) + t.deletefile(tmp) +end + +T["struct_tags"]["should add tags on var"] = function() + -- local tmp = t.tmpfile() + local tmp = "/tmp/test.go" + 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) + t.deletefile(tmp) end return T