Skip to content

Commit

Permalink
Allow lambdas in rg-custom-type-aliases
Browse files Browse the repository at this point in the history
Now in addition to (string . string) cons cells you can add lambda(s)
that return (string . string) cons cells. The deferred evaluation lets
you build dynamic types based on e.g. the current project, current
mode, current file path, etc.
  • Loading branch information
Muir Manders committed May 3, 2018
1 parent 4eaf35f commit c804d88
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
18 changes: 14 additions & 4 deletions rg.el
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@
(defcustom rg-custom-type-aliases
'(("gn" . "*.gn *.gni")
("gyp" . "*.gyp *.gypi"))
"Alist of file type aliases that are added to the 'rg' built in aliases."
:type '(alist :key-type string :value-type string)
"A list of file type aliases that are added to the 'rg' built in aliases.
Each list element may be a (string . string) cons containing the name of the
type alias and the file patterns, or a lambda returning a similar cons cell."
:type '(repeat (choice (cons string string) function))
:group 'rg)

(defcustom rg-command-line-flags nil
Expand Down Expand Up @@ -195,7 +197,7 @@ These are not produced by 'rg --type-list' but we need them anyway.")
(concat "--type-add "
(shell-quote-argument (concat name ":" glob))))
(split-string globs) " ")))
rg-custom-type-aliases))
(rg-get-custom-type-aliases)))

(defun rg-is-custom-file-pattern (files)
"Return non nil if FILES is a custom file pattern."
Expand Down Expand Up @@ -249,13 +251,21 @@ are command line flags to use for the search."
type-list)))


(defun rg-get-custom-type-aliases ()
"Get alist of custom type aliases.
Any lambda elements will be evaluated, and nil results will be
filtered out."
(delq nil (mapcar
(lambda (ct) (if (functionp ct) (funcall ct) ct))
rg-custom-type-aliases)))

(defun rg-get-type-aliases (&optional skip-internal)
"Return supported type aliases.
If SKIP-INTERNAL is non nil the `rg-internal-type-aliases' will be
excluded."
(unless rg-builtin-type-aliases
(setq rg-builtin-type-aliases (rg-list-builtin-type-aliases)))
(append rg-custom-type-aliases rg-builtin-type-aliases
(append (rg-get-custom-type-aliases) rg-builtin-type-aliases
(unless skip-internal rg-internal-type-aliases)))

(defun rg-default-alias ()
Expand Down
17 changes: 15 additions & 2 deletions test/rg.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ Test `:flags' directive."
(should (= 0 (s-count-matches "bar.baz.*hello" bufstr)))))))

(ert-deftest rg-integration-test/search-alias-custom ()
"Test that aliases defined in `rg-custom-type-aliases' works if explicitly selected."
"Test that aliases defined in `rg-custom-type-aliases' work if explicitly selected."
:tags '(need-rg)
(let ((rg-ignore-case 'force)
(rg-custom-type-aliases '(("test" . "*.baz"))))
Expand All @@ -476,7 +476,7 @@ Test `:flags' directive."
(should (= 3 (s-count-matches "bar.baz.*hello" bufstr)))))))

(ert-deftest rg-integration-test/search-alias-all-custom ()
"Test that aliases defined in `rg-custom-type-ailiases' works if
"Test that aliases defined in `rg-custom-type-ailiases' work if
implicitly selected via '--type all'."
:tags '(need-rg)
(let ((rg-ignore-case 'force)
Expand All @@ -489,6 +489,19 @@ Test `:flags' directive."
(should (= 3 (s-count-matches "foo.baz.*hello" bufstr)))
(should (= 3 (s-count-matches "bar.baz.*hello" bufstr)))))))

(ert-deftest rg-integration-test/search-alias-custom-lambda ()
"Test that aliases defined via lambdas in `rg-custom-type-aliases' work."
:tags '(need-rg)
(let ((rg-ignore-case 'force)
(rg-custom-type-aliases '((lambda () '("test" . "*.baz")))))
(rg-run "hello" "test" (concat default-directory "test/data"))
(rg-with-current-result
(let ((bufstr (buffer-substring-no-properties (point-min) (point-max))))
(should (= 0 (s-count-matches "foo.el.*hello" bufstr)))
(should (= 0 (s-count-matches "bar.el.*hello" bufstr)))
(should (= 3 (s-count-matches "foo.baz.*hello" bufstr)))
(should (= 3 (s-count-matches "bar.baz.*hello" bufstr)))))))

(ert-deftest rg-integration-test/search-no-alias()
"Test that custom file pattern that is not an alias works."
:tags '(need-rg)
Expand Down

0 comments on commit c804d88

Please sign in to comment.