summaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
authorDominik Honnef <dominik.honnef@gmail.com>2014-06-17 15:52:29 -0400
committerDominik Honnef <dominik.honnef@gmail.com>2014-06-17 15:52:29 -0400
commitf386304f6c7eb3b3994c57d1e18b0bad2b6d29df (patch)
treeadd6de5443b4761eb9197ff4104edf73d581a931 /misc
parent68035a99127dc29cad56a033351ea2ac612c871e (diff)
downloadgo-f386304f6c7eb3b3994c57d1e18b0bad2b6d29df.tar.gz
misc/emacs: add new function godoc-at-point
LGTM=adonovan R=adonovan, ruiu CC=golang-codereviews https://codereview.appspot.com/107160048 Committer: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'misc')
-rw-r--r--misc/emacs/go-mode.el56
1 files changed, 44 insertions, 12 deletions
diff --git a/misc/emacs/go-mode.el b/misc/emacs/go-mode.el
index 6a2fcc0e4..de71e3097 100644
--- a/misc/emacs/go-mode.el
+++ b/misc/emacs/go-mode.el
@@ -762,7 +762,7 @@ you save any file, kind of defeating the point of autoloading."
;;;###autoload
(defun godoc (query)
- "Show go documentation for a query, much like M-x man."
+ "Show Go documentation for a query, much like M-x man."
(interactive (list (godoc--read-query)))
(unless (string= query "")
(set-process-sentinel
@@ -771,6 +771,31 @@ you save any file, kind of defeating the point of autoloading."
'godoc--buffer-sentinel)
nil))
+(defun godoc-at-point (point)
+ "Show Go documentation for the identifier at POINT.
+
+`godoc-at-point' requires godef to work.
+
+Due to a limitation in godoc, it is not possible to differentiate
+between functions and methods, which may cause `godoc-at-point'
+to display more documentation than desired."
+ ;; TODO(dominikh): Support executing godoc-at-point on a package
+ ;; name.
+ (interactive "d")
+ (condition-case nil
+ (let* ((output (godef--call point))
+ (file (car output))
+ (name-parts (split-string (cadr output) " "))
+ (first (car name-parts)))
+ (if (not (godef--successful-p file))
+ (message "%s" (godef--error file))
+ (godoc (format "%s %s"
+ (file-name-directory file)
+ (if (or (string= first "type") (string= first "const"))
+ (cadr name-parts)
+ (car name-parts))))))
+ (file-error (message "Could not run godef binary"))))
+
(defun go-goto-imports ()
"Move point to the block of imports.
@@ -1039,6 +1064,21 @@ description at POINT."
(with-current-buffer outbuf
(split-string (buffer-substring-no-properties (point-min) (point-max)) "\n")))))
+(defun godef--successful-p (output)
+ (not (or (string= "-" output)
+ (string= "godef: no identifier found" output)
+ (go--string-prefix-p "godef: no declaration found for " output)
+ (go--string-prefix-p "error finding import path for " output))))
+
+(defun godef--error (output)
+ (cond
+ ((godef--successful-p output)
+ nil)
+ ((string= "-" output)
+ "godef: expression is not defined anywhere")
+ (t
+ output)))
+
(defun godef-describe (point)
"Describe the expression at POINT."
(interactive "d")
@@ -1054,19 +1094,11 @@ description 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))
- ((go--string-prefix-p "error finding import path for " file)
- (message "%s" file))
- (t
+ (if (not (godef--successful-p file))
+ (message "%s" (godef--error file))
(push-mark)
(ring-insert find-tag-marker-ring (point-marker))
- (godef--find-file-line-column file other-window))))
+ (godef--find-file-line-column file other-window)))
(file-error (message "Could not run godef binary"))))
(defun godef-jump-other-window (point)