From c804d887fa63fba57321a53914a8615e07db171a Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Thu, 3 May 2018 18:58:33 +0900 Subject: [PATCH] Allow lambdas in rg-custom-type-aliases 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. --- rg.el | 18 ++++++++++++++---- test/rg.el-test.el | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/rg.el b/rg.el index 9a7e9f8..20b4e45 100644 --- a/rg.el +++ b/rg.el @@ -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 @@ -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." @@ -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 () diff --git a/test/rg.el-test.el b/test/rg.el-test.el index 5087969..a3802fc 100644 --- a/test/rg.el-test.el +++ b/test/rg.el-test.el @@ -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")))) @@ -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) @@ -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)