-
Notifications
You must be signed in to change notification settings - Fork 15
/
helm-xref.el
200 lines (172 loc) · 6.56 KB
/
helm-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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
;;; helm-xref.el --- Helm interface for xref results -*- lexical-binding: t -*-
;; Copyright (C) 2017 Fritz Stelzer <[email protected]>
;; Author: Fritz Stelzer <[email protected]>
;; URL: https://github.com/brotzeit/helm-xref
;; Version: 1.0
;; Package-Requires: ((emacs "25.1") (helm "1.9.4"))
;;; License:
;;
;; 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.
;;; Contributors:
;; Sanjeev Sivasankaran <kalasalatemp at gmail.com> in 2019 changed font-face.
;;; Code:
(require 'helm)
(require 'helm-utils)
(require 'xref)
(require 'cl-seq)
(defvar helm-xref-alist nil
"Holds helm candidates.")
(defgroup helm-xref nil
"Xref with helm."
:prefix "helm-xref-" :group 'helm)
(defface helm-xref-file-name
'((t (:inherit 'font-lock-builtin-face)))
"Face for xref file name"
:group 'helm-xref)
(defface helm-xref-line-number
'((t (:inherit 'helm-grep-lineno)))
"Face for xref line number"
:group 'helm-xref)
(defcustom helm-xref-candidate-formatting-function 'helm-xref-format-candidate-short
"Select the function for candidate formatting."
:type '(radio
(function-item helm-xref-format-candidate-short)
(function-item helm-xref-format-candidate-full-path)
(function-item helm-xref-format-candidate-long)
function)
:group 'helm-xref)
(defcustom helm-xref-input ""
"Initial input in Helm."
:type 'string
:group 'helm-xref)
(defun helm-xref-candidates-26 (xrefs)
"Convert XREF-ALIST items to helm candidates and add them to `helm-xref-alist'."
(dolist (xref xrefs)
(let* ((summary (xref-item-summary xref))
(location (xref-item-location xref))
(line (xref-location-line location))
(file (xref-location-group location))
candidate)
(setq candidate
(funcall helm-xref-candidate-formatting-function file line summary))
(push (cons candidate xref) helm-xref-alist)))
(setq helm-xref-alist (reverse helm-xref-alist)))
(defun helm-xref-candidates-27 (fetcher alist)
"Convert XREF-ALIST items to helm candidates and add them to `helm-xref-alist'."
(cl-assert (functionp fetcher))
(let* ((xrefs
(or
(assoc-default 'fetched-xrefs alist)
(funcall fetcher))))
(dolist (xref xrefs)
(let* ((summary (xref-item-summary xref))
(location (xref-item-location xref))
(line (xref-location-line location))
(file (xref-location-group location))
candidate)
(setq candidate
(funcall helm-xref-candidate-formatting-function file line summary))
(push (cons candidate xref) helm-xref-alist))))
(setq helm-xref-alist (reverse helm-xref-alist)))
(defun helm-xref-format-candidate-short (file line summary)
"Build short form of candidate format with FILE, LINE, and SUMMARY."
(concat
(propertize (car (reverse (split-string file "\\/")))
'font-lock-face 'helm-xref-file-name)
(when (string= "integer" (type-of line))
(concat
":"
(propertize (int-to-string line)
'font-lock-face 'helm-xref-line-number)))
":"
summary))
(defun helm-xref-format-candidate-full-path (file line summary)
"Same as `helm-xref-format-candidate-short', but display entire path."
(concat
(propertize file 'font-lock-face 'helm-xref-file-name)
(when (string= "integer" (type-of line))
(concat
":"
(propertize (int-to-string line)
'font-lock-face 'helm-xref-line-number)))
":"
summary))
(defun helm-xref-format-candidate-long (file line summary)
"Use two lines for each candidate. One contains the path and the other the actual candidate."
(concat
(propertize file 'font-lock-face 'helm-xref-file-name)
(when (string= "integer" (type-of line))
(concat
"\n:"
(propertize (int-to-string line)
'font-lock-face 'helm-xref-line-number)))
":"
summary))
(defun helm-xref-goto-xref-item (item func)
"Set buffer and point according to xref-item ITEM.
Use FUNC to display buffer."
(let* ((summary (xref-item-summary item))
(location (xref-item-location item))
(marker (xref-location-marker location))
(buf (marker-buffer marker))
(offset (marker-position marker)))
(switch-to-buffer buf)
(goto-char offset)
(funcall func buf)))
(defun helm-xref-source ()
"Return a `helm' source for xref results."
(helm-build-sync-source "Helm Xref"
:candidates helm-xref-alist
:persistent-action (lambda (item)
(helm-xref-goto-xref-item
item '(lambda (buf) (helm-highlight-current-line))))
:action '(("Switch to buffer" . (lambda (item) (helm-xref-goto-xref-item item 'switch-to-buffer)))
("Other window" . (lambda (item) (helm-xref-goto-xref-item item 'switch-to-buffer-other-window))))
:candidate-number-limit 9999))
;;;###autoload
(defun helm-xref-show-xrefs (xrefs _alist)
"Function to display XREFS.
Needs to be set the value of `xref-show-xrefs-function'."
(setq helm-xref-alist nil)
(helm-xref-candidates-26 xrefs)
(helm :sources (helm-xref-source)
:truncate-lines t
:input helm-xref-input
:buffer "*helm-xref*"))
;;;###autoload
(defun helm-xref-show-xrefs-27 (fetcher alist)
"Function to display XREFS.
Needs to be set the value of `xref-show-xrefs-function'."
(setq helm-xref-alist nil)
(helm-xref-candidates-27 fetcher alist)
(helm :sources (helm-xref-source)
:truncate-lines t
:buffer "*helm-xref*"))
;;;###autoload
(defun helm-xref-show-defs-27 (fetcher alist)
"Function to display list of definitions."
(let ((xrefs (funcall fetcher)))
(cond
((not (cdr xrefs))
(xref-pop-to-location (car xrefs)
(assoc-default 'display-action alist)))
(t
(helm-xref-show-xrefs-27 fetcher
(cons (cons 'fetched-xrefs xrefs)
alist))))))
;;;###autoload
(progn
(if (< emacs-major-version 27)
(setq xref-show-xrefs-function 'helm-xref-show-xrefs)
(progn
(setq xref-show-xrefs-function 'helm-xref-show-xrefs-27)
(setq xref-show-definitions-function 'helm-xref-show-defs-27))))
(provide 'helm-xref)
;;; helm-xref.el ends here