Skip to content

Commit

Permalink
Merge pull request #5 from syohex/nest-struct
Browse files Browse the repository at this point in the history
Fix nest struct issue
  • Loading branch information
syohex committed Oct 3, 2016
2 parents af89c05 + fbe67f7 commit 1486535
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
40 changes: 30 additions & 10 deletions go-add-tags.el
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,41 @@
(backward-char 1)
(insert " " tag-field))))))

(defun go-add-tags--insert-tags (tags begin end conv-func)
(defun go-add-tags--overwrite-or-insert-tag (tags field conv-fn)
(let* ((prop (funcall conv-fn field))
(exist-p (go-add-tags--tag-exist-p)))
(if (not exist-p)
(insert (format " `%s`" (go-add-tags--tag-string tags prop)))
(go-add-tags--insert-tag tags prop))))

(defun go-add-tags--struct-name ()
(save-excursion
(when (ignore-errors (backward-sexp 1) t)
(back-to-indentation)
(when (looking-at "\\(\\S-+\\)\\s-+\\(?:\\[\\]\\)?struct\\s-*")
(match-string-no-properties 1)))))

(defun go-add-tags--insert-tags (tags begin end conv-fn)
(save-excursion
(let ((end-marker (make-marker)))
(set-marker end-marker end)
(goto-char begin)
(goto-char (line-beginning-position))
(while (and (<= (point) end-marker) (not (eobp)))
(let ((bound (min end-marker (line-end-position))))
(when (re-search-forward "^\\s-*\\(\\S-+\\)\\s-+\\(\\S-+\\)" bound t)
(goto-char (min bound (match-end 2)))
(let* ((field (funcall conv-func (match-string-no-properties 1)))
(exist-p (go-add-tags--tag-exist-p)))
(if (not exist-p)
(insert (format " `%s`" (go-add-tags--tag-string tags field)))
(go-add-tags--insert-tag tags field)))))
(cond ((re-search-forward "^\\s-*\\(\\S-+\\)\\s-+\\(\\S-+\\)" bound t)
(let ((field (match-string-no-properties 1))
(type-end (match-end 2))
(line (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
(unless (string-match-p "struct\\s-*+{" line)
(goto-char (min bound type-end))
(go-add-tags--overwrite-or-insert-tag tags field conv-fn))))
((re-search-forward "^\\s-*}" bound t)
(unless (zerop (current-indentation))
(let ((field (go-add-tags--struct-name)))
(when field
(go-add-tags--overwrite-or-insert-tag tags field conv-fn)))))))
(forward-line 1)))))

(defun go-add-tags--style-candidates (field)
Expand Down Expand Up @@ -117,7 +137,7 @@
ret))

;;;###autoload
(defun go-add-tags (tags begin end conv-func)
(defun go-add-tags (tags begin end conv-fn)
"Add field tags for struct fields."
(interactive
(list
Expand All @@ -135,7 +155,7 @@
(unless inside-struct-p
(error "Here is not struct"))
(save-excursion
(go-add-tags--insert-tags tags begin end (or conv-func #'identity)))))
(go-add-tags--insert-tags tags begin end (or conv-fn #'identity)))))

(provide 'go-add-tags)

Expand Down
28 changes: 28 additions & 0 deletions test/test.el
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,32 @@ type Foo struct {
(let ((line (buffer-substring (line-beginning-position) (line-end-position))))
(should (s-contains? "`yaml:\"apple_orange\" json:\"apple_orange\"`" line)))))

(ert-deftest nested-tag ()
"Insert tag for nested struct
https://github.com/syohex/emacs-go-add-tags/issues/4"
(with-go-temp-buffer
"
type APIErr struct {
Message string
Errors []struct {
Resource string
Code string
}
}
"
(let ((expected "
type APIErr struct {
Message string `json:\"message\"`
Errors []struct {
Resource string `json:\"resource\"`
Code string `json:\"code\"`
} `json:\"errors\"`
}
"))

(forward-line 1)
(go-add-tags--insert-tags
'("json") (point) (point-max) #'s-lower-camel-case)
(should (string= (buffer-string) expected)))))

;;; test.el ends here

0 comments on commit 1486535

Please sign in to comment.