feat(struct-tag): add default option
This commit is contained in:
parent
848a9dbe97
commit
3ad6f73b57
11 changed files with 122 additions and 7 deletions
|
|
@ -72,6 +72,9 @@ Requirements:
|
||||||
" add json tag
|
" add json tag
|
||||||
:GoTagAdd json
|
:GoTagAdd json
|
||||||
|
|
||||||
|
" add json tag with omitempty option
|
||||||
|
:GoTagAdd json=omitempty
|
||||||
|
|
||||||
" remove yaml tag
|
" remove yaml tag
|
||||||
:GoTagRm yaml
|
:GoTagRm yaml
|
||||||
```
|
```
|
||||||
|
|
@ -233,6 +236,8 @@ require("gopher").setup {
|
||||||
transform = "snakecase",
|
transform = "snakecase",
|
||||||
-- default tags to add to struct fields
|
-- default tags to add to struct fields
|
||||||
default_tag = "json",
|
default_tag = "json",
|
||||||
|
-- default tag option added struct fields, set to nil to disable
|
||||||
|
option = nil,
|
||||||
},
|
},
|
||||||
iferr = {
|
iferr = {
|
||||||
-- choose a custom error message
|
-- choose a custom error message
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,10 @@ to install them synchronously pass `{sync = true}` as an argument.
|
||||||
|
|
||||||
-- default tags to add to struct fields
|
-- default tags to add to struct fields
|
||||||
default_tag = "json",
|
default_tag = "json",
|
||||||
|
|
||||||
|
-- default tag option added struct fields, set to nil to disable
|
||||||
|
---@type string|nil
|
||||||
|
option = nil,
|
||||||
},
|
},
|
||||||
iferr = {
|
iferr = {
|
||||||
-- choose a custom error message
|
-- choose a custom error message
|
||||||
|
|
@ -124,6 +128,9 @@ How to add/remove tags to struct fields:
|
||||||
2. Run `:GoTagAdd json` to add json tags to struct fields
|
2. Run `:GoTagAdd json` to add json tags to struct fields
|
||||||
3. Run `:GoTagRm json` to remove json tags to struct fields
|
3. Run `:GoTagRm json` to remove json tags to struct fields
|
||||||
|
|
||||||
|
If you want to add/remove tag with options, you can use `json=omitempty` (where json is tag, and omitempty is its option).
|
||||||
|
Example: `:GoTagAdd xml json=omitempty`
|
||||||
|
|
||||||
To clear all tags from struct run: `:GoTagClear`
|
To clear all tags from struct run: `:GoTagClear`
|
||||||
|
|
||||||
NOTE: if you dont specify the tag it will use `json` as default
|
NOTE: if you dont specify the tag it will use `json` as default
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,10 @@ local default_config = {
|
||||||
|
|
||||||
-- default tags to add to struct fields
|
-- default tags to add to struct fields
|
||||||
default_tag = "json",
|
default_tag = "json",
|
||||||
|
|
||||||
|
-- default tag option added struct fields, set to nil to disable
|
||||||
|
---@type string|nil
|
||||||
|
option = nil,
|
||||||
},
|
},
|
||||||
iferr = {
|
iferr = {
|
||||||
-- choose a custom error message
|
-- choose a custom error message
|
||||||
|
|
@ -105,6 +109,7 @@ function config.setup(user_config)
|
||||||
vim.validate("gotag", _config.gotag, "table")
|
vim.validate("gotag", _config.gotag, "table")
|
||||||
vim.validate("gotag.transform", _config.gotag.transform, "string")
|
vim.validate("gotag.transform", _config.gotag.transform, "string")
|
||||||
vim.validate("gotag.default_tag", _config.gotag.default_tag, "string")
|
vim.validate("gotag.default_tag", _config.gotag.default_tag, "string")
|
||||||
|
vim.validate("gotag.option", _config.gotag.option, { "string", "nil" })
|
||||||
vim.validate("iferr", _config.iferr, "table")
|
vim.validate("iferr", _config.iferr, "table")
|
||||||
vim.validate("iferr.message", _config.iferr.message, { "string", "nil" })
|
vim.validate("iferr.message", _config.iferr.message, { "string", "nil" })
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@
|
||||||
--- 2. Run `:GoTagAdd json` to add json tags to struct fields
|
--- 2. Run `:GoTagAdd json` to add json tags to struct fields
|
||||||
--- 3. Run `:GoTagRm json` to remove json tags to struct fields
|
--- 3. Run `:GoTagRm json` to remove json tags to struct fields
|
||||||
---
|
---
|
||||||
|
--- If you want to add/remove tag with options, you can use `json=omitempty` (where json is tag, and omitempty is its option).
|
||||||
|
--- Example: `:GoTagAdd xml json=omitempty`
|
||||||
|
---
|
||||||
--- To clear all tags from struct run: `:GoTagClear`
|
--- To clear all tags from struct run: `:GoTagClear`
|
||||||
---
|
---
|
||||||
--- NOTE: if you dont specify the tag it will use `json` as default
|
--- NOTE: if you dont specify the tag it will use `json` as default
|
||||||
|
|
@ -104,7 +107,7 @@ local function handle_tags(fpath, bufnr, range, user_args)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Adds tags to a struct under the cursor
|
-- Adds tags to a struct under the cursor
|
||||||
-- See |gopher.nvim-struct-tags|
|
-- See `:h gopher.nvim-struct-tags`
|
||||||
---@param opts gopher.StructTagInput
|
---@param opts gopher.StructTagInput
|
||||||
---@dochide
|
---@dochide
|
||||||
function struct_tags.add(opts)
|
function struct_tags.add(opts)
|
||||||
|
|
@ -114,12 +117,14 @@ function struct_tags.add(opts)
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local user_args = st.parse_args(opts.input)
|
local user_args = st.parse_args(opts.input)
|
||||||
handle_tags(fpath, bufnr, opts.range, {
|
local a = {
|
||||||
"-add-tags",
|
"-add-tags",
|
||||||
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||||
(#user_args.options ~= 0) and "-add-options" or nil,
|
(user_args.options ~= "" or c.gotag.option) and "-add-options" or nil,
|
||||||
(#user_args.options ~= 0) and user_args.options or nil,
|
(user_args.options ~= "") and user_args.options or c.gotag.option,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
handle_tags(fpath, bufnr, opts.range, a)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Removes tags from a struct under the cursor
|
-- Removes tags from a struct under the cursor
|
||||||
|
|
@ -136,8 +141,8 @@ function struct_tags.remove(opts)
|
||||||
handle_tags(fpath, bufnr, opts.range, {
|
handle_tags(fpath, bufnr, opts.range, {
|
||||||
"-remove-tags",
|
"-remove-tags",
|
||||||
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||||
(#user_args.options ~= 0) and "-remove-options" or nil,
|
(user_args.options ~= "" or c.gotag.option ~= nil) and "-remove-options" or nil,
|
||||||
(#user_args.options ~= 0) and user_args.options or nil,
|
(user_args.options ~= "") and user_args.options or c.gotag.option,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
8
spec/fixtures/tags/overwrite_default_option_input.go
vendored
Normal file
8
spec/fixtures/tags/overwrite_default_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int
|
||||||
|
Another struct {
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/overwrite_default_option_output.go
vendored
Normal file
8
spec/fixtures/tags/overwrite_default_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `xml:"id,otheroption"`
|
||||||
|
Another struct {
|
||||||
|
Second string `xml:"second,otheroption"`
|
||||||
|
} `xml:"another,otheroption"`
|
||||||
|
}
|
||||||
11
spec/fixtures/tags/remove_with_option_input.go
vendored
Normal file
11
spec/fixtures/tags/remove_with_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `json:"id,omitempty" xml:"id,someoption"`
|
||||||
|
Name string `json:"name,omitempty" xml:"name,someoption"`
|
||||||
|
Num int64 `json:"num,omitempty" xml:"num,someoption"`
|
||||||
|
Another struct {
|
||||||
|
First int `json:"first,omitempty" xml:"first,someoption"`
|
||||||
|
Second string `json:"second,omitempty" xml:"second,someoption"`
|
||||||
|
} `json:"another,omitempty" xml:"another,someoption"`
|
||||||
|
}
|
||||||
11
spec/fixtures/tags/remove_with_option_output.go
vendored
Normal file
11
spec/fixtures/tags/remove_with_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `xml:"id,someoption"`
|
||||||
|
Name string `xml:"name,someoption"`
|
||||||
|
Num int64 `xml:"num,someoption"`
|
||||||
|
Another struct {
|
||||||
|
First int `xml:"first,someoption"`
|
||||||
|
Second string `xml:"second,someoption"`
|
||||||
|
} `xml:"another,someoption"`
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/with_default_option_input.go
vendored
Normal file
8
spec/fixtures/tags/with_default_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int
|
||||||
|
Another struct {
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/with_default_option_output.go
vendored
Normal file
8
spec/fixtures/tags/with_default_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `xml:"id,theoption"`
|
||||||
|
Another struct {
|
||||||
|
Second string `xml:"second,theoption"`
|
||||||
|
} `xml:"another,theoption"`
|
||||||
|
}
|
||||||
|
|
@ -105,4 +105,43 @@ struct_tags["should add tags with option"] = function()
|
||||||
t.cleanup(rs)
|
t.cleanup(rs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
struct_tags["should add tags with default option"] = function()
|
||||||
|
child.lua [[
|
||||||
|
require("gopher").setup {
|
||||||
|
gotag = { option = "xml=theoption" },
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
|
||||||
|
local rs = t.setup_test("tags/with_default_option", child, { 3, 6 })
|
||||||
|
child.cmd "GoTagAdd xml"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
|
struct_tags["should add tags and overwrite default option"] = function()
|
||||||
|
child.lua [[
|
||||||
|
require("gopher").setup {
|
||||||
|
gotag = { option = "xml=theoption" },
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
|
||||||
|
local rs = t.setup_test("tags/overwrite_default_option", child, { 3, 6 })
|
||||||
|
child.cmd "GoTagAdd xml=otheroption"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
|
struct_tags["should remove tag with specified option"] = function()
|
||||||
|
local rs = t.setup_test("tags/remove_with_option", child, { 3, 6 })
|
||||||
|
child.cmd "GoTagRm json=omitempty"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
return T
|
return T
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue