cmd/guru: emacs: add function for expandiong region

The go-guru-expand-region function uses the "what" query to determine
the enclosing regions. Consecutive calls to go-guru-expand-region will
reuse a cached version of the list.

Change-Id: Ice9ac5540c1b639c6cbdc505866bbab347be1e98
Reviewed-on: https://go-review.googlesource.com/21754
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Dominik Honnef 2016-04-09 20:52:02 +02:00 committed by Alan Donovan
parent b9ac36caf8
commit fb9c7fc43c
1 changed files with 31 additions and 1 deletions

View File

@ -73,6 +73,10 @@
nil
"The global timer used for highlighting identifiers.")
(defvar go-guru--last-enclosing
nil
"The remaining enclosing regions of the previous go-expand-region invocation.")
;; Extend go-mode-map.
(let ((m (define-prefix-command 'go-guru-map)))
(define-key m "d" #'go-guru-describe)
@ -85,7 +89,8 @@
(define-key m "s" #'go-guru-callstack) ; s for stack
(define-key m "e" #'go-guru-whicherrs) ; e for error
(define-key m "<" #'go-guru-callers)
(define-key m ">" #'go-guru-callees))
(define-key m ">" #'go-guru-callees)
(define-key m "x" #'go-guru-expand-region)) ;; x for expand
(define-key go-mode-map (kbd "C-c C-o") #'go-guru-map)
@ -447,6 +452,31 @@ timeout."
;; TODO(dominikh): a future feature may be to cycle through all uses
;; of an identifier.
(defun go-guru--enclosing ()
"Return a list of enclosing regions, with duplicates removed."
(let ((enclosing (cdr (assoc 'enclosing (go-guru-what)))))
(cl-remove-duplicates enclosing
:test (lambda (a b)
(and (= (cdr (assoc 'start a))
(cdr (assoc 'start b)))
(= (cdr (assoc 'end a))
(cdr (assoc 'end b))))))))
(defun go-guru-expand-region ()
"Expand region to the next enclosing syntactic unit."
(interactive)
(let* ((enclosing (if (eq last-command #'go-guru-expand-region)
go-guru--last-enclosing
(go-guru--enclosing)))
(block (if (> (length enclosing) 0) (elt enclosing 0))))
(when block
(goto-char (1+ (cdr (assoc 'start block))))
(set-mark (1+ (cdr (assoc 'end block))))
(setq go-guru--last-enclosing (subseq enclosing 1))
(message "Region: %s" (cdr (assoc 'desc block)))
(setq deactivate-mark nil))))
(provide 'go-guru)
;;; go-guru.el ends here