Skip to content

Commit

Permalink
Merge 9ca4eda into 6898409
Browse files Browse the repository at this point in the history
  • Loading branch information
zonotope committed Dec 25, 2017
2 parents 6898409 + 9ca4eda commit 7339b24
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
22 changes: 18 additions & 4 deletions rg.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))))

(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)
(signal 'user-error '("No project root found")))))

;;;###autoload
(defun rg-project (regexp files)
"Run ripgrep in current project searching for REGEXP in FILES.
Expand All @@ -1018,10 +1025,17 @@ 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)
Expand Down
20 changes: 20 additions & 0 deletions test/rg.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7339b24

Please sign in to comment.