-
Notifications
You must be signed in to change notification settings - Fork 1
/
go-fill-struct.el
76 lines (62 loc) · 2.96 KB
/
go-fill-struct.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;;; go-fill-struct.el --- Fill struct for golang. -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Sergey Kostyaev <[email protected]>
;; Author: Sergey Kostyaev <[email protected]>
;; Keywords: tools
;; Package-Version: 20171225.331
;; Package-Commit: a613d0b378473eef39e8fd5724abe790aea84321
;; Version: 0.1.0
;; URL: https://github.com/s-kostyaev/go-fill-struct
;; Package-Requires: ((emacs "24"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; For use this packages you should install `fillstruct' first:
;; % go get -u github.com/davidrjenni/reftools/cmd/fillstruct
;;; Code:
(require 'json)
(defvar go-fill-struct--buf nil
"Currently used go buffer for fillstruct.")
;;;###autoload
(defun go-fill-struct ()
"Fill go struct at point."
(interactive)
(save-buffer)
(setq go-fill-struct--buf (buffer-name))
(let* ((cmd
(format
"fillstruct -file %s -offset %s"
(shell-quote-argument (buffer-file-name))
(- (position-bytes (point)) 1)))
(proc (start-process-shell-command "fillstruct" "*fillstruct*" cmd))
(sentinel (lambda (_proc _state)
(with-current-buffer "*fillstruct*"
(ignore-errors
(let* ((json-object-type 'hash-table)
(json-array-type 'list)
(json-key-type 'string)
(json (json-read-from-string
(buffer-substring-no-properties (point-min) (point-max))))
(json-data (or (car-safe json) json))
(begin-byte (+ (gethash "start" json-data) 1))
(end-byte (+ (gethash "end" json-data) 1))
(content (gethash "code" json-data)))
(with-current-buffer go-fill-struct--buf
(let* ((begin (byte-to-position begin-byte))
(end (byte-to-position end-byte)))
(delete-region begin end)
(goto-char begin)
(insert content)
(goto-char begin)
(goto-char (line-end-position))))))
(kill-buffer)))))
(set-process-sentinel proc sentinel)))
(provide 'go-fill-struct)
;;; go-fill-struct.el ends here