Replace :xref-backend setting with :jump
Which takes :definitions, :references, :documentation and :xref-backend. Each accepts a function that will jump to definitions, references, and documentation respectively. If a major-mode has an :xref-backend, you don't need :definitions or :references.
This commit is contained in:
parent
928812da8a
commit
bc3aee2a4f
6 changed files with 102 additions and 56 deletions
|
@ -3,25 +3,39 @@
|
||||||
(defvar +jump--rg-installed-p (executable-find "rg"))
|
(defvar +jump--rg-installed-p (executable-find "rg"))
|
||||||
(defvar +jump--ag-installed-p (executable-find "ag"))
|
(defvar +jump--ag-installed-p (executable-find "ag"))
|
||||||
|
|
||||||
|
(defun +jump-to (prop identifier &optional other-window)
|
||||||
|
(with-selected-window
|
||||||
|
(if other-window
|
||||||
|
(save-excursion (other-window) (selected-window))
|
||||||
|
(selected-window))
|
||||||
|
(let ((fn (plist-get +jump-current-functions prop)))
|
||||||
|
(if (commandp fn)
|
||||||
|
(call-interactively fn)
|
||||||
|
(funcall fn identifier)))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +jump/definition (&optional other-window)
|
(defun +jump/definition (identifier &optional other-window)
|
||||||
"Jump to the definition of the symbol at point.
|
"Jump to the definition of the symbol at point.
|
||||||
|
|
||||||
Tries xref and falls back to `dumb-jump', then rg/ag, then
|
Tries xref and falls back to `dumb-jump', then rg/ag, then
|
||||||
`evil-goto-definition' (if evil is active)."
|
`evil-goto-definition' (if evil is active)."
|
||||||
(interactive "P")
|
(interactive
|
||||||
(let ((sym (thing-at-point 'symbol t))
|
(list (thing-at-point 'symbol t)
|
||||||
successful)
|
current-prefix-arg))
|
||||||
(cond ((null sym)
|
(cond ((null identifier)
|
||||||
(user-error "Nothing under point"))
|
(user-error "Nothing under point"))
|
||||||
|
|
||||||
((ignore-errors (if other-window
|
((plist-member +jump-current-functions :definition)
|
||||||
(xref-find-definitions-other-window sym)
|
(+jump-to :definition identifier))
|
||||||
(xref-find-definitions sym))
|
|
||||||
t))
|
|
||||||
|
|
||||||
((and (fboundp 'dumb-jump-go)
|
((ignore-errors (if other-window
|
||||||
;; dumb-jump doesn't tell us if it succeeded or not
|
(xref-find-definitions-other-window identifier)
|
||||||
|
(xref-find-definitions identifier))
|
||||||
|
t))
|
||||||
|
|
||||||
|
((and (fboundp 'dumb-jump-go)
|
||||||
|
;; dumb-jump doesn't tell us if it succeeded or not
|
||||||
|
(let (successful)
|
||||||
(cl-letf (((symbol-function 'dumb-jump-result-follow)
|
(cl-letf (((symbol-function 'dumb-jump-result-follow)
|
||||||
`(lambda (result &optional use-tooltip proj)
|
`(lambda (result &optional use-tooltip proj)
|
||||||
(setq successful t)
|
(setq successful t)
|
||||||
|
@ -30,48 +44,59 @@ Tries xref and falls back to `dumb-jump', then rg/ag, then
|
||||||
(if other-window
|
(if other-window
|
||||||
(dumb-jump-go-other-window)
|
(dumb-jump-go-other-window)
|
||||||
(dumb-jump-go))
|
(dumb-jump-go))
|
||||||
successful)))
|
successful))))
|
||||||
|
|
||||||
((and sym
|
((and identifier
|
||||||
(featurep 'counsel)
|
(featurep 'counsel)
|
||||||
(let ((regex (rxt-quote-pcre sym)))
|
(let ((regex (rxt-quote-pcre identifier)))
|
||||||
(or (and +jump--rg-installed-p
|
(or (and +jump--rg-installed-p
|
||||||
(counsel-rg regex (doom-project-root)))
|
(counsel-rg regex (doom-project-root)))
|
||||||
(and +jump--ag-installed-p
|
(and +jump--ag-installed-p
|
||||||
(counsel-ag regex (doom-project-root)))))))
|
(counsel-ag regex (doom-project-root)))))))
|
||||||
|
|
||||||
((and (featurep 'evil)
|
((and (featurep 'evil)
|
||||||
evil-mode
|
evil-mode
|
||||||
(destructuring-bind (beg end) (bounds-of-thing-at-point 'symbol)
|
(destructuring-bind (beg end)
|
||||||
(evil-goto-definition)
|
(bounds-of-thing-at-point 'symbol)
|
||||||
(let ((pt (point)))
|
(evil-goto-definition)
|
||||||
(not (and (>= pt beg)
|
(let ((pt (point)))
|
||||||
(< pt end)))))))
|
(not (and (>= pt beg)
|
||||||
|
(< pt end)))))))
|
||||||
|
|
||||||
(t (user-error "Couldn't find '%s'" sym)))))
|
(t (user-error "Couldn't find '%s'" identifier))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +jump/references ()
|
(defun +jump/references (identifier)
|
||||||
"Show a list of references to the symbol at point.
|
"Show a list of references to the symbol at point.
|
||||||
|
|
||||||
Tries `xref-find-references' and falls back to rg/ag."
|
Tries `xref-find-references' and falls back to rg/ag."
|
||||||
(interactive)
|
(interactive (list (thing-at-point 'symbol t)))
|
||||||
(let ((sym (thing-at-point 'symbol t)))
|
(cond ((plist-member +jump-current-functions :references)
|
||||||
(cond ((ignore-errors (xref-find-references sym)
|
(+jump-to :references identifier))
|
||||||
t))
|
|
||||||
|
|
||||||
((and sym
|
((ignore-errors (xref-find-references identifier)
|
||||||
(featurep 'counsel)
|
t))
|
||||||
(let ((regex (rxt-quote-pcre sym)))
|
|
||||||
(or (and (executable-find "rg")
|
|
||||||
(counsel-rg regex (doom-project-root)))
|
|
||||||
(and (executable-find "ag")
|
|
||||||
(counsel-ag regex (doom-project-root)))))))
|
|
||||||
|
|
||||||
(t (error "Couldn't find '%s'" sym)))))
|
((and identifier
|
||||||
|
(featurep 'counsel)
|
||||||
|
(let ((regex (rxt-quote-pcre identifier)))
|
||||||
|
(or (and (executable-find "rg")
|
||||||
|
(counsel-rg regex (doom-project-root)))
|
||||||
|
(and (executable-find "ag")
|
||||||
|
(counsel-ag regex (doom-project-root)))))))
|
||||||
|
|
||||||
|
(t (error "Couldn't find '%s'" identifier))))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun +jump/documentation (identifier)
|
||||||
|
"Show documentation for the symbol at point, if available."
|
||||||
|
(interactive (list (thing-at-point 'symbol t)))
|
||||||
|
(cond ((plist-member +jump-current-functions :documentation)
|
||||||
|
(+jump-to :documentation identifier))
|
||||||
|
(t
|
||||||
|
(+jump/online (caar +jump-search-url-alist) identifier))))
|
||||||
|
|
||||||
(defvar +jump--online-last nil)
|
(defvar +jump--online-last nil)
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +jump/online (where search)
|
(defun +jump/online (where search)
|
||||||
"Looks up SEARCH online, in you browser, as dictated by WHERE.
|
"Looks up SEARCH online, in you browser, as dictated by WHERE.
|
||||||
|
|
|
@ -22,29 +22,46 @@
|
||||||
"An alist that maps online resources to their search url or a function that
|
"An alist that maps online resources to their search url or a function that
|
||||||
produces an url. Used by `+jump/online'.")
|
produces an url. Used by `+jump/online'.")
|
||||||
|
|
||||||
(def-setting! :xref-backend (mode function)
|
(defvar +jump-function-alist nil
|
||||||
"TODO"
|
"TODO")
|
||||||
`(add-hook! ,mode
|
|
||||||
(add-hook 'xref-backend-functions #',function nil t)))
|
|
||||||
|
|
||||||
(set! :popup "*xref*" :noselect t :autokill t :autoclose t)
|
(defvar-local +jump-current-functions nil
|
||||||
|
"TODO")
|
||||||
|
|
||||||
|
(def-setting! :jump (modes &rest plist)
|
||||||
|
"TODO"
|
||||||
|
`(dolist (mode (doom-enlist ,modes))
|
||||||
|
(push (cons mode (list ,@plist)) +jump-function-alist)))
|
||||||
|
|
||||||
;; Let me control what backends to fall back on
|
;; Let me control what backends to fall back on
|
||||||
(setq-default xref-backend-functions '(t))
|
(setq-default xref-backend-functions '(t))
|
||||||
|
|
||||||
|
(set! :popup "*xref*" :noselect t :autokill t :autoclose t)
|
||||||
|
|
||||||
;; Recenter after certain jumps
|
;; Recenter after certain jumps
|
||||||
(add-hook!
|
(add-hook!
|
||||||
'(imenu-after-jump-hook evil-jumps-post-jump-hook
|
'(imenu-after-jump-hook evil-jumps-post-jump-hook
|
||||||
counsel-grep-post-action-hook dumb-jump-after-jump-hook)
|
counsel-grep-post-action-hook dumb-jump-after-jump-hook)
|
||||||
#'recenter)
|
#'recenter)
|
||||||
|
|
||||||
|
(defun +jump|init ()
|
||||||
|
"Initialize `+jump-current-functions', used by `+jump/definition',
|
||||||
|
`+jump/references' and `+jump/documentation'."
|
||||||
|
(when-let (plist (cdr (assq major-mode +jump-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 +jump-current-functions plist)))
|
||||||
|
(add-hook 'after-change-major-mode-hook #'+jump|init)
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Packages
|
;; Packages
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(def-package! dumb-jump
|
(def-package! dumb-jump
|
||||||
:commands (dumb-jump-go dumb-jump-quick-look dumb-jump-back)
|
:commands (dumb-jump-go dumb-jump-quick-look
|
||||||
|
dumb-jump-back dumb-jump-result-follow)
|
||||||
:config
|
:config
|
||||||
(setq dumb-jump-default-project doom-emacs-dir
|
(setq dumb-jump-default-project doom-emacs-dir
|
||||||
dumb-jump-aggressive nil
|
dumb-jump-aggressive nil
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
:config
|
:config
|
||||||
(set! :repl 'emacs-lisp-mode #'+emacs-lisp/repl)
|
(set! :repl 'emacs-lisp-mode #'+emacs-lisp/repl)
|
||||||
(set! :eval 'emacs-lisp-mode #'+emacs-lisp-eval)
|
(set! :eval 'emacs-lisp-mode #'+emacs-lisp-eval)
|
||||||
|
(set! :jump 'emacs-lisp-mode :documentation #'describe-symbol)
|
||||||
(set! :rotate 'emacs-lisp-mode
|
(set! :rotate 'emacs-lisp-mode
|
||||||
:symbols '(("t" "nil")
|
:symbols '(("t" "nil")
|
||||||
("let" "let*")
|
("let" "let*")
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
(add-hook! 'js2-mode-hook
|
(add-hook! 'js2-mode-hook
|
||||||
#'(flycheck-mode highlight-indentation-mode rainbow-delimiters-mode))
|
#'(flycheck-mode highlight-indentation-mode rainbow-delimiters-mode))
|
||||||
|
|
||||||
|
(set! :repl 'js2-mode '+javascript/repl)
|
||||||
|
(set! :electric 'js2-mode :chars '(?\} ?\) ?.))
|
||||||
|
(set! :jump 'js2-mode :xref-backend #'xref-js2-xref-backend)
|
||||||
|
|
||||||
;; Conform switch-case indentation to editorconfig's config
|
;; Conform switch-case indentation to editorconfig's config
|
||||||
(set! :editorconfig :add '(js2-mode js2-basic-offset js-switch-indent-offset))
|
(set! :editorconfig :add '(js2-mode js2-basic-offset js-switch-indent-offset))
|
||||||
|
|
||||||
|
@ -23,10 +27,6 @@
|
||||||
(setq-local flycheck-javascript-eslint-executable eslint))))
|
(setq-local flycheck-javascript-eslint-executable eslint))))
|
||||||
(add-hook 'flycheck-mode-hook #'+javascript|init-flycheck-elint)
|
(add-hook 'flycheck-mode-hook #'+javascript|init-flycheck-elint)
|
||||||
|
|
||||||
(set! :repl 'js2-mode '+javascript/repl)
|
|
||||||
(set! :electric 'js2-mode :chars '(?\} ?\) ?.))
|
|
||||||
(set! :xref-backend 'js2-mode 'xref-js2-xref-backend)
|
|
||||||
|
|
||||||
(sp-with-modes '(js2-mode rjsx-mode)
|
(sp-with-modes '(js2-mode rjsx-mode)
|
||||||
(sp-local-pair "/* " " */" :post-handlers '(("| " "SPC"))))
|
(sp-local-pair "/* " " */" :post-handlers '(("| " "SPC"))))
|
||||||
|
|
||||||
|
|
|
@ -26,10 +26,7 @@
|
||||||
(flycheck-mode +1)
|
(flycheck-mode +1)
|
||||||
(eldoc-mode +1)))
|
(eldoc-mode +1)))
|
||||||
(add-hook! (typescript-mode web-mode) #'+typescript|tide-setup)
|
(add-hook! (typescript-mode web-mode) #'+typescript|tide-setup)
|
||||||
|
(map! :localleader
|
||||||
(map! :map typescript-mode-map
|
|
||||||
:m "gd" #'tide-jump-to-definition
|
|
||||||
:localleader
|
|
||||||
:m "fh" #'tide-documentation-at-point))
|
:m "fh" #'tide-documentation-at-point))
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,6 +34,11 @@
|
||||||
:after typescript-mode
|
:after typescript-mode
|
||||||
:config
|
:config
|
||||||
(set! :company-backend 'typescript-mode '(company-tide))
|
(set! :company-backend 'typescript-mode '(company-tide))
|
||||||
|
(set! :jump 'typescript-mode
|
||||||
|
:definition #'tide-jump-to-definition
|
||||||
|
:references #'tide-references
|
||||||
|
:documentation #'tide-documentation-at-point)
|
||||||
|
|
||||||
(setq tide-format-options
|
(setq tide-format-options
|
||||||
'(:insertSpaceAfterFunctionKeywordForAnonymousFunctions t
|
'(:insertSpaceAfterFunctionKeywordForAnonymousFunctions t
|
||||||
:placeOpenBraceOnNewLineForFunctions nil))
|
:placeOpenBraceOnNewLineForFunctions nil))
|
||||||
|
|
|
@ -292,6 +292,7 @@
|
||||||
:m "gT" #'+workspace/switch-left
|
:m "gT" #'+workspace/switch-left
|
||||||
:m "gd" #'+jump/definition
|
:m "gd" #'+jump/definition
|
||||||
:m "gD" #'+jump/references
|
:m "gD" #'+jump/references
|
||||||
|
:m "gh" #'+jump/documentation
|
||||||
:n "gp" #'+evil/reselect-paste
|
:n "gp" #'+evil/reselect-paste
|
||||||
:n "gr" #'+eval:region
|
:n "gr" #'+eval:region
|
||||||
:n "gR" #'+eval/buffer
|
:n "gR" #'+eval/buffer
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue