feature/lookup: support multiple lookup functions & minor modes in :lookup

This commit is contained in:
Henrik Lissner 2018-04-22 23:55:49 -04:00
parent 97812583f0
commit 6ba9259735
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 44 additions and 43 deletions

View file

@ -25,17 +25,18 @@
((xref-backend-identifier-at-point (xref-find-backend))))) ((xref-backend-identifier-at-point (xref-find-backend)))))
(defun +lookup--jump-to (prop identifier) (defun +lookup--jump-to (prop identifier)
(let ((fn (plist-get +lookup-current-functions prop)) (cl-loop for fn in (plist-get '(:definition +lookup-definition-functions
(origin (point-marker))) :references +lookup-references-functions
(setq fn (or (command-remapping fn) fn)) :documentation +lookup-documentation-functions)
(condition-case e prop)
for fn = (or (command-remapping fn) fn)
if (condition-case e
(or (if (commandp fn) (or (if (commandp fn)
(call-interactively fn) (call-interactively fn)
(funcall fn identifier)) (funcall fn identifier))
(/= (point-marker) origin)) (/= (point-marker) origin))
('error ('error (ignore (message "%s" e))))
(message "%s" e) return it))
nil))))
;; ;;
@ -60,14 +61,9 @@ Failing all that, it will give up with an error."
(cond ((null identifier) (cond ((null identifier)
(user-error "Nothing under point")) (user-error "Nothing under point"))
((and (plist-member +lookup-current-functions :definition) ((and +lookup-definition-functions
(+lookup--jump-to :definition identifier))) (+lookup--jump-to :definition identifier)))
((ignore-errors (if other-window
(xref-find-definitions-other-window identifier)
(xref-find-definitions identifier))
t))
((and (require 'dumb-jump nil t) ((and (require 'dumb-jump nil t)
;; dumb-jump doesn't tell us if it succeeded or not ;; dumb-jump doesn't tell us if it succeeded or not
(let ((old-fn (symbol-function 'dumb-jump-get-results)) (let ((old-fn (symbol-function 'dumb-jump-get-results))
@ -110,12 +106,9 @@ Failing all that, it will give up with an error."
Tries `xref-find-references' and falls back to rg/ag." Tries `xref-find-references' and falls back to rg/ag."
(interactive (interactive
(list (+lookup--symbol-or-region))) (list (+lookup--symbol-or-region)))
(cond ((and (plist-member +lookup-current-functions :references) (cond ((and +lookup-references-functions
(+lookup--jump-to :references identifier))) (+lookup--jump-to :references identifier)))
((ignore-errors (xref-find-references identifier)
t))
((and identifier ((and identifier
(featurep 'counsel) (featurep 'counsel)
(let ((regex (rxt-quote-pcre identifier))) (let ((regex (rxt-quote-pcre identifier)))
@ -138,7 +131,7 @@ Goes down a list of possible backends:
4. Fall back to an online search, with `+lookup/online'" 4. Fall back to an online search, with `+lookup/online'"
(interactive (interactive
(list (+lookup--symbol-or-region))) (list (+lookup--symbol-or-region)))
(cond ((and (plist-member +lookup-current-functions :documentation) (cond ((and +lookup-documentation-functions
(+lookup--jump-to :documentation identifier))) (+lookup--jump-to :documentation identifier)))
((and (featurep! :feature lookup +docsets) ((and (featurep! :feature lookup +docsets)

View file

@ -33,13 +33,20 @@ produces an url. Used by `+lookup/online'.")
(defvar +lookup-open-url-fn #'browse-url (defvar +lookup-open-url-fn #'browse-url
"Function to use to open search urls.") "Function to use to open search urls.")
(defvar +lookup-function-alist nil (defvar +lookup-definition-functions '(xref-find-definitions)
"An alist mapping major modes to jump function plists, describing what to do "Functions for `+lookup/definition' to try, before resorting to `dumb-jump'.
with `+lookup/definition', `+lookup/references' and `+lookup/documentation' are Stops at the first function to return non-nil or change the current
called.") window/point.")
(defvar-local +lookup-current-functions nil (defvar +lookup-references-functions '(xref-find-references)
"The enabled jump functions for the current buffer.") "Functions for `+lookup/references' to try, before resorting to `dumb-jump'.
Stops at the first function to return non-nil or change the current
window/point.")
(defvar +lookup-documentation-functions ()
"Functions for `+lookup/documentation' to try, before resorting to
`dumb-jump'. Stops at the first function to return non-nil or change the current
window/point.")
(def-setting! :lookup (modes &rest plist) (def-setting! :lookup (modes &rest plist)
"Defines a jump target for major MODES. PLIST accepts the following "Defines a jump target for major MODES. PLIST accepts the following
@ -57,9 +64,22 @@ properties:
:xref-backend FN :xref-backend FN
Defines an xref backend for a major-mode. With this, :definition and Defines an xref backend for a major-mode. With this, :definition and
:references are unnecessary." :references are unnecessary."
`(dolist (mode (doom-enlist ,modes)) `(progn
(push (cons mode (list ,@plist)) ,@(cl-loop for mode in (doom-enlist (doom-unquote modes))
+lookup-function-alist))) for def-name = (intern (format "doom--init-lookup-%s" mode))
collect
`(defun ,def-name ()
(when (or (eq major-mode ',mode)
(bound-and-true-p ,mode))
(let ((xref ,(plist-get plist :xref-backend))
(def ,(plist-get plist :definition))
(ref ,(plist-get plist :references))
(doc ,(plist-get plist :docuemntation)))
(if xref (add-hook 'xref-backend-functions xref nil t))
(if def (add-hook '+lookup-definition-functions def nil t))
(if ref (add-hook '+lookup-references-functions ref nil t))
(if doc (add-hook '+lookup-documentation-functions doc nil t)))))
collect `(add-hook! ,mode #',def-name))))
;; Recenter buffer after certain jumps ;; Recenter buffer after certain jumps
(add-hook! (add-hook!
@ -99,18 +119,6 @@ properties:
(funcall orig-fn))) (funcall orig-fn)))
(advice-add #'projectile-find-tag :around #'+lookup*projectile-find-tag)) (advice-add #'projectile-find-tag :around #'+lookup*projectile-find-tag))
(defun +lookup|init-xref-backends ()
"Set `+lookup-current-functions' for the current buffer.
This variable is used by `+lookup/definition',`+lookup/references' and
`+lookup/documentation'."
(when-let* ((plist (cdr (assq major-mode +lookup-function-alist))))
(when-let* ((backend (plist-get plist :xref-backend)))
(make-variable-buffer-local 'xref-backend-functions)
(cl-pushnew backend xref-backend-functions :test #'eq))
(setq-local +lookup-current-functions plist)))
(add-hook 'after-change-major-mode-hook #'+lookup|init-xref-backends)
(def-package! ivy-xref (def-package! ivy-xref
:when (featurep! :completion ivy) :when (featurep! :completion ivy)