test: test it and fix it
This commit is contained in:
parent
46c093dbae
commit
d629fe9282
6 changed files with 76 additions and 4 deletions
|
|
@ -3,8 +3,9 @@ local queries = {
|
||||||
struct = [[
|
struct = [[
|
||||||
[(type_spec name: (type_identifier) @_name
|
[(type_spec name: (type_identifier) @_name
|
||||||
type: (struct_type))
|
type: (struct_type))
|
||||||
(var_spec name: (identifier) @_name
|
(var_declaration (var_spec
|
||||||
type: (struct_type))
|
name: (identifier) @_name @_var
|
||||||
|
type: (struct_type)))
|
||||||
(short_var_declaration
|
(short_var_declaration
|
||||||
left: (expression_list (identifier) @_name @_var)
|
left: (expression_list (identifier) @_name @_var)
|
||||||
right: (expression_list (composite_literal
|
right: (expression_list (composite_literal
|
||||||
|
|
@ -46,7 +47,7 @@ end
|
||||||
---@param query vim.treesitter.Query
|
---@param query vim.treesitter.Query
|
||||||
---@param node TSNode
|
---@param node TSNode
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
---@return {name:string}
|
---@return {name:string, is_varstruct:boolean}
|
||||||
local function get_captures(query, node, bufnr)
|
local function get_captures(query, node, bufnr)
|
||||||
local res = {}
|
local res = {}
|
||||||
for _, match, _ in query:iter_matches(node, bufnr) do
|
for _, match, _ in query:iter_matches(node, bufnr) do
|
||||||
|
|
@ -110,7 +111,7 @@ function ts.get_struct_under_cursor(bufnr)
|
||||||
return do_stuff(bufnr, {
|
return do_stuff(bufnr, {
|
||||||
"type_spec",
|
"type_spec",
|
||||||
"type_declaration",
|
"type_declaration",
|
||||||
"var_spec",
|
"var_declaration",
|
||||||
"short_var_declaration",
|
"short_var_declaration",
|
||||||
}, queries.struct)
|
}, queries.struct)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
11
spec/fixtures/tags/svar_input.go
vendored
Normal file
11
spec/fixtures/tags/svar_input.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := struct {
|
||||||
|
API string
|
||||||
|
Key string
|
||||||
|
}{
|
||||||
|
API: "api.com",
|
||||||
|
Key: "key",
|
||||||
|
}
|
||||||
|
}
|
||||||
11
spec/fixtures/tags/svar_output.go
vendored
Normal file
11
spec/fixtures/tags/svar_output.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := struct {
|
||||||
|
API string `xml:"api"`
|
||||||
|
Key string `xml:"key"`
|
||||||
|
}{
|
||||||
|
API: "api.com",
|
||||||
|
Key: "key",
|
||||||
|
}
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/var_input.go
vendored
Normal file
8
spec/fixtures/tags/var_input.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var a struct {
|
||||||
|
TestField1 string
|
||||||
|
TestField2 string
|
||||||
|
}
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/var_output.go
vendored
Normal file
8
spec/fixtures/tags/var_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var a struct {
|
||||||
|
TestField1 string `yaml:"test_field_1"`
|
||||||
|
TestField2 string `yaml:"test_field_2"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -21,6 +21,7 @@ T["struct_tags"]["should add tag"] = function()
|
||||||
child.cmd "write"
|
child.cmd "write"
|
||||||
|
|
||||||
t.eq(t.readfile(tmp), fixtures.output)
|
t.eq(t.readfile(tmp), fixtures.output)
|
||||||
|
t.deletefile(tmp)
|
||||||
end
|
end
|
||||||
|
|
||||||
T["struct_tags"]["should remove tag"] = function()
|
T["struct_tags"]["should remove tag"] = function()
|
||||||
|
|
@ -34,6 +35,7 @@ T["struct_tags"]["should remove tag"] = function()
|
||||||
child.cmd "write"
|
child.cmd "write"
|
||||||
|
|
||||||
t.eq(t.readfile(tmp), fixtures.output)
|
t.eq(t.readfile(tmp), fixtures.output)
|
||||||
|
t.deletefile(tmp)
|
||||||
end
|
end
|
||||||
|
|
||||||
T["struct_tags"]["should be able to handle many structs"] = function()
|
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"
|
child.cmd "write"
|
||||||
|
|
||||||
t.eq(t.readfile(tmp), fixtures.output)
|
t.eq(t.readfile(tmp), fixtures.output)
|
||||||
|
t.deletefile(tmp)
|
||||||
end
|
end
|
||||||
|
|
||||||
T["struct_tags"]["should clear struct"] = function()
|
T["struct_tags"]["should clear struct"] = function()
|
||||||
|
|
@ -60,6 +63,7 @@ T["struct_tags"]["should clear struct"] = function()
|
||||||
child.cmd "write"
|
child.cmd "write"
|
||||||
|
|
||||||
t.eq(t.readfile(tmp), fixtures.output)
|
t.eq(t.readfile(tmp), fixtures.output)
|
||||||
|
t.deletefile(tmp)
|
||||||
end
|
end
|
||||||
|
|
||||||
T["struct_tags"]["should add more than one tag"] = function()
|
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"
|
child.cmd "write"
|
||||||
|
|
||||||
t.eq(t.readfile(tmp), fixtures.output)
|
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
|
end
|
||||||
|
|
||||||
return T
|
return T
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue