config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

consult-xref.el (4569B)


      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 (eval-when-compile (require 'subr-x))
     31 
     32 (defvar consult-xref--history nil)
     33 
     34 (defvar consult-xref--fetcher nil
     35   "The current xref fetcher.
     36 The fetch is stored globally such that it can be accessed by
     37  Embark for `embark-export'.")
     38 
     39 (defun consult-xref--candidates ()
     40   "Return xref candidate list."
     41   (let ((root (consult--project-root)))
     42     (mapcar (lambda (xref)
     43               (let* ((loc (xref-item-location xref))
     44                      (group (xref-location-group loc))
     45                      (group (if root (string-remove-prefix root group) group))
     46                      (cand (consult--format-file-line-match
     47                             group
     48                             (or (xref-location-line loc) 0)
     49                             (xref-item-summary xref))))
     50                 (add-text-properties
     51                  0 1 `(consult-xref ,xref consult--prefix-group ,group) cand)
     52                 cand))
     53             (funcall consult-xref--fetcher))))
     54 
     55 (defun consult-xref--preview (display)
     56   "Xref preview with DISPLAY function."
     57   (let ((open (consult--temporary-files))
     58         (preview (consult--jump-preview)))
     59     (lambda (action cand)
     60       (unless cand
     61         (funcall open))
     62       (let ((consult--buffer-display display))
     63         (funcall preview action
     64                  (when-let (loc (and cand (eq action 'preview)
     65                                      (xref-item-location cand)))
     66                    (let ((type (type-of loc)))
     67                      ;; Only preview file and buffer markers
     68                      (pcase type
     69                        ('xref-buffer-location
     70                         (xref-location-marker loc))
     71                        ((or 'xref-file-location 'xref-etags-location)
     72                         (consult--marker-from-line-column
     73                          (funcall open
     74                                   ;; xref-location-group returns the file name
     75                                   (let ((xref-file-name-display 'abs))
     76                                     (xref-location-group loc)))
     77                          (xref-location-line loc)
     78                          (if (eq type 'xref-file-location)
     79                              (xref-file-location-column loc)
     80                            0)))))))))))
     81 
     82 ;;;###autoload
     83 (defun consult-xref (fetcher &optional alist)
     84   "Show xrefs with preview in the minibuffer.
     85 
     86 This function can be used for `xref-show-xrefs-function'.
     87 See `xref-show-xrefs-function' for the description of the
     88 FETCHER and ALIST arguments."
     89   (let* ((consult-xref--fetcher fetcher)
     90          (candidates (consult-xref--candidates))
     91          (display (alist-get 'display-action alist))
     92          (this-command #'consult-xref))
     93     (unless candidates
     94       (user-error "No xref locations"))
     95     (xref-pop-to-location
     96      (if (cdr candidates)
     97          (consult--read
     98           candidates
     99           :prompt "Go to xref: "
    100           :history 'consult-xref--history
    101           :require-match t
    102           :sort nil
    103           :category 'consult-xref
    104           :group #'consult--prefix-group
    105           :state
    106           ;; do not preview other frame
    107           (when-let (fun (pcase-exhaustive display
    108                            ('frame nil)
    109                            ('window #'switch-to-buffer-other-window)
    110                            ('nil #'switch-to-buffer)))
    111             (consult-xref--preview fun))
    112           :lookup (apply-partially #'consult--lookup-prop 'consult-xref))
    113        (get-text-property 0 'consult-xref (car candidates)))
    114      display)))
    115 
    116 (provide 'consult-xref)
    117 ;;; consult-xref.el ends here