19 files changed,
300 insertions(+),
14 deletions(-)
Author:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2025-11-06 14:58:21 +0200
Change ID:
ssxzxuuvnxqozqklwnynymvtxmnylsop
Parent:
0de1892
jump to
M
README.md
@@ -72,6 +72,9 @@ ```vim
" add json tag :GoTagAdd json + " add json tag with omitempty option + :GoTagAdd json=omitempty + " remove yaml tag :GoTagRm yaml ```@@ -233,6 +236,8 @@ gotag = {
transform = "snakecase", -- default tags to add to struct fields default_tag = "json", + -- default tag option added struct fields, set to nil to disable + option = nil, }, iferr = { -- choose a custom error message
M
doc/gopher.nvim.txt
@@ -89,6 +89,10 @@ transform = "snakecase",
-- default tags to add to struct fields default_tag = "json", + + -- default tag option added struct fields, set to nil to disable + ---@type string|nil + option = nil, }, iferr = { -- choose a custom error message@@ -123,6 +127,9 @@ How to add/remove tags to struct fields:
1. Place cursor on the struct 2. Run `:GoTagAdd json` to add 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`
M
lua/gopher/_utils/init.lua
@@ -50,4 +50,23 @@ end
return string.rep(char, indent) end +---@generic T +---@param tbl T[] +---@return T[] +function utils.list_unique(tbl) + if vim.fn.has "nvim-0.12" == 1 then + return vim.list.unique(tbl) + end + + for i = #tbl, 1, -1 do + for j = 1, i - 1 do + if tbl[i] == tbl[j] then + table.remove(tbl, i) + break + end + end + end + return tbl +end + return utils
M
lua/gopher/config.lua
@@ -61,6 +61,10 @@ transform = "snakecase",
-- default tags to add to struct fields default_tag = "json", + + -- default tag option added struct fields, set to nil to disable + ---@type string|nil + option = nil, }, iferr = { -- choose a custom error message@@ -105,6 +109,7 @@ vim.validate("gotests.named", _config.gotests.named, "boolean")
vim.validate("gotag", _config.gotag, "table") vim.validate("gotag.transform", _config.gotag.transform, "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.message", _config.iferr.message, { "string", "nil" }) end
M
plugin/gopher.lua
@@ -48,7 +48,7 @@
-- :GoTag cmd("GoTagAdd", function(opts) require("gopher").tags.add { - tags = opts.fargs, + input = opts.fargs, range = (opts.count ~= -1) and { start = opts.line1, end_ = opts.line2,@@ -58,7 +58,7 @@ end, "*", true)
cmd("GoTagRm", function(opts) require("gopher").tags.rm { - tags = opts.fargs, + input = opts.fargs, range = (opts.count ~= -1) and { start = opts.line1, end_ = opts.line2,
A
spec/unit/struct_tag_test.lua
@@ -0,0 +1,68 @@
+local t = require "spec.testutils" +local _, T, st = t.setup "struct_tags" + +st["should parse tags"] = function() + local out = require("gopher.struct_tags").parse_args { "json", "yaml", "etc" } + + t.eq(out.tags, "json,yaml,etc") + t.eq(out.options, "") +end + +st["should parse tags separated by commas"] = function() + local out = require("gopher.struct_tags").parse_args { "json,yaml,etc" } + + t.eq(out.tags, "json,yaml,etc") + t.eq(out.options, "") +end + +st["should parse tags separated by command and spaces"] = function() + local out = require("gopher.struct_tags").parse_args { + "json,yaml", + "json=omitempty", + "xml=something", + } + + t.eq(out.tags, "json,yaml,xml") + t.eq(out.options, "json=omitempty,xml=something") +end + +st["should parse tag with an option"] = function() + local out = require("gopher.struct_tags").parse_args { + "json=omitempty", + "xml", + "xml=theoption", + } + + t.eq(out.tags, "json,xml") + t.eq(out.options, "json=omitempty,xml=theoption") +end + +st["should parse tags with an option"] = function() + local out = require("gopher.struct_tags").parse_args { "json=omitempty", "yaml" } + + t.eq(out.tags, "json,yaml") + t.eq(out.options, "json=omitempty") +end + +st["should parse tags with an option separated with comma"] = function() + local out = require("gopher.struct_tags").parse_args { "json=omitempty,yaml" } + + t.eq(out.tags, "json,yaml") + t.eq(out.options, "json=omitempty") +end + +st["should parse tags with options specified separately"] = function() + local out = require("gopher.struct_tags").parse_args { "json", "yaml", "json=omitempty" } + + t.eq(out.tags, "json,yaml") + t.eq(out.options, "json=omitempty") +end + +st["should parse tags with options specified separately and separated by comma"] = function() + local out = require("gopher.struct_tags").parse_args { "json,yaml,json=omitempty" } + + t.eq(out.tags, "json,yaml") + t.eq(out.options, "json=omitempty") +end + +return T
M
spec/unit/utils_test.lua
@@ -46,4 +46,14 @@
t.eq("\t\t", u.indent(line, indent)) end +utils["should .list_unique on list with duplicates"] = function() + local u = require "gopher._utils" + t.eq({ "json", "xml" }, u.list_unique { "json", "xml", "json" }) +end + +utils["should .list_unique on list with no duplicates"] = function() + local u = require "gopher._utils" + t.eq({ "json", "xml" }, u.list_unique { "json", "xml" }) +end + return T