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

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