/emacs-go-tag

Edit field tags for golang struct fields

Primary LanguageEmacs LispGNU General Public License v3.0GPL-3.0

emacs-go-tag

https://img.shields.io/badge/license-GPL_v3-green.svg https://melpa.org/packages/go-tag-badge.svg https://stable.melpa.org/packages/go-tag-badge.svg

Introduction

Edit field tags for golang struct fields, based on gomodifytags. This package is inspired by GoAddTags of vim-go and go-add-tags.

gomodifytags

Go tool to modify/update field tags in structs. gomodifytags makes it easy to update, add or delete the tags in a struct field. It can be installed with
go install github.com/fatih/gomodifytags@latest

Installation

Available on all major package.el community maintained repos - MELPA Stable and MELPA repos.

MELPA Stable is recommended as it has the latest stable version. MELPA has a development snapshot for users who don’t mind breakage but don’t want to run from a git checkout.

You can install go-tag using the following command:

M-x package-install [RET] go-tag [RET]

or if you’d rather keep it in your dotfiles:

(unless (package-installed-p 'go-tag)
  (package-refresh-contents)
  (package-install 'go-tag))

If the installation doesn’t work try refreshing the package list:

M-x package-refresh-contents

Commands

If you are familiar with GoAddTags AND GoRemoveTags of vim-go , go-tag you will get started quickly.
  • go-tag-add

    :[range]go-tag-add [key],[option] [key1],[option] …

    Adds field tags for the fields of a struct. If called inside a struct it automatically add field tags with the json key and the value automatically generated based on the field name. An error message is given if it’s called outside a struct definition or if the file is not correctly formatted.

    If [range] is given, only the selected fields will be changed.

    The default json can be changed by providing one or more [key] arguments. An example of adding xml and db would be:

    :go-tag-add xml db

    If [option] is passed it’ll either add a new tag with an option or will modify existing tags. An example of adding omitempty to all json fields would be:

    :go-tag-add json,omitempty

    You can define a constant value instead of the default field based value. For example the following command will add `valid:"1"` to all fields.

    :go-tag-add valid=1

    https://github.com/brantou/emacs-go-tag/blob/master/img/go-tag-add.gif

  • go-tag-remove

    :[range]go-tag-remove [key],[option] [key1],[option1] …

    Rmove field tags for the fields of a struct. If called inside a struct it automatically remove all field tags. An error message is given if it’s called outside a struct definition or if the file is not correctly formatted

    If [range] is given, only the selected fields will be changed.

    If [key] is given, it will only remove those keys. Example:

    :go-tag-remove json

    If [option] is passed with a [key], it will only remove the options. Example, this will only remove omitempty options from fields containing json:

    :go-tag-remove json,omitempty

    https://github.com/brantou/emacs-go-tag/blob/master/img/go-tag-remove.gif

  • go-tag-refresh (Useful when designing structures)

    :[range]go-tag-refresh [key],[option] [key1],[option] …

    Equal to:

    :[range]go-tag-remove [key] [key1] …

    :[range]go-tag-add [key],[option] [key1],[option] …

Configuration

support the following transformations:

  • snakecase: BaseDomain -> base_domain
  • camelcase: BaseDomain -> baseDomain
  • lispcase: BaseDomain -> base-domain
(setq go-tag-args (list "-transform" "camelcase"))
(with-eval-after-load 'go-mode
  (define-key go-mode-map (kbd "C-c t") #'go-tag-add)
  (define-key go-mode-map (kbd "C-c T") #'go-tag-remove))