Skip to content

Commit

Permalink
add rg-dwim-current-file to narrow rg-dwim even more.
Browse files Browse the repository at this point in the history
Following the trend of c-u meaning "more focused", this commit
implements c-u c-u rg-dwim as even narrower search, searching just in
the current file.
  • Loading branch information
kidd authored and dajva committed Feb 5, 2019
1 parent f09df88 commit 77670a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
30 changes: 23 additions & 7 deletions rg.el
Original file line number Diff line number Diff line change
Expand Up @@ -707,17 +707,33 @@ under the current directory."
:files current
:dir current)

;;;###autoload (autoload 'rg-dwim-current-file "rg.el" "" t)
(rg-define-search rg-dwim-current-file
"Search for thing at point in files matching the current file
name (as a pattern) under the current directory."
:query point
:format literal
:files (file-name-nondirectory (buffer-file-name))
:dir current)

;;;###autoload
(defun rg-dwim (&optional curdir)
"Run ripgrep without user interaction figuring out the intention by magic(!).
The default magic searches for thing at
point in files matching current file under project root
directory. With \\[universal-argument] prefix (CURDIR), search is
done in current dir instead of project root."
The default magic searches for thing at point in files matching
current file under project root directory.
With \\[universal-argument] prefix (CURDIR), search is done in
current dir instead of project root.
With repeated \\[universal-argument] prefix, search is done in
the current dir and using the current variable `buffer-file-name'
as a pattern. Subdirectories are still searched, so different
files with the same name pattern still will be searched."
(interactive "P")
(if curdir
(rg-dwim-current-dir)
(rg-dwim-project-dir)))
(cond
((eq 4 (and (consp curdir) (car curdir))) (rg-dwim-current-dir))
((eq 16 (and (consp curdir) (car curdir))) (rg-dwim-current-file))
(t (rg-dwim-project-dir))))

;;;###autoload (autoload 'rg-literal "rg.el" "" t)
(rg-define-search rg-literal
Expand Down
6 changes: 4 additions & 2 deletions test/rg.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,10 @@ and ungrouped otherwise."
(should (equal called-files "elisp"))
(should (equal (expand-file-name called-dir) project-dir))
(should-not (eq called-literal nil))
(rg-dwim 'curdir)
(should (equal (expand-file-name called-dir) (expand-file-name default-directory)))))
(rg-dwim '(4))
(should (equal (expand-file-name called-dir) (expand-file-name default-directory)))
(rg-dwim '(16))
(should (equal called-files "foo.el"))))

(ert-deftest rg-integration/project-search ()
"Test `rg-project'."
Expand Down

0 comments on commit 77670a4

Please sign in to comment.