From 07f86d669bda736f8f57c2a964ceff9b9c31c936 Mon Sep 17 00:00:00 2001 From: Oliver <1571880470@qq.com> Date: Wed, 19 Mar 2025 15:54:00 +0200 Subject: [PATCH] feat(struct_tags): add :GoTagClear --- lua/gopher/init.lua | 1 + lua/gopher/struct_tags.lua | 13 +++++++++++-- plugin/gopher.vim | 1 + spec/fixtures/tags/clear_input.go | 11 +++++++++++ spec/fixtures/tags/clear_output.go | 11 +++++++++++ spec/integration/struct_tags_test.lua | 18 +++++++++++++++--- 6 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 spec/fixtures/tags/clear_input.go create mode 100644 spec/fixtures/tags/clear_output.go diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 0de883b..bb9888b 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -43,6 +43,7 @@ gopher.comment = require("gopher.comment").comment gopher.tags = { add = tags.add, rm = tags.remove, + clear = tags.clear, } gopher.test = { diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua index 82aaf04..0e00319 100644 --- a/lua/gopher/struct_tags.lua +++ b/lua/gopher/struct_tags.lua @@ -89,7 +89,8 @@ local function handler_user_args(args) return res end --- add tags to struct under cursor +---Adds tags to a struct under the cursor +---@param ... string Tags to add to the struct fields. If not provided, it will use [config.tag.default_tag] function struct_tags.add(...) local args = { ... } local fpath = vim.fn.expand "%" @@ -101,7 +102,8 @@ function struct_tags.add(...) handle_tags(fpath, bufnr, user_args) end --- remove tags to struct under cursor +---Removes tags from a struct under the cursor +---@param ... string Tags to add to the struct fields. If not provided, it will use [config.tag.default_tag] function struct_tags.remove(...) local args = { ... } local fpath = vim.fn.expand "%" @@ -113,4 +115,11 @@ function struct_tags.remove(...) handle_tags(fpath, bufnr, user_args) end +---Removes all tags from a struct under the cursor +function struct_tags.clear() + local fpath = vim.fn.expand "%" + local bufnr = vim.api.nvim_get_current_buf() + handle_tags(fpath, bufnr, { "-clear-tags" }) +end + return struct_tags diff --git a/plugin/gopher.vim b/plugin/gopher.vim index b61ed6e..dc924b9 100644 --- a/plugin/gopher.vim +++ b/plugin/gopher.vim @@ -1,5 +1,6 @@ command! -nargs=* GoTagAdd :lua require"gopher".tags.add() command! -nargs=* GoTagRm :lua require"gopher".tags.rm() +command! GoTagClear :lua require"gopher".tags.clear() command! GoTestAdd :lua require"gopher".test.add() command! GoTestsAll :lua require"gopher".test.all() command! GoTestsExp :lua require"gopher".test.exported() diff --git a/spec/fixtures/tags/clear_input.go b/spec/fixtures/tags/clear_input.go new file mode 100644 index 0000000..050b327 --- /dev/null +++ b/spec/fixtures/tags/clear_input.go @@ -0,0 +1,11 @@ +package main + +type Test struct { + ID int `json:"id" yaml:"id" xml:"id" db:"id"` + Name string `json:"name" yaml:"name" xml:"name" db:"name"` + Num int64 `json:"num" yaml:"num" xml:"num" db:"num"` + Another struct { + First int `json:"first" yaml:"first" xml:"first" db:"first"` + Second string `json:"second" yaml:"second" xml:"second" db:"second"` + } `json:"another" yaml:"another" xml:"another" db:"another"` +} diff --git a/spec/fixtures/tags/clear_output.go b/spec/fixtures/tags/clear_output.go new file mode 100644 index 0000000..7e27a27 --- /dev/null +++ b/spec/fixtures/tags/clear_output.go @@ -0,0 +1,11 @@ +package main + +type Test struct { + ID int + Name string + Num int64 + Another struct { + First int + Second string + } +} diff --git a/spec/integration/struct_tags_test.lua b/spec/integration/struct_tags_test.lua index e8f8606..e821192 100644 --- a/spec/integration/struct_tags_test.lua +++ b/spec/integration/struct_tags_test.lua @@ -10,7 +10,7 @@ local T = MiniTest.new_set { }, } T["struct_tags"] = MiniTest.new_set {} -T["struct_tags"]["works add"] = function() +T["struct_tags"]["should add tag"] = function() local tmp = t.tmpfile() local fixtures = t.get_fixtures "tags/add" t.writefile(tmp, fixtures.input) @@ -22,7 +22,7 @@ T["struct_tags"]["works add"] = function() t.eq(t.readfile(tmp), fixtures.output) end -T["struct_tags"]["works remove"] = function() +T["struct_tags"]["should remove tag"] = function() local tmp = t.tmpfile() local fixtures = t.get_fixtures "tags/remove" t.writefile(tmp, fixtures.input) @@ -34,7 +34,7 @@ T["struct_tags"]["works remove"] = function() t.eq(t.readfile(tmp), fixtures.output) end -T["struct_tags"]["works many structs"] = function() +T["struct_tags"]["should be able to handle many structs"] = function() local tmp = t.tmpfile() local fixtures = t.get_fixtures "tags/many" t.writefile(tmp, fixtures.input) @@ -46,4 +46,16 @@ T["struct_tags"]["works many structs"] = function() t.eq(t.readfile(tmp), fixtures.output) end +T["struct_tags"]["should clear struct"] = function() + local tmp = t.tmpfile() + local fixtures = t.get_fixtures "tags/clear" + t.writefile(tmp, fixtures.input) + + child.cmd("silent edit " .. tmp) + child.fn.setpos(".", { child.fn.bufnr "%", 3, 1, 0 }) + child.cmd "GoTagClear" + + t.eq(t.readfile(tmp), fixtures.output) +end + return T