Better ag/rg fallbacks for +jump/definition

This commit is contained in:
Henrik Lissner 2017-05-21 12:16:25 +02:00
parent ac1ef3fdee
commit 3b56f41c20

View file

@ -1,38 +1,71 @@
;;; feature/jump/autoload.el ;;; feature/jump/autoload.el
(defvar +jump--rg-installed-p (executable-find "rg"))
(defvar +jump--ag-installed-p (executable-find "ag"))
;;;###autoload ;;;###autoload
(defun +jump/definition (&optional other-window) (defun +jump/definition (&optional other-window)
"Find definition, falling back to dumb-jump, then "Find definition of the symbol at point.
`evil-goto-definition' otherwise."
(interactive "p") Tries xref and falls back to `dumb-jump', then rg/ag."
(let ((orig-pt (point)) (interactive "P")
(orig-file (buffer-file-name)) (let ((sym (thing-at-point 'symbol t))
(sym (thing-at-point 'symbol t))) successful)
(cond ((ignore-errors (xref-find-definitions sym) (cond ((null sym)
(user-error "Nothing under point"))
((ignore-errors (if other-window
(xref-find-definitions-other-window sym)
(xref-find-definitions sym))
t)) t))
((and (fboundp 'dumb-jump-go) ((and (fboundp 'dumb-jump-go)
(progn (dumb-jump-go) ;; dumb-jump doesn't tell us if it succeeded or not
(and (= orig-pt (point)) (cl-letf (((symbol-function 'dumb-jump-result-follow)
(equal (file-truename orig-file) `(lambda (result &optional use-tooltip proj)
(file-truename buffer-file-name)))))) (setq successful t)
(,(symbol-function 'dumb-jump-result-follow)
result use-tooltip proj))))
(if other-window
(dumb-jump-go-other-window)
(dumb-jump-go))
successful)))
((fboundp 'counsel-ag) ((and sym
(counsel-ag sym (doom-project-root))) (featurep 'counsel)
(let ((regex (rxt-quote-pcre sym)))
(or (and +jump--rg-installed-p
(counsel-rg regex (doom-project-root)))
(and +jump--ag-installed-p
(counsel-ag regex (doom-project-root)))))))
(t (error "Couldn't find '%s'" sym))))) ((and (featurep 'evil)
evil-mode
(let ((bounds (bounds-of-thing-at-point 'symbol))
(orig-pt (point)))
(evil-goto-definition)
(let ((pt (point)))
(not (and (>= pt (car bounds))
(< pt (cdr bounds))))))))
(t (user-error "Couldn't find '%s'" sym)))))
;;;###autoload ;;;###autoload
(defun +jump/references (&optional other-window) (defun +jump/references ()
"TODO" "TODO"
(interactive "p") (interactive)
(let ((sym (thing-at-point 'symbol t))) (let ((sym (thing-at-point 'symbol t)))
(cond ((progn (cond ((progn
(ignore-errors (xref-find-references sym) (ignore-errors (xref-find-references sym)
t))) t)))
((fboundp 'counsel-ag) ((and sym
(counsel-ag sym (doom-project-root))) (featurep 'counsel)
(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))))) (t (error "Couldn't find '%s'" sym)))))