From e0ba1ba1d3bf68f44e0aa8e315edc64c5bf423a9 Mon Sep 17 00:00:00 2001 From: ben lamothe Date: Mon, 25 Dec 2017 00:44:35 -0500 Subject: [PATCH 1/3] add rg-dwim-regexp --- rg.el | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/rg.el b/rg.el index c14be78..92942f8 100644 --- a/rg.el +++ b/rg.el @@ -1007,6 +1007,13 @@ prefix is not supplied `rg-keymap-prefix' is used." (message "Global key bindings for `rg' enabled with prefix: %s" (edmacro-format-keys prefix)))) +;;;###autoload +(defun rg-run-in-project (regexp files) + (let ((root (rg-project-root buffer-file-name))) + (if root + (rg-run regexp files root) + (signal 'user-error '("No project root found"))))) + ;;;###autoload (defun rg-project (regexp files) "Run ripgrep in current project searching for REGEXP in FILES. @@ -1018,10 +1025,18 @@ version control system." (let* ((regexp (rg-read-pattern)) (files (rg-read-files regexp))) (list regexp files)))) - (let ((root (rg-project-root buffer-file-name))) - (if root - (rg-run regexp files root) - (signal 'user-error '("No project root found"))))) + (rg-run-in-project regexp files)) + +;;;###autoload +(defun rg-dwim-regexp (regexp) + "Run ripgrep in current project searching for REGEXP in files +like the current file" + (interactive + (progn + (let* ((regexp (rg-read-pattern))) + (list regexp)))) + (let ((files (car (rg-default-alias)))) + (rg-run-in-project regexp files))) ;;;###autoload (defun rg-dwim (&optional curdir) From df18cec5660485a451839e447453289c79bf6449 Mon Sep 17 00:00:00 2001 From: ben lamothe Date: Mon, 25 Dec 2017 15:51:39 -0500 Subject: [PATCH 2/3] add docstrings --- rg.el | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rg.el b/rg.el index 92942f8..65399d5 100644 --- a/rg.el +++ b/rg.el @@ -1007,8 +1007,8 @@ prefix is not supplied `rg-keymap-prefix' is used." (message "Global key bindings for `rg' enabled with prefix: %s" (edmacro-format-keys prefix)))) -;;;###autoload (defun rg-run-in-project (regexp files) + "Search for `REGEXP' in files of type `FILES' starting at the rg-project-root." (let ((root (rg-project-root buffer-file-name))) (if root (rg-run regexp files root) @@ -1029,8 +1029,7 @@ version control system." ;;;###autoload (defun rg-dwim-regexp (regexp) - "Run ripgrep in current project searching for REGEXP in files -like the current file" + "Run ripgrep in current project searching for REGEXP in files like the current file." (interactive (progn (let* ((regexp (rg-read-pattern))) From 9ca4eda6dac82f327a48e190b172f1d768c106a0 Mon Sep 17 00:00:00 2001 From: ben lamothe Date: Mon, 25 Dec 2017 16:23:00 -0500 Subject: [PATCH 3/3] add test for rg-dwim-regexp --- test/rg.el-test.el | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/rg.el-test.el b/test/rg.el-test.el index 7bb22ec..d04acab 100644 --- a/test/rg.el-test.el +++ b/test/rg.el-test.el @@ -651,6 +651,26 @@ and ungrouped otherwise." (rg-dwim 'curdir) (should (equal (expand-file-name called-dir) (expand-file-name default-directory))))) +(ert-deftest rg-integration/dwim-regexp-search () + "Test `rg-dwim-regexp'." + (cl-letf ((called-pattern nil) + (called-files nil) + (called-dir nil) + (called-literal nil) + (project-dir (expand-file-name default-directory)) + ((symbol-function #'rg-run) + (lambda (pattern files dir &optional literal _) + (setq called-pattern pattern) + (setq called-files files) + (setq called-dir dir) + (setq called-literal literal)))) + (find-file "test/data/foo.el") + (rg-dwim-regexp "hello") + (should (equal called-pattern "hello")) + (should (equal called-files "elisp")) + (should (equal (expand-file-name called-dir) project-dir)) + (should (eq called-literal nil)))) + (ert-deftest rg-integration/project-search () "Test `rg-project'." (cl-letf ((called-pattern nil)