tools/lookup: Support type definition lookup handlers

This commit is contained in:
Seong Yong-ju 2020-05-12 01:06:23 +09:00
parent 13134726dc
commit d08f2dd888
3 changed files with 33 additions and 4 deletions

View file

@ -18,6 +18,9 @@ properties:
:implementations FN :implementations FN
Run when looking for implementations of a symbol in the current project. Used Run when looking for implementations of a symbol in the current project. Used
by `+lookup/implementations'. by `+lookup/implementations'.
:type-definition FN
Run when jumping to a symbol's type definition. Used by
`+lookup/type-definition'.
:references FN :references FN
Run when looking for usage references of a symbol in the current project. Used Run when looking for usage references of a symbol in the current project. Used
by `+lookup/references'. by `+lookup/references'.
@ -49,8 +52,9 @@ change the current buffer or window or return non-nil when it succeeds.
If it doesn't change the current buffer, or it returns nil, the lookup module If it doesn't change the current buffer, or it returns nil, the lookup module
will fall back to the next handler in `+lookup-definition-functions', will fall back to the next handler in `+lookup-definition-functions',
`+lookup-implementations-functions', `+lookup-references-functions', `+lookup-implementations-functions', `+lookup-type-definition-functions',
`+lookup-file-functions' or `+lookup-documentation-functions'. `+lookup-references-functions', `+lookup-file-functions' or
`+lookup-documentation-functions'.
Consecutive `set-lookup-handlers!' calls will overwrite previously defined Consecutive `set-lookup-handlers!' calls will overwrite previously defined
handlers for MODES. If used on minor modes, they are stacked onto handlers handlers for MODES. If used on minor modes, they are stacked onto handlers
@ -60,7 +64,7 @@ This can be passed nil as its second argument to unset handlers for MODES. e.g.
(set-lookup-handlers! 'python-mode nil) (set-lookup-handlers! 'python-mode nil)
\(fn MODES &key DEFINITION IMPLEMENTATIONS REFERENCES DOCUMENTATION FILE XREF-BACKEND ASYNC)" \(fn MODES &key DEFINITION IMPLEMENTATIONS TYPE-DEFINITION REFERENCES DOCUMENTATION FILE XREF-BACKEND ASYNC)"
(declare (indent defun)) (declare (indent defun))
(dolist (mode (doom-enlist modes)) (dolist (mode (doom-enlist modes))
(let ((hook (intern (format "%s-hook" mode))) (let ((hook (intern (format "%s-hook" mode)))
@ -72,17 +76,19 @@ This can be passed nil as its second argument to unset handlers for MODES. e.g.
(fset (fset
fn fn
(lambda () (lambda ()
(cl-destructuring-bind (&key definition implementations references documentation file xref-backend async) (cl-destructuring-bind (&key definition implementations type-definition references documentation file xref-backend async)
plist plist
(cl-mapc #'+lookup--set-handler (cl-mapc #'+lookup--set-handler
(list definition (list definition
implementations implementations
type-definition
references references
documentation documentation
file file
xref-backend) xref-backend)
(list '+lookup-definition-functions (list '+lookup-definition-functions
'+lookup-implementations-functions '+lookup-implementations-functions
'+lookup-type-definition-functions
'+lookup-references-functions '+lookup-references-functions
'+lookup-documentation-functions '+lookup-documentation-functions
'+lookup-file-functions '+lookup-file-functions
@ -139,6 +145,7 @@ This can be passed nil as its second argument to unset handlers for MODES. e.g.
(handlers (handlers
(plist-get (list :definition '+lookup-definition-functions (plist-get (list :definition '+lookup-definition-functions
:implementations '+lookup-implementations-functions :implementations '+lookup-implementations-functions
:type-definition '+lookup-type-definition-functions
:references '+lookup-references-functions :references '+lookup-references-functions
:documentation '+lookup-documentation-functions :documentation '+lookup-documentation-functions
:file '+lookup-file-functions) :file '+lookup-file-functions)
@ -259,6 +266,18 @@ the point or current buffer."
((+lookup--jump-to :implementations identifier nil arg)) ((+lookup--jump-to :implementations identifier nil arg))
((error "Couldn't find the implementations of %S" identifier)))) ((error "Couldn't find the implementations of %S" identifier))))
;;;###autoload
(defun +lookup/type-definition (identifier &optional arg)
"Jump to the type definition of IDENTIFIER (defaults to the symbol at point).
Each function in `+lookup-type-definition-functions' is tried until one changes
the point or current buffer."
(interactive (list (doom-thing-at-point-or-region)
current-prefix-arg))
(cond ((null identifier) (user-error "Nothing under point"))
((+lookup--jump-to :type-definition identifier nil arg))
((error "Couldn't find the definition of %S" identifier))))
;;;###autoload ;;;###autoload
(defun +lookup/references (identifier &optional arg) (defun +lookup/references (identifier &optional arg)
"Show a list of usages of IDENTIFIER (defaults to the symbol at point) "Show a list of usages of IDENTIFIER (defaults to the symbol at point)

View file

@ -63,6 +63,15 @@ If the argument is interactive (satisfies `commandp'), it is called with
argument: the identifier at point. See `set-lookup-handlers!' about adding to argument: the identifier at point. See `set-lookup-handlers!' about adding to
this list.") this list.")
(defvar +lookup-type-definition-functions ()
"Functions for `+lookup/type-definition' to try. Stops at the first function to
return non-nil or change the current window/point.
If the argument is interactive (satisfies `commandp'), it is called with
`call-interactively' (with no arguments). Otherwise, it is called with one
argument: the identifier at point. See `set-lookup-handlers!' about adding to
this list.")
(defvar +lookup-references-functions (defvar +lookup-references-functions
'(+lookup-xref-references-backend-fn '(+lookup-xref-references-backend-fn
+lookup-project-search-backend-fn) +lookup-project-search-backend-fn)

View file

@ -55,6 +55,7 @@ working on that project after closing the last buffer.")
:documentation #'lsp-describe-thing-at-point :documentation #'lsp-describe-thing-at-point
:definition #'lsp-find-definition :definition #'lsp-find-definition
:implementations #'lsp-find-implementation :implementations #'lsp-find-implementation
:type-definition #'lsp-find-type-definition
:references #'lsp-find-references) :references #'lsp-find-references)
;; TODO Lazy load these. They don't need to be loaded all at once unless the ;; TODO Lazy load these. They don't need to be loaded all at once unless the