forked from brotzeit/helm-xref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-xref.el
128 lines (107 loc) · 4.25 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
;;; 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: 0.3
;; 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-buffer-size)))
"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-long)
function)
:group 'helm-xref)
(defun helm-xref-candidates (xrefs)
"Convert XREF-ALIST items to helm candidates and add them to `helm-xref-alist'."
(dolist (xref xrefs)
(with-slots (summary location) xref
(let* ((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-long (file line summary)
"Build long form of candidate format with FILE, LINE, and SUMMARY."
(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 (xref-item func)
"Set buffer and point according to xref-item XREF-ITEM.
Use FUNC to display buffer."
(with-slots (summary location) xref-item
(let* ((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 (lambda ()
helm-xref-alist)
:persistent-action (lambda (xref-item)
(helm-xref-goto-xref-item
xref-item '(lambda (buf) (helm-highlight-current-line))))
:action '(("Switch to buffer" . (lambda (xref-item) (helm-xref-goto-xref-item xref-item 'switch-to-buffer)))
("Other window" . (lambda (xref-item) (helm-xref-goto-xref-item xref-item 'switch-to-buffer-other-window))))
:candidate-number-limit 9999))
(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 xrefs)
(helm :sources (helm-xref-source)
:truncate-lines t
:buffer "*helm-xref*"))
(provide 'helm-xref)
;;; helm-xref.el ends here