A Neovim plugin for adding and removing struct tags in Go, powered by gomodifytags.
Place your cursor inside a Go struct and run a command to add or remove tags -- the plugin uses treesitter to detect the struct name automatically.
- Neovim 0.9+
- gomodifytags installed and on your
$PATH
-- lazy.nvim
{
"simondrake/gomodifytags",
ft = "go",
opts = {},
}Pass options to setup() to set defaults. Every option can also be overridden per-call (see Usage).
{
"simondrake/gomodifytags",
ft = "go",
opts = {
transformation = "snakecase",
skip_unexported = true,
override = true,
sort = true,
options = { "json=omitempty" },
},
}| Option | Type | Default | Description |
|---|---|---|---|
transformation |
string |
"camelcase" |
Tag name casing: camelcase, snakecase, lispcase, pascalcase, titlecase, keep |
skip_unexported |
boolean |
true |
Skip unexported (lowercase) fields |
override |
boolean |
true |
Override existing tags instead of skipping them |
sort |
boolean |
false |
Sort tags alphabetically |
options |
string[] |
{} |
Tag options (e.g. { "json=omitempty", "xml=cdata" }) |
parse |
table |
{ enabled = false, separator = "--" } |
Enable string-based argument parsing for user commands (see User Commands) |
Add struct tags. Place your cursor inside a struct and run:
:lua require('gomodifytags').GoAddTags("json")Add multiple tags at once:
:lua require('gomodifytags').GoAddTags("json,yaml")Override defaults for a single call:
:lua require('gomodifytags').GoAddTags("json", { transformation = "snakecase", skip_unexported = false })Remove struct tags:
:lua require('gomodifytags').GoRemoveTags("json")Remove multiple tags:
:lua require('gomodifytags').GoRemoveTags("json,yaml")Pass debug = true to any call to print the constructed command and intermediate values:
:lua require('gomodifytags').GoAddTags("json", { debug = true })vim.keymap.set("n", "<leader>gat", function()
require("gomodifytags").GoAddTags("json")
end, { desc = "Add json struct tags" })
vim.keymap.set("n", "<leader>grt", function()
require("gomodifytags").GoRemoveTags("json")
end, { desc = "Remove json struct tags" })Because nvim_create_user_command passes arguments as a single string, passing a Lua table of overrides requires the parse option.
Enable it in your config:
opts = {
parse = { enabled = true, separator = "--" },
}Then create the commands:
vim.api.nvim_create_user_command("GoAddTags", function(cmd)
require("gomodifytags").GoAddTags(cmd.fargs[1], cmd.args)
end, { nargs = "+" })
vim.api.nvim_create_user_command("GoRemoveTags", function(cmd)
require("gomodifytags").GoRemoveTags(cmd.fargs[1], cmd.args)
end, { nargs = "+" })Usage (where -- is your configured separator):
:GoAddTags json -- { transformation = "snakecase" }
:GoRemoveTags yaml