refactor: struct tags (#94)

* refactor(struct_tags): finally my hands got to this

* feat(struct_tags): trim output

* feat(struct_tags): add :GoTagClear

* docgen

* refactor(struct_tags): error on out-of-bounds

* feat(struct_tags): add support for working with multiple tags at the once

* test(struct_tags): test both possible inputs

* refactor(struct_tags): add type annotation, dont force write

* refactor(struct_tags): optimization ig

* docs: fix

* fixup! refactor(struct_tags): add type annotation, dont force write

* task docgen

---------

Co-authored-by: Oliver <1571880470@qq.com>
This commit is contained in:
Smirnov Oleksandr 2025-03-19 18:06:33 +02:00 committed by GitHub
parent e9f2eef5e7
commit 55bc5787d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 177 additions and 70 deletions

11
spec/fixtures/tags/add_many_input.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
}
}

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

@ -0,0 +1,11 @@
package main
type Test struct {
ID int `test4:"id" test5:"id" test1:"id" test2:"id"`
Name string `test4:"name" test5:"name" test1:"name" test2:"name"`
Num int64 `test4:"num" test5:"num" test1:"num" test2:"num"`
Another struct {
First int `test4:"first" test5:"first" test1:"first" test2:"first"`
Second string `test4:"second" test5:"second" test1:"second" test2:"second"`
} `test4:"another" test5:"another" test1:"another" test2:"another"`
}

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

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

View file

@ -10,38 +10,74 @@ 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)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 3, 6, 0 })
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 6, 0 })
child.cmd "GoTagAdd json"
child.cmd "write"
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)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 4, 6, 0 })
child.fn.setpos(".", { child.fn.bufnr(tmp), 4, 6, 0 })
child.cmd "GoTagRm json"
child.cmd "write"
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)
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr "%", 10, 3, 0 })
child.fn.setpos(".", { child.fn.bufnr(tmp), 10, 3, 0 })
child.cmd "GoTagAdd testing"
child.cmd "write"
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(tmp), 3, 1, 0 })
child.cmd "GoTagClear"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
end
T["struct_tags"]["should add more than one tag"] = function()
local tmp = t.tmpfile()
local fixtures = t.get_fixtures "tags/add_many"
t.writefile(tmp, fixtures.input)
--- with comma, like gomodifytags
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 1 })
child.cmd "GoTagAdd test4,test5"
child.cmd "write"
-- without comma
child.cmd("silent edit " .. tmp)
child.fn.setpos(".", { child.fn.bufnr(tmp), 3, 1 })
child.cmd "GoTagAdd test1 test2"
child.cmd "write"
t.eq(t.readfile(tmp), fixtures.output)
end