forked from minad/consult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consult-xref.el
120 lines (104 loc) · 4.55 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
;;; consult-xref.el --- Xref integration for Consult -*- lexical-binding: t -*-
;; Copyright (C) 2021-2024 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Provides Xref integration for Consult. This is an extra package, to
;; allow lazy loading of xref.el. The `consult-xref' function is
;; autoloaded.
;;; Code:
(require 'consult)
(require 'xref)
(eval-when-compile (require 'subr-x))
(defvar consult-xref--history nil)
(defvar consult-xref--fetcher nil
"The current xref fetcher.
The fetch is stored globally such that it can be accessed by
Embark for `embark-export'.")
(defun consult-xref--candidates ()
"Return xref candidate list."
(let ((root (consult--project-root)))
(mapcar (lambda (xref)
(let* ((loc (xref-item-location xref))
(group (xref-location-group loc))
(group (if root (string-remove-prefix root group) group))
(cand (consult--format-file-line-match
group
(or (xref-location-line loc) 0)
(xref-item-summary xref))))
(add-text-properties
0 1 `(consult-xref ,xref consult--prefix-group ,group) cand)
cand))
(funcall consult-xref--fetcher))))
(defun consult-xref--preview (display)
"Xref preview with DISPLAY function."
(let ((open (consult--temporary-files))
(preview (consult--jump-preview)))
(lambda (action cand)
(unless cand
(funcall open))
(let ((consult--buffer-display display))
(funcall preview action
(when-let (loc (and cand (eq action 'preview)
(xref-item-location cand)))
(let ((type (type-of loc)))
;; Only preview file and buffer markers
(pcase type
('xref-buffer-location
(xref-location-marker loc))
((or 'xref-file-location 'xref-etags-location)
(consult--marker-from-line-column
(funcall open
;; xref-location-group returns the file name
(let ((xref-file-name-display 'abs))
(xref-location-group loc)))
(xref-location-line loc)
(if (eq type 'xref-file-location)
(xref-file-location-column loc)
0)))))))))))
;;;###autoload
(defun consult-xref (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."
(let* ((consult-xref--fetcher fetcher)
(candidates (consult-xref--candidates))
(display (alist-get 'display-action alist)))
(unless candidates
(user-error "No xref locations"))
(xref-pop-to-location
(if (cdr candidates)
(apply
#'consult--read
candidates
(append
(consult--customize-get #'consult-xref)
(list
:prompt "Go to xref: "
:history 'consult-xref--history
:require-match t
:sort nil
:category 'consult-xref
:group #'consult--prefix-group
:state
;; do not preview other frame
(when-let (fun (pcase-exhaustive display
('frame nil)
('window #'switch-to-buffer-other-window)
('nil #'switch-to-buffer)))
(consult-xref--preview fun))
:lookup (apply-partially #'consult--lookup-prop 'consult-xref))))
(get-text-property 0 'consult-xref (car candidates)))
display)))
(provide 'consult-xref)
;;; consult-xref.el ends here