aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-06-20 12:50:40 -0400
committerCraig Jennings <c@cjennings.net>2026-06-20 12:50:40 -0400
commit4f088c3930ee8ee2ee58796f0634d90b76b50526 (patch)
treea65d18f99a6e28e3e34e14d53dd43b897c486334
parentffb9a7de2f86ef9f1aab84b197ca5806fe657f0b (diff)
downloaddotemacs-4f088c3930ee8ee2ee58796f0634d90b76b50526.tar.gz
dotemacs-4f088c3930ee8ee2ee58796f0634d90b76b50526.zip
refactor(prog-general): dedup the deadgrep search tail, lift its helpers
cj/deadgrep-here and cj/deadgrep-in-dir repeated the same normalize-directory + read-term + invoke-deadgrep tail. Lift cj/deadgrep--initial-term out of :config and add cj/--deadgrep-run for the shared tail; each command resolves its root then delegates. Adds coverage for the term seeding and the run helper.
-rw-r--r--modules/prog-general.el34
-rw-r--r--tests/test-prog-general--deadgrep.el44
2 files changed, 65 insertions, 13 deletions
diff --git a/modules/prog-general.el b/modules/prog-general.el
index ae48bc9cb..968032831 100644
--- a/modules/prog-general.el
+++ b/modules/prog-general.el
@@ -232,6 +232,23 @@ If no such file exists there, display a message."
;; ---------------------------------- Ripgrep ----------------------------------
+(declare-function deadgrep "deadgrep")
+
+(defun cj/deadgrep--initial-term ()
+ "Return the region text or the symbol at point, to seed a Deadgrep search."
+ (cond
+ ((use-region-p)
+ (buffer-substring-no-properties (region-beginning) (region-end)))
+ (t (thing-at-point 'symbol t))))
+
+(defun cj/--deadgrep-run (root &optional term)
+ "Run Deadgrep for TERM under directory ROOT.
+ROOT is normalized to a directory name; TERM defaults to a minibuffer read
+seeded by `cj/deadgrep--initial-term'. Shared tail of the deadgrep commands."
+ (let ((root (file-name-as-directory (expand-file-name root)))
+ (term (or term (read-from-minibuffer "Search: " (cj/deadgrep--initial-term)))))
+ (deadgrep term root)))
+
(use-package deadgrep
:after projectile
:bind
@@ -242,12 +259,6 @@ If no such file exists there, display a message."
:config
(require 'thingatpt)
- (defun cj/deadgrep--initial-term ()
- (cond
- ((use-region-p)
- (buffer-substring-no-properties (region-beginning) (region-end)))
- (t (thing-at-point 'symbol t))))
-
(defun cj/deadgrep-here (&optional term)
"Search with Deadgrep in the most relevant directory at point."
(interactive)
@@ -264,17 +275,14 @@ If no such file exists there, display a message."
(buffer-file-name
(file-name-directory (file-truename buffer-file-name)))
(t default-directory)))
- (root (file-name-as-directory (expand-file-name root)))
- (term (or term (read-from-minibuffer "Search: " (cj/deadgrep--initial-term)))))
- (deadgrep term root)))
+ )
+ (cj/--deadgrep-run root term)))
(defun cj/deadgrep-in-dir (&optional dir term)
"Prompt for a directory, then search there with Deadgrep."
(interactive)
- (let* ((dir (or dir (read-directory-name "Search in directory: " default-directory nil t)))
- (dir (file-name-as-directory (expand-file-name dir)))
- (term (or term (read-from-minibuffer "Search: " (cj/deadgrep--initial-term)))))
- (deadgrep term dir))))
+ (let ((dir (or dir (read-directory-name "Search in directory: " default-directory nil t))))
+ (cj/--deadgrep-run dir term))))
(with-eval-after-load 'dired
(keymap-set dired-mode-map "G" #'cj/deadgrep-here))
diff --git a/tests/test-prog-general--deadgrep.el b/tests/test-prog-general--deadgrep.el
new file mode 100644
index 000000000..21223105d
--- /dev/null
+++ b/tests/test-prog-general--deadgrep.el
@@ -0,0 +1,44 @@
+;;; test-prog-general--deadgrep.el --- Tests for the deadgrep helpers -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; cj/deadgrep--initial-term (region text or symbol at point) and cj/--deadgrep-run
+;; (the normalize-root + read-term + invoke tail shared by cj/deadgrep-here and
+;; cj/deadgrep-in-dir) were lifted out of the deadgrep use-package :config.
+;; deadgrep is mocked at the boundary.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(require 'prog-general)
+
+(ert-deftest test-prg-deadgrep-initial-term-symbol-at-point ()
+ "Normal: with no region, the symbol at point seeds the search."
+ (with-temp-buffer
+ (insert "hello world")
+ (goto-char (point-min))
+ (should (equal (cj/deadgrep--initial-term) "hello"))))
+
+(ert-deftest test-prg-deadgrep-initial-term-region ()
+ "Normal: an active region's text seeds the search."
+ (with-temp-buffer
+ (insert "needle")
+ (transient-mark-mode 1)
+ (set-mark (point-min))
+ (goto-char (point-max))
+ (activate-mark)
+ (should (equal (cj/deadgrep--initial-term) "needle"))))
+
+(ert-deftest test-prg-deadgrep-run-normalizes-root-and-passes-term ()
+ "Normal: ROOT is normalized to a directory and TERM is passed through."
+ (let (got-term got-root)
+ (cl-letf (((symbol-function 'deadgrep)
+ (lambda (term root) (setq got-term term got-root root))))
+ (cj/--deadgrep-run "/tmp/foo" "needle"))
+ (should (equal got-term "needle"))
+ (should (equal got-root "/tmp/foo/"))))
+
+(provide 'test-prog-general--deadgrep)
+;;; test-prog-general--deadgrep.el ends here