emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/go-mode a88f1d5 115/495: add support for godef


From: ELPA Syncer
Subject: [nongnu] elpa/go-mode a88f1d5 115/495: add support for godef
Date: Sat, 7 Aug 2021 09:04:55 -0400 (EDT)

branch: elpa/go-mode
commit a88f1d5c63ec7f9f35eb5b46c01f969b5b6bc287
Author: Dominik Honnef <dominikh@fork-bomb.org>
Commit: Dominik Honnef <dominikh@fork-bomb.org>

    add support for godef
    
    this allows us to jump to definitions as well as get descriptions for
    expressions
---
 README.md  |  5 ++++-
 go-mode.el | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 1d4fde7..5066d5f 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,8 @@ Features
 - A function for jumping to the file's imports (`go-goto-imports`)
 - A function for adding imports, including tab completion (`go-import-add`, 
bound to `C-c C-a`)
 - A function for removing or commenting unused imports 
(`go-remove-unused-imports`)
+- `godef-describe` and `godef-jump` (`C-c C-d` and `C-c C-j`) to
+  describe expressions and jump to their declarations.
 - Adds basic support for imenu (functions and variables)
 
 Other extensions
@@ -43,7 +45,8 @@ Other extensions
 For a richer experience, consider installing
 [goflymake](https://github.com/dougm/goflymake) for on-the-fly syntax
 checking and [gocode](https://github.com/nsf/gocode) for auto
-completion.
+completion. Some features require you to install godef via `go get
+code.google.com/p/rog-go/exp/cmd/godef`.
 
 Also, if you're using YASnippet, consider using the snippets from
 [yasnippet-go](https://github.com/dominikh/yasnippet-go).
diff --git a/go-mode.el b/go-mode.el
index 980a182..4e615d4 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -123,6 +123,8 @@
     (define-key m ":" 'go-mode-insert-and-indent)
     (define-key m "=" 'go-mode-insert-and-indent)
     (define-key m (kbd "C-c C-a") 'go-import-add)
+    (define-key m (kbd "C-c C-j") 'godef-jump)
+    (define-key m (kbd "C-c C-d") 'godef-describe)
     m)
   "Keymap used by Go mode to implement electric keys.")
 
@@ -718,4 +720,58 @@ will be commented, otherwise they will be removed 
completely."
         (message "Removed %d imports" (length lines)))
       (if flymake-state (flymake-mode-on)))))
 
+(defun godef--find-file-line-column (specifier)
+  "Given a file name in the format of `filename:line:column',
+visit FILENAME and go to line LINE and column COLUMN."
+  (let* ((components (split-string specifier ":"))
+         (line (string-to-number (nth 1 components)))
+         (column (string-to-number (nth 2 components))))
+    (with-current-buffer (find-file (car components))
+      (goto-char (point-min))
+      (forward-line (1- line))
+      (beginning-of-line)
+      (forward-char (1- column))
+      (if (buffer-modified-p)
+          (message "Buffer is modified, file position might not have been 
correct")))))
+
+(defun godef--call (point)
+  "Call godef, acquiring definition position and expression
+description at POINT."
+  (if (go--xemacs-p)
+      (error "godef does not reliably work in XEmacs, sorry"))
+  (if (not buffer-file-name)
+      (message "Cannot use godef on a buffer without a file name")
+    (let ((outbuf (get-buffer-create "*godef*")))
+      (with-current-buffer outbuf
+        (erase-buffer))
+      (call-process-region (point-min) (point-max) "godef" nil outbuf nil "-i" 
"-t" "-f" (file-truename buffer-file-name) "-o" (number-to-string 
(position-bytes (point))))
+      (with-current-buffer outbuf
+        (split-string (buffer-substring-no-properties (point-min) (point-max)) 
"\n")))))
+
+(defun godef-describe (point)
+  "Describe the expression at POINT."
+  (interactive "d")
+  (condition-case nil
+      (let ((description (nth 1 (godef--call point))))
+        (if (string= "" description)
+            (message "No description found for expression at point")
+          (message "%s" description)))
+    (file-error (message "Could not run godef binary"))))
+
+(defun godef-jump (point)
+  "Jump to the definition of the expression at POINT."
+  (interactive "d")
+  (condition-case nil
+      (let ((file (car (godef--call point))))
+        (cond
+         ((string= "-" file)
+          (message "godef: expression is not defined anywhere"))
+         ((string= "godef: no identifier found" file)
+          (message "%s" file))
+         ((go--string-prefix-p "godef: no declaration found for " file)
+          (message "%s" file))
+         (t
+          (godef--find-file-line-column file))))
+    (file-error (message "Could not run godef binary"))))
+
 (provide 'go-mode)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]