Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / cmd / keyify / go-keyify.el
1 ;;; go-keyify.el --- keyify integration for Emacs
2
3 ;; Copyright 2016 Dominik Honnef. All rights reserved.
4 ;; Use of this source code is governed by a BSD-style
5 ;; license that can be found in the LICENSE file.
6
7 ;; Author: Dominik Honnef
8 ;; Version: 1.0.0
9 ;; Keywords: languages go
10 ;; URL: https://github.com/dominikh/go-keyify
11 ;;
12 ;; This file is not part of GNU Emacs.
13
14 ;;; Code:
15
16 (require 'json)
17
18 ;;;###autoload
19 (defun go-keyify ()
20   "Turn an unkeyed struct literal into a keyed one.
21
22 Call with point on or in a struct literal."
23   (interactive)
24   (let* ((name (buffer-file-name))
25          (point (point))
26          (bpoint (1- (position-bytes point)))
27          (out (get-buffer-create "*go-keyify-output")))
28     (with-current-buffer out
29       (setq buffer-read-only nil)
30       (erase-buffer))
31     (with-current-buffer (get-buffer-create "*go-keyify-input*")
32       (setq buffer-read-only nil)
33       (erase-buffer)
34       (go--insert-modified-files)
35       (call-process-region (point-min) (point-max) "keyify" t out nil
36                            "-modified"
37                            "-json"
38                            (format "%s:#%d" name bpoint)))
39     (let ((res (with-current-buffer out
40                  (goto-char (point-min))
41                  (json-read))))
42       (delete-region
43        (1+ (cdr (assoc 'start res)))
44        (1+ (cdr (assoc 'end res))))
45       (insert (cdr (assoc 'replacement res)))
46       (indent-region (1+ (cdr (assoc 'start res))) (point))
47       (goto-char point))))
48
49 (provide 'go-keyify)
50
51 ;;; go-keyify.el ends here