-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelm-rg.el
169 lines (142 loc) · 5.06 KB
/
helm-rg.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
;;; helm-rg.el --- Helm frontend for rg (ripgrep)
;; Copyright (C) 2016 Sangho Na <[email protected]>
;;
;; Author: Sangho Na <[email protected]>
;; Version: 0.0.1
;; Keywords: rg ripgrep search
;; Homepage: https://github.com/microamp/helm-rg
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Helm frontend for rg (ripgrep).
;;; Code:
(require 'helm)
(require 's)
(defcustom helm-rg-executable "rg"
"Default executable for rg (ripgrep)."
:type 'string
:group 'helm-rg)
(defcustom helm-rg-requires-pattern 3
"Minimum pattern length. Default is 3."
:type 'integer
:group 'helm-rg)
(defcustom helm-rg-ignore-case nil
"Case insensitive search."
:type 'boolean
:group 'helm-rg)
(defcustom helm-rg-hidden nil
"Search hidden directories and files. (Hidden directories and files are
skipped by default.)"
:type 'boolean
:group 'helm-rg)
(defcustom helm-rg-smart-case nil
"Search case insensitively if the pattern is all lowercase. Search case
sensitively otherwise."
:type 'boolean
:group 'helm-rg)
(defvar helm-rg-process-name "helm-rg")
(defvar helm-rg-path nil)
(defvar helm-rg-source
(helm-build-async-source "rg (ripgrep)"
:candidates-process 'helm-rg--candidates-process
:filter-one-by-one 'helm-rg--filter-one-by-one
:action 'helm-rg--action
:candidate-number-limit 9999
:requires-pattern helm-rg-requires-pattern))
(defun helm-rg-command ()
(-filter 'identity
(list helm-rg-executable
"--no-heading"
(when helm-rg-ignore-case "--ignore-case")
(when helm-rg-hidden "--hidden")
(when helm-rg-smart-case "--smart-case")
"-n"
helm-pattern
helm-rg-path)))
(defun helm-rg--candidates-process ()
(let* ((cmd (helm-rg-command))
(proc (apply 'start-file-process
helm-rg-process-name
helm-buffer
cmd)))
(prog1 proc
(set-process-sentinel
(get-buffer-process helm-buffer)
#'(lambda (process event)
(helm-process-deferred-sentinel-hook
process
event
helm-rg-path))))))
(defun helm-rg--remove-drive-letter (candidate)
"Remove the drive letter from CANDIDATE if we're on windows."
(if (eq system-type 'windows-nt)
(let ((match-pos (string-match "^[a-zA-Z]:" candidate)))
(if (eq match-pos 0)
(substring candidate 2)
candidate))
candidate))
(defun helm-rg--filter-one-by-one (candidate)
(let* ((without-letter (helm-rg--remove-drive-letter candidate))
(split (s-split-up-to ":" without-letter 2))
(filename (nth 0 split))
(filename-short (s-chop-prefix helm-rg-path filename))
(line-number (nth 1 split))
(value (nth 2 split))
(highlighted (helm-grep-highlight-match value t)))
(cons (concat (propertize filename-short 'face 'helm-grep-file)
":"
(propertize line-number 'face 'helm-grep-lineno)
":"
(or highlighted value))
candidate)))
(defun helm-rg--action (candidate)
(let* ((without-letter (helm-rg--remove-drive-letter candidate))
(split (s-split-up-to ":" without-letter 2))
(filename (nth 0 split))
(line-number (string-to-number (nth 1 split))))
(progn
(find-file filename)
(helm-goto-line line-number)
(ignore-errors
(re-search-forward helm-pattern (line-end-position) t)
(goto-char (match-beginning 0))))))
(defun get-symbol-at-point ()
"Get the name of the symbol at point, or an empty string if there is no such
symbol."
(let ((symbol (symbol-at-point)))
(if symbol (symbol-name symbol) "")))
;;;###autoload
(defun helm-rg (&optional pattern basedir)
(interactive)
(unless (executable-find helm-rg-executable)
(error "rg not installed"))
(progn
(setq helm-rg-path (or basedir default-directory))
(helm
:buffer "*helm rg*"
:input (or pattern "")
:sources '(helm-rg-source))))
;;;###autoload
(defun helm-rg-at-point ()
(interactive)
(helm-rg (get-symbol-at-point)))
;;;###autoload
(defun helm-projectile-rg (&optional pattern)
(interactive)
(progn
(require 'projectile)
(let ((project-root (projectile-project-root)))
(helm-rg (or pattern "") project-root))))
;;;###autoload
(defun helm-projectile-rg-at-point ()
(interactive)
(helm-projectile-rg (get-symbol-at-point)))
(provide 'helm-rg)