forked from dominikh/go-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-keyify.el
51 lines (43 loc) · 1.5 KB
/
go-keyify.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
;;; go-keyify.el --- keyify integration for Emacs
;; Copyright 2016 Dominik Honnef. All rights reserved.
;; Use of this source code is governed by a BSD-style
;; license that can be found in the LICENSE file.
;; Author: Dominik Honnef
;; Version: 1.0.0
;; Keywords: languages go
;; URL: https://github.com/dominikh/go-keyify
;;
;; This file is not part of GNU Emacs.
;;; Code:
(require 'json)
;;;###autoload
(defun go-keyify ()
"Turn an unkeyed struct literal into a keyed one.
Call with point on or in a struct literal."
(interactive)
(let* ((name (buffer-file-name))
(point (point))
(bpoint (1- (position-bytes point)))
(out (get-buffer-create "*go-keyify-output")))
(with-current-buffer out
(setq buffer-read-only nil)
(erase-buffer))
(with-current-buffer (get-buffer-create "*go-keyify-input*")
(setq buffer-read-only nil)
(erase-buffer)
(go--insert-modified-files)
(call-process-region (point-min) (point-max) "keyify" t out nil
"-modified"
"-json"
(format "%s:#%d" name bpoint)))
(let ((res (with-current-buffer out
(goto-char (point-min))
(json-read))))
(delete-region
(1+ (cdr (assoc 'start res)))
(1+ (cdr (assoc 'end res))))
(insert (cdr (assoc 'replacement res)))
(indent-region (1+ (cdr (assoc 'start res))) (point))
(goto-char point))))
(provide 'go-keyify)
;;; go-keyify.el ends here