consult-xref.el (4825B)
1 ;;; consult-xref.el --- Xref integration for Consult -*- lexical-binding: t -*- 2 3 ;; Copyright (C) 2021-2024 Free Software Foundation, Inc. 4 5 ;; This file is part of GNU Emacs. 6 7 ;; This program is free software: you can redistribute it and/or modify 8 ;; it under the terms of the GNU General Public License as published by 9 ;; the Free Software Foundation, either version 3 of the License, or 10 ;; (at your option) any later version. 11 12 ;; This program is distributed in the hope that it will be useful, 13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 ;; GNU General Public License for more details. 16 17 ;; You should have received a copy of the GNU General Public License 18 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 19 20 ;;; Commentary: 21 22 ;; Provides Xref integration for Consult. This is an extra package, to 23 ;; allow lazy loading of xref.el. The `consult-xref' function is 24 ;; autoloaded. 25 26 ;;; Code: 27 28 (require 'consult) 29 (require 'xref) 30 31 (defvar consult-xref--history nil) 32 33 (defvar consult-xref--fetcher nil 34 "The current xref fetcher. 35 The fetch is stored globally such that it can be accessed by 36 Embark for `embark-export'.") 37 38 (defun consult-xref--candidates () 39 "Return xref candidate list." 40 (let ((root (consult--project-root))) 41 (mapcar (lambda (xref) 42 (let* ((loc (xref-item-location xref)) 43 (group (if (fboundp 'xref--group-name-for-display) 44 ;; This function is available in xref 1.3.2 45 (xref--group-name-for-display 46 (xref-location-group loc) root) 47 (xref-location-group loc))) 48 (cand (consult--format-file-line-match 49 group 50 (or (xref-location-line loc) 0) 51 (xref-item-summary xref)))) 52 (add-text-properties 53 0 1 `(consult-xref ,xref consult--prefix-group ,group) cand) 54 cand)) 55 (funcall consult-xref--fetcher)))) 56 57 (defun consult-xref--preview (display) 58 "Xref preview with DISPLAY function." 59 (let ((open (consult--temporary-files)) 60 (preview (consult--jump-preview))) 61 (lambda (action cand) 62 (unless cand 63 (funcall open)) 64 (let ((consult--buffer-display display)) 65 (funcall preview action 66 (when-let (loc (and cand (eq action 'preview) 67 (xref-item-location cand))) 68 (let ((type (type-of loc))) 69 ;; Only preview file and buffer markers 70 (pcase type 71 ('xref-buffer-location 72 (xref-location-marker loc)) 73 ((or 'xref-file-location 'xref-etags-location) 74 (consult--marker-from-line-column 75 (funcall open 76 ;; xref-location-group returns the file name 77 (let ((xref-file-name-display 'abs)) 78 (xref-location-group loc))) 79 (xref-location-line loc) 80 (if (eq type 'xref-file-location) 81 (xref-file-location-column loc) 82 0))))))))))) 83 84 ;;;###autoload 85 (defun consult-xref (fetcher &optional alist) 86 "Show xrefs with preview in the minibuffer. 87 88 This function can be used for `xref-show-xrefs-function'. 89 See `xref-show-xrefs-function' for the description of the 90 FETCHER and ALIST arguments." 91 (let* ((consult-xref--fetcher fetcher) 92 (candidates (consult-xref--candidates)) 93 (display (alist-get 'display-action alist))) 94 (unless candidates 95 (user-error "No xref locations")) 96 (xref-pop-to-location 97 (if (cdr candidates) 98 (apply 99 #'consult--read 100 candidates 101 (append 102 (consult--customize-get #'consult-xref) 103 (list 104 :prompt "Go to xref: " 105 :history 'consult-xref--history 106 :require-match t 107 :sort nil 108 :category 'consult-xref 109 :group #'consult--prefix-group 110 :state 111 ;; do not preview other frame 112 (when-let (fun (pcase-exhaustive display 113 ('frame nil) 114 ('window #'switch-to-buffer-other-window) 115 ('nil #'switch-to-buffer))) 116 (consult-xref--preview fun)) 117 :lookup (apply-partially #'consult--lookup-prop 'consult-xref)))) 118 (get-text-property 0 'consult-xref (car candidates))) 119 display))) 120 121 (provide 'consult-xref) 122 ;;; consult-xref.el ends here