github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/refactor/rename/go-rename.el (about)

     1  ;;; go-rename.el --- Integration of the 'gorename' tool into Emacs.
     2  
     3  ;; Copyright 2014 The Go Authors. 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  ;; Version: 0.1
     8  ;; Package-Requires: ((go-mode "1.3.1"))
     9  ;; Keywords: tools
    10  
    11  ;;; Commentary:
    12  
    13  ;; To install:
    14  
    15  ;; % go get golang.org/x/tools/cmd/gorename
    16  ;; % go build golang.org/x/tools/cmd/gorename
    17  ;; % mv gorename $HOME/bin/         # or elsewhere on $PATH
    18  
    19  ;; The go-rename-command variable can be customized to specify an
    20  ;; alternative location for the installed command.
    21  
    22  ;;; Code:
    23  
    24  (require 'compile)
    25  (require 'go-mode)
    26  (require 'thingatpt)
    27  
    28  (defgroup go-rename nil
    29    "Options specific to the Go rename."
    30    :group 'go)
    31  
    32  (defcustom go-rename-command "gorename"
    33    "The `gorename' command; by the default, $PATH is searched."
    34    :type 'string
    35    :group 'go-rename)
    36  
    37  ;;;###autoload
    38  (defun go-rename (new-name &optional force)
    39    "Rename the entity denoted by the identifier at point, using
    40  the `gorename' tool. With FORCE, call `gorename' with the
    41  `-force' flag."
    42    (interactive (list (read-string "New name: " (thing-at-point 'symbol))
    43                       current-prefix-arg))
    44    (if (not buffer-file-name)
    45        (error "Cannot use go-rename on a buffer without a file name"))
    46    ;; It's not sufficient to save the current buffer if modified,
    47    ;; since if gofmt-before-save is on the before-save-hook,
    48    ;; saving will disturb the selected region.
    49    (if (buffer-modified-p)
    50        (error "Please save the current buffer before invoking go-rename"))
    51    ;; Prompt-save all other modified Go buffers, since they might get written.
    52    (save-some-buffers nil #'(lambda ()
    53                (and (buffer-file-name)
    54                     (string= (file-name-extension (buffer-file-name)) ".go"))))
    55    (let* ((posflag (format "-offset=%s:#%d"
    56                            buffer-file-name
    57                            (1- (go--position-bytes (point)))))
    58           (env-vars (go-root-and-paths))
    59           (goroot-env (concat "GOROOT=" (car env-vars)))
    60           (gopath-env (concat "GOPATH=" (mapconcat #'identity (cdr env-vars) ":")))
    61           success)
    62      (with-current-buffer (get-buffer-create "*go-rename*")
    63        (setq buffer-read-only nil)
    64        (erase-buffer)
    65        (let ((args (append (list go-rename-command nil t nil posflag "-to" new-name) (if force '("-force")))))
    66          ;; Log the command to *Messages*, for debugging.
    67          (message "Command: %s:" args)
    68          (message "Running gorename...")
    69          ;; Use dynamic binding to modify/restore the environment
    70          (setq success (zerop (let ((process-environment (list* goroot-env gopath-env process-environment)))
    71            (apply #'call-process args))))
    72        (insert "\n")
    73        (compilation-mode)
    74        (setq compilation-error-screen-columns nil)
    75  
    76        ;; On success, print the one-line result in the message bar,
    77        ;; and hide the *go-rename* buffer.
    78        (if success
    79            (progn
    80              (message "%s" (go--buffer-string-no-trailing-space))
    81              (gofmt--kill-error-buffer (current-buffer)))
    82          ;; failure
    83          (let ((w (display-buffer (current-buffer))))
    84            (message "gorename exited")
    85            (shrink-window-if-larger-than-buffer w)
    86            (set-window-point w (point-min)))))))
    87  
    88    ;; Reload the modified files, saving line/col.
    89    ;; (Don't restore the point since the text has changed.)
    90    ;;
    91    ;; TODO(adonovan): should we also do this for all other files
    92    ;; that were updated (the tool can print them)?
    93    (let ((line (line-number-at-pos))
    94          (col (current-column)))
    95      (revert-buffer t t t) ; safe, because we just saved it
    96      (goto-char (point-min))
    97      (forward-line (1- line))
    98      (forward-char col)))
    99  
   100  
   101  (defun go--buffer-string-no-trailing-space ()
   102    (replace-regexp-in-string "[\t\n ]*\\'"
   103                              ""
   104                              (buffer-substring (point-min) (point-max))))
   105  
   106  (provide 'go-rename)
   107  
   108  ;;; go-rename.el ends here