Skip to content

Commit b52029d

Browse files
authored
Add support for rg.
1 parent 7a73b54 commit b52029d

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

gptel-cpp-complete.el

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
;; Author: Huming Chen <chenhuming@gmail.com>
66
;; URL: https://github.com/beacoder/gptel-cpp-complete
7-
;; Version: 0.1.8
7+
;; Version: 0.1.9
88
;; Created: 2025-12-26
99
;; Keywords: programming, convenience
1010
;; Package-Requires: ((emacs "30.1") (eglot "1.19") (gptel "0.9.8"))
@@ -49,6 +49,7 @@
4949
;; 0.1.6 Remove duplicated texts from completion
5050
;; 0.1.7 Retrieve completion-symbols and fix ag search issue
5151
;; 0.1.8 Show completion only when location didn't change
52+
;; 0.1.9 Add rg support
5253

5354
;;; Code:
5455

@@ -84,6 +85,11 @@
8485
:type 'string
8586
:group 'gptel-cpp-complete)
8687

88+
(defcustom gptel-cpp-complete-rg-cmd "rg -t cpp -C 3 \"%s\" --no-heading --color never | head -n 30"
89+
"Rg command to search similar pattern."
90+
:type 'string
91+
:group 'gptel-cpp-complete)
92+
8793
;; ------------------------------------------------------------
8894
;; Helpers
8995
;; ------------------------------------------------------------
@@ -226,8 +232,8 @@
226232
(gptel-cpp-complete--safe-subseq (plist-get classified :vars) 0 1)
227233
(gptel-cpp-complete--safe-subseq (plist-get classified :members) 0 1)))
228234

229-
(defun gptel-cpp-complete--ag-pattern-for-symbol (symbol)
230-
"Format SYMBOL for searching with `ag'."
235+
(defun gptel-cpp-complete--grep-pattern-for-symbol (symbol)
236+
"Format SYMBOL for searching with `ag' or `rg'."
231237
(let* ((name (plist-get symbol :label))
232238
(kind (plist-get symbol :kind)))
233239
(cond
@@ -243,19 +249,22 @@
243249
(t
244250
(format "\\b%s\\b" name)))))
245251

246-
(defun gptel-cpp-complete--ag-search-pattern (pattern)
247-
"Search PATTERN using `ag'."
248-
(shell-command-to-string (format gptel-cpp-complete-ag-cmd pattern)))
252+
(defun gptel-cpp-complete--grep-search-pattern (pattern)
253+
"Search PATTERN using `ag' or `rg'."
254+
(let ((cmd (cond ((executable-find "rg") gptel-cpp-complete-rg-cmd)
255+
((executable-find "ag") gptel-cpp-complete-ag-cmd)
256+
(t (error "Neither rg nor ag found")))))
257+
(shell-command-to-string (format cmd pattern))))
249258

250-
(defun gptel-cpp-complete--ag-similar-patterns (s-k)
259+
(defun gptel-cpp-complete--grep-similar-patterns (s-k)
251260
"Search similar patterns based on S-K."
252261
(let* ((symbols s-k)
253262
(classified (gptel-cpp-complete--classify-symbols symbols))
254263
(targets (gptel-cpp-complete--select-search-symbols classified)))
255264
(string-join
256265
(cl-loop for sym in targets
257-
for pat = (gptel-cpp-complete--ag-pattern-for-symbol sym)
258-
collect (gptel-cpp-complete--ag-search-pattern pat))
266+
for pat = (gptel-cpp-complete--grep-pattern-for-symbol sym)
267+
collect (gptel-cpp-complete--grep-search-pattern pat))
259268
"\n\n")))
260269

261270
;; ------------------------------------------------------------
@@ -400,7 +409,7 @@ Callees of this function:
400409
(symbols+kind (or (gptel-cpp-complete--in-scope-symbols+kind) '()))
401410
(symbols (or (delete-dups (mapcar #'car symbols+kind)) '()))
402411
(s+k (or (mapcar #'cdr symbols+kind) '()))
403-
(patterns (or (gptel-cpp-complete--ag-similar-patterns s+k) "None found"))
412+
(patterns (or (gptel-cpp-complete--grep-similar-patterns s+k) "None found"))
404413
(calls (and gptel-cpp-complete-include-call-hierarchy
405414
(gptel-cpp-complete--call-hierarchy-context)))
406415
(incomming (or (car calls) "None found"))

0 commit comments

Comments
 (0)