feature/lookup: support multiple lookup functions & minor modes in :lookup
This commit is contained in:
parent
97812583f0
commit
6ba9259735
2 changed files with 44 additions and 43 deletions
|
@ -25,17 +25,18 @@
|
|||
((xref-backend-identifier-at-point (xref-find-backend)))))
|
||||
|
||||
(defun +lookup--jump-to (prop identifier)
|
||||
(let ((fn (plist-get +lookup-current-functions prop))
|
||||
(origin (point-marker)))
|
||||
(setq fn (or (command-remapping fn) fn))
|
||||
(condition-case e
|
||||
(or (if (commandp fn)
|
||||
(call-interactively fn)
|
||||
(funcall fn identifier))
|
||||
(/= (point-marker) origin))
|
||||
('error
|
||||
(message "%s" e)
|
||||
nil))))
|
||||
(cl-loop for fn in (plist-get '(:definition +lookup-definition-functions
|
||||
:references +lookup-references-functions
|
||||
:documentation +lookup-documentation-functions)
|
||||
prop)
|
||||
for fn = (or (command-remapping fn) fn)
|
||||
if (condition-case e
|
||||
(or (if (commandp fn)
|
||||
(call-interactively fn)
|
||||
(funcall fn identifier))
|
||||
(/= (point-marker) origin))
|
||||
('error (ignore (message "%s" e))))
|
||||
return it))
|
||||
|
||||
|
||||
;;
|
||||
|
@ -60,14 +61,9 @@ Failing all that, it will give up with an error."
|
|||
(cond ((null identifier)
|
||||
(user-error "Nothing under point"))
|
||||
|
||||
((and (plist-member +lookup-current-functions :definition)
|
||||
((and +lookup-definition-functions
|
||||
(+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)
|
||||
;; dumb-jump doesn't tell us if it succeeded or not
|
||||
(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."
|
||||
(interactive
|
||||
(list (+lookup--symbol-or-region)))
|
||||
(cond ((and (plist-member +lookup-current-functions :references)
|
||||
(cond ((and +lookup-references-functions
|
||||
(+lookup--jump-to :references identifier)))
|
||||
|
||||
((ignore-errors (xref-find-references identifier)
|
||||
t))
|
||||
|
||||
((and identifier
|
||||
(featurep 'counsel)
|
||||
(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'"
|
||||
(interactive
|
||||
(list (+lookup--symbol-or-region)))
|
||||
(cond ((and (plist-member +lookup-current-functions :documentation)
|
||||
(cond ((and +lookup-documentation-functions
|
||||
(+lookup--jump-to :documentation identifier)))
|
||||
|
||||
((and (featurep! :feature lookup +docsets)
|
||||
|
|
|
@ -33,13 +33,20 @@ produces an url. Used by `+lookup/online'.")
|
|||
(defvar +lookup-open-url-fn #'browse-url
|
||||
"Function to use to open search urls.")
|
||||
|
||||
(defvar +lookup-function-alist nil
|
||||
"An alist mapping major modes to jump function plists, describing what to do
|
||||
with `+lookup/definition', `+lookup/references' and `+lookup/documentation' are
|
||||
called.")
|
||||
(defvar +lookup-definition-functions '(xref-find-definitions)
|
||||
"Functions for `+lookup/definition' to try, before resorting to `dumb-jump'.
|
||||
Stops at the first function to return non-nil or change the current
|
||||
window/point.")
|
||||
|
||||
(defvar-local +lookup-current-functions nil
|
||||
"The enabled jump functions for the current buffer.")
|
||||
(defvar +lookup-references-functions '(xref-find-references)
|
||||
"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)
|
||||
"Defines a jump target for major MODES. PLIST accepts the following
|
||||
|
@ -57,9 +64,22 @@ properties:
|
|||
:xref-backend FN
|
||||
Defines an xref backend for a major-mode. With this, :definition and
|
||||
:references are unnecessary."
|
||||
`(dolist (mode (doom-enlist ,modes))
|
||||
(push (cons mode (list ,@plist))
|
||||
+lookup-function-alist)))
|
||||
`(progn
|
||||
,@(cl-loop for mode in (doom-enlist (doom-unquote modes))
|
||||
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
|
||||
(add-hook!
|
||||
|
@ -99,18 +119,6 @@ properties:
|
|||
(funcall orig-fn)))
|
||||
(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
|
||||
:when (featurep! :completion ivy)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue