spacemacs/core/core-jump.el
Eivind Fonn 928983da47 Refactor jump to definition
This commit defines:

- spacemacs-default-jump-handlers: a list of functions that can jump to
  definition in ALL modes.

- spacemacs-jump-handlers-MODE: a list of functions that can jump to
  definition in MODE.

- spacemacs-jump-handlers: a buffer-local list of functions that can
  jump to definition. This is made up of the values of the two previous
  variables whenever a given major mode is activated.

- spacemacs/jump-to-definition: a function that tries each function in
  spacemacs-jump-handlers in order, and stops when one of them takes us
  somewhere new.

- spacemacs|define-jump-handlers: a macro that
  * defines spacemacs-jump-handlers-MODE, possibly filled with initial
    functions
  * defines a function that is added to the hook of the given MODE
  * binds “SPC m g g” of that MODE to spacemacs/jump-to-definition

This is an attempt to harmonize all the different approaches to jumping.
Specifically,

- Existing intelligent jump packages that work for only a single mode
  should go to the beginning of spacemacs-jump-handlers-MODE. E.g.
  anaconda for python, ensime for scala, etc.

- Packages like gtags that work for several modes (but potentially not
  all) and which is dumber than the intelligent jumpers should go the
  the END of spacemacs-jump-handlers-MODE.

- Packages like dumb-jump that work for all modes should go to
  spacemacs-default-jump-handlers.

In all cases the order of the jump handlers in each list should be from
most to least intelligent.

Fixes #6619
2016-08-22 15:08:25 +02:00

62 lines
2.3 KiB
EmacsLisp

;;; core-jump.el --- Spacemacs Core File
;;
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defvar spacemacs-default-jump-handlers '()
"List of jump handlers available in every mode.")
(defvar-local spacemacs-jump-handlers '()
"List of jump handlers local to this buffer.")
(defmacro spacemacs|define-jump-handlers (mode &rest handlers)
"Defines jump handlers for the given MODE.
This defines a variable `spacemacs-jump-handlers-MODE' to which
handlers can be added, and a function added to MODE-hook which
sets `spacemacs-jump-handlers' in buffers of that mode."
(let ((mode-hook (intern (format "%S-hook" mode)))
(func (intern (format "spacemacs//init-jump-handlers-%S" mode)))
(handlers-list (intern (format "spacemacs-jump-handlers-%S" mode))))
`(progn
(defvar ,handlers-list ',handlers
,(format (concat "List of mode-specific jump handlers for %S. "
"These take priority over those in "
"`spacemacs-default-jump-handlers'.")
mode))
(defun ,func ()
(setq spacemacs-jump-handlers
(append ,handlers-list
spacemacs-default-jump-handlers)))
(add-hook ',mode-hook ',func)
(spacemacs/set-leader-keys-for-major-mode ',mode
"gg" 'spacemacs/jump-to-definition))))
(defun spacemacs/jump-to-definition ()
(interactive)
(catch 'done
(let ((old-buffer (current-buffer))
(old-point (point)))
(dolist (handler spacemacs-jump-handlers)
(ignore-errors
(call-interactively handler))
(unless (and (eq old-point (point))
(equal old-buffer (current-buffer)))
(throw 'done t))))
(message "No jump handler was able to find this symbol.")))
;; Set the `:jump' property manually instead of just using `evil-define-motion'
;; in an `eval-after-load' macro invocation because doing that prevents
;; `describe-function' from correctly finding the source.
;;
;; See discussion on https://github.com/syl20bnr/spacemacs/pull/6771
(with-eval-after-load 'evil
(evil-set-command-property 'spacemacs/jump-to-definition :jump t))
(provide 'core-jump)