-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
consult-xref.el
53 lines (47 loc) · 2.05 KB
/
consult-xref.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
;; -*- lexical-binding: t -*-
(defvar consult-preview-xref t)
(defun consult--xref-candidates (xrefs)
"Return candidate list from XREFS."
(let* ((candidates
(mapcar (lambda (xref)
(let ((loc (xref-item-location xref))
(xref-file-name-display 'nondirectory))
(list (xref-location-group loc)
(xref-location-line loc)
(xref-item-summary xref)
xref)))
xrefs))
(max-name (apply #'max (mapcar (lambda (x) (length (car x))) candidates)))
(max-line (apply #'max (mapcar (lambda (x) (cadr x)) candidates)))
(fmt (format "%%%ds:%%-%dd" max-name (length (number-to-string max-line)))))
(mapcar (pcase-lambda (`(,name ,line ,str ,xref))
(cons (concat (propertize (format fmt name line) 'face 'consult-location)
" " str)
xref))
candidates)))
(defun consult--xref (prompt xrefs &optional display)
"Select from XREFS and jump.
PROMPT is the `completing-read' prompt.
DISPLAY is the display action according to `xref-pop-to-location'."
(xref-pop-to-location
(consult--read
prompt
(consult--xref-candidates xrefs)
:preview (when consult-preview-xref
(let ((preview (consult--preview-position)))
(lambda (cand restore)
(cond
(restore (funcall preview cand t))
(cand (funcall preview (xref-location-marker (xref-item-location cand)) nil))))))
:require-match t
:sort nil
:lookup #'consult--lookup-cdr)
display))
(defun consult-show-xrefs (fetcher &optional alist)
"Show xrefs with preview in the minibuffer.
This function can be used for `xref-show-xrefs-function'.
See `xref-show-xrefs-function' for the description of the
FETCHER and ALIST arguments."
(consult--xref "Go to xref: "
(funcall fetcher)
(cdr (assoc 'display-action alist))))