Skip to content

Instantly share code, notes, and snippets.

@syohex
Created August 4, 2016 03:22
Show Gist options
  • Save syohex/aa8379c7ec4e441f52150f15fc26c66c to your computer and use it in GitHub Desktop.
Save syohex/aa8379c7ec4e441f52150f15fc26c66c to your computer and use it in GitHub Desktop.
Like vim-go's GoAddTags but this has few its features
(defun go-add-tags (type begin end)
(interactive
(list
(or (and current-prefix-arg (completing-read "Type: " '(json yaml toml)))
"json")
(or (and (use-region-p) (region-beginning))
(line-beginning-position))
(or (and (use-region-p) (region-end))
(line-end-position))))
(deactivate-mark)
(let ((inside-struct-p
(save-excursion
(goto-char begin)
(ignore-errors
(backward-up-list))
(looking-back "struct\\s-*"))))
(unless inside-struct-p
(error "Here is not struct"))
(let ((end-line (line-number-at-pos end)))
(goto-char begin)
(goto-char (line-beginning-position))
(while (and (<= (line-number-at-pos) end-line) (not (eobp)))
(let ((bound (line-end-position)))
(when (re-search-forward "^\\s-*\\(\\S-+\\)\\s-+\\(\\S-+\\)" nil bound)
(goto-char (min bound (match-end 2)))
(let* ((field (match-string-no-properties 1))
(tag (s-snake-case field)))
(insert (format " `%s:\"%s\"`" type tag)))))
(forward-line 1))
(goto-char begin))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment