feat(struct_tags): add :GoTagClear

This commit is contained in:
Oliver 2025-03-19 15:54:00 +02:00 committed by Oleksandr Smirnov
parent 10abcb661e
commit 07f86d669b
No known key found for this signature in database
6 changed files with 50 additions and 5 deletions

View file

@ -43,6 +43,7 @@ gopher.comment = require("gopher.comment").comment
gopher.tags = { gopher.tags = {
add = tags.add, add = tags.add,
rm = tags.remove, rm = tags.remove,
clear = tags.clear,
} }
gopher.test = { gopher.test = {

View file

@ -89,7 +89,8 @@ local function handler_user_args(args)
return res return res
end 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(...) function struct_tags.add(...)
local args = { ... } local args = { ... }
local fpath = vim.fn.expand "%" local fpath = vim.fn.expand "%"
@ -101,7 +102,8 @@ function struct_tags.add(...)
handle_tags(fpath, bufnr, user_args) handle_tags(fpath, bufnr, user_args)
end 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(...) function struct_tags.remove(...)
local args = { ... } local args = { ... }
local fpath = vim.fn.expand "%" local fpath = vim.fn.expand "%"
@ -113,4 +115,11 @@ function struct_tags.remove(...)
handle_tags(fpath, bufnr, user_args) handle_tags(fpath, bufnr, user_args)
end 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 return struct_tags

View file

@ -1,5 +1,6 @@
command! -nargs=* GoTagAdd :lua require"gopher".tags.add(<f-args>) command! -nargs=* GoTagAdd :lua require"gopher".tags.add(<f-args>)
command! -nargs=* GoTagRm :lua require"gopher".tags.rm(<f-args>) command! -nargs=* GoTagRm :lua require"gopher".tags.rm(<f-args>)
command! GoTagClear :lua require"gopher".tags.clear()
command! GoTestAdd :lua require"gopher".test.add() command! GoTestAdd :lua require"gopher".test.add()
command! GoTestsAll :lua require"gopher".test.all() command! GoTestsAll :lua require"gopher".test.all()
command! GoTestsExp :lua require"gopher".test.exported() command! GoTestsExp :lua require"gopher".test.exported()

11
spec/fixtures/tags/clear_input.go vendored Normal file
View file

@ -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"`
}

11
spec/fixtures/tags/clear_output.go vendored Normal file
View file

@ -0,0 +1,11 @@
package main
type Test struct {
ID int
Name string
Num int64
Another struct {
First int
Second string
}
}

View file

@ -10,7 +10,7 @@ local T = MiniTest.new_set {
}, },
} }
T["struct_tags"] = 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 tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/add" local fixtures = t.get_fixtures "tags/add"
t.writefile(tmp, fixtures.input) t.writefile(tmp, fixtures.input)
@ -22,7 +22,7 @@ T["struct_tags"]["works add"] = function()
t.eq(t.readfile(tmp), fixtures.output) t.eq(t.readfile(tmp), fixtures.output)
end end
T["struct_tags"]["works remove"] = function() T["struct_tags"]["should remove tag"] = function()
local tmp = t.tmpfile() local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/remove" local fixtures = t.get_fixtures "tags/remove"
t.writefile(tmp, fixtures.input) t.writefile(tmp, fixtures.input)
@ -34,7 +34,7 @@ T["struct_tags"]["works remove"] = function()
t.eq(t.readfile(tmp), fixtures.output) t.eq(t.readfile(tmp), fixtures.output)
end end
T["struct_tags"]["works many structs"] = function() T["struct_tags"]["should be able to handle many structs"] = function()
local tmp = t.tmpfile() local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/many" local fixtures = t.get_fixtures "tags/many"
t.writefile(tmp, fixtures.input) t.writefile(tmp, fixtures.input)
@ -46,4 +46,16 @@ T["struct_tags"]["works many structs"] = function()
t.eq(t.readfile(tmp), fixtures.output) t.eq(t.readfile(tmp), fixtures.output)
end 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 return T