Skip to content

Commit

Permalink
Fix bad boundary matches (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktasia authored Dec 17, 2017
1 parent 3d69721 commit 94275ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
26 changes: 16 additions & 10 deletions dumb-jump.el
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,25 @@ or most optimal searcher."
:type 'string)

(defcustom dumb-jump-ag-word-boundary
"(?![\\w\\?\\*-])"
"(?![a-zA-Z0-9\\?\\*-])"
"`\\b` thinks `-` is a word boundary. When this matters use `\\j` instead and ag will use this value."
:group 'dumb-jump
:type 'string)

(defcustom dumb-jump-rg-word-boundary
"($|[^\\w\\?\\*-])"
"($|[^a-zA-Z0-9\\?\\*-])"
"`\\b` thinks `-` is a word boundary. When this matters use `\\j` instead and rg will use this value."
:group 'dumb-jump
:type 'string)

(defcustom dumb-jump-git-grep-word-boundary
"($|[^\\w\\?\\*-])"
"($|[^a-zA-Z0-9\\?\\*-])"
"`\\b` thinks `-` is a word boundary. When this matters use `\\j` instead and git grep will use this value."
:group 'dumb-jump
:type 'string)

(defcustom dumb-jump-grep-word-boundary
"($|[^\\w\\?\\*-])"
"($|[^a-zA-Z0-9\\?\\*-])"
"`\\b` thinks `-` is a word boundary. When this matters use `\\j` instead and grep will use this value."
:group 'dumb-jump
:type 'string)
Expand Down Expand Up @@ -213,20 +213,26 @@ or most optimal searcher."
:type 'boolean)

(defcustom dumb-jump-find-rules
'((:type "function" :supports ("ag" "grep" "rg" "git-grep") :language "elisp" :regex "\\\((defun|cl-defun)\\s+JJJ\\j"
'((:type "function" :supports ("ag" "grep" "rg" "git-grep") :language "elisp"
:regex "\\\((defun|cl-defun)\\s+JJJ\\j"
;; \\j usage see `dumb-jump-ag-word-boundary`
:tests ("(defun test (blah)" "(defun test\n" "(cl-defun test (blah)" "(cl-defun test\n")
:not ("(defun test-asdf (blah)" "(defun test-blah\n" "(cl-defun test-asdf (blah)" "(cl-defun test-blah\n"))

:not ("(defun test-asdf (blah)" "(defun test-blah\n" "(cl-defun test-asdf (blah)"
"(cl-defun test-blah\n" "(defun tester (blah)" "(defun test? (blah)" "(defun test- (blah)"))

(:type "variable" :supports ("ag" "grep" "rg" "git-grep") :language "elisp"
:regex "\\\(defvar\\b\\s*JJJ\\j" :tests ("(defvar test " "(defvar test\n"))
:regex "\\\(defvar\\b\\s*JJJ\\j"
:tests ("(defvar test " "(defvar test\n")
:not ("(defvar tester" "(defvar test?" "(defvar test-"))

(:type "variable" :supports ("ag" "grep" "rg" "git-grep") :language "elisp"
:regex "\\\(defcustom\\b\\s*JJJ\\j" :tests ("(defcustom test " "(defcustom test\n"))
:regex "\\\(defcustom\\b\\s*JJJ\\j"
:tests ("(defcustom test " "(defcustom test\n")
:not ("(defcustom tester" "(defcustom test?" "(defcustom test-"))

(:type "variable" :supports ("ag" "grep" "rg" "git-grep") :language "elisp"
:regex "\\\(setq\\b\\s*JJJ\\j" :tests ("(setq test 123)") :not ("setq test-blah 123)"))
:regex "\\\(setq\\b\\s*JJJ\\j" :tests ("(setq test 123)")
:not ("setq test-blah 123)" "(setq tester" "(setq test?" "(setq test-"))

(:type "variable" :supports ("ag" "grep" "rg" "git-grep") :language "elisp"
:regex "\\\(JJJ\\s+" :tests ("(let ((test 123)))") :not ("(let ((test-2 123)))"))
Expand Down
38 changes: 19 additions & 19 deletions test/dumb-jump-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@
(let* ((system-type 'darwin)
(regexes (dumb-jump-get-contextual-regexes "elisp" nil 'grep))
(expected-regexes (--map (concat " -e " (shell-quote-argument it))
'("\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])"
"\\(defvar\\b\\s*tester($|[^\\w\\?\\*-])"
"\\(defcustom\\b\\s*tester($|[^\\w\\?\\*-])"
"\\(setq\\b\\s*tester($|[^\\w\\?\\*-])"
'("\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(defvar\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(defcustom\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(setq\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(tester\\s+")))
(expected (concat "LANG=C grep -REn --include \\*.el --include \\*.el.gz" (s-join "" expected-regexes) " .")))
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "elisp" nil)))))
Expand All @@ -93,37 +93,37 @@
(let* ((system-type 'darwin)
(regexes (dumb-jump-get-contextual-regexes "elisp" nil 'gnu-grep))
(expected-regexes (--map (concat " -e " (shell-quote-argument it))
'("\\((defun|cl-defun)[[:space:]]+tester($|[^\\w\\?\\*-])"
"\\(defvar\\b[[:space:]]*tester($|[^\\w\\?\\*-])"
"\\(defcustom\\b[[:space:]]*tester($|[^\\w\\?\\*-])"
"\\(setq\\b[[:space:]]*tester($|[^\\w\\?\\*-])"
'("\\((defun|cl-defun)[[:space:]]+tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(defvar\\b[[:space:]]*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(defcustom\\b[[:space:]]*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(setq\\b[[:space:]]*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(tester[[:space:]]+")))
(expected (concat "LANG=C grep -rEn" (s-join "" expected-regexes) " .")))
(should (string= expected (dumb-jump-generate-gnu-grep-command "tester" "blah.el" "." regexes "elisp" nil)))))

(ert-deftest dumb-jump-generate-ag-command-no-ctx-test ()
(let* ((regexes (dumb-jump-get-contextual-regexes "elisp" nil 'ag))
(expected-regexes "\\((defun|cl-defun)\\s+tester(?![\\w\\?\\*-])|\\(defvar\\b\\s*tester(?![\\w\\?\\*-])|\\(defcustom\\b\\s*tester(?![\\w\\?\\*-])|\\(setq\\b\\s*tester(?![\\w\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester(?![\\w\\?\\*-])\\s*\\)?")
(expected-regexes "\\((defun|cl-defun)\\s+tester(?![a-zA-Z0-9\\?\\*-])|\\(defvar\\b\\s*tester(?![a-zA-Z0-9\\?\\*-])|\\(defcustom\\b\\s*tester(?![a-zA-Z0-9\\?\\*-])|\\(setq\\b\\s*tester(?![a-zA-Z0-9\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester(?![a-zA-Z0-9\\?\\*-])\\s*\\)?")
(expected (concat "ag --nocolor --nogroup --elisp " (shell-quote-argument expected-regexes) " .")))
(should (string= expected (dumb-jump-generate-ag-command "tester" "blah.el" "." regexes "elisp" nil)))))

(ert-deftest dumb-jump-generate-rg-command-no-ctx-test ()
(let* ((regexes (dumb-jump-get-contextual-regexes "elisp" nil 'rg))
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])|\\(defvar\\b\\s*tester($|[^\\w\\?\\*-])|\\(defcustom\\b\\s*tester($|[^\\w\\?\\*-])|\\(setq\\b\\s*tester($|[^\\w\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester($|[^\\w\\?\\*-])\\s*\\)?")
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])|\\(defvar\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(defcustom\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(setq\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester($|[^a-zA-Z0-9\\?\\*-])\\s*\\)?")
(expected (concat "rg --color never --no-heading --line-number --type elisp " (shell-quote-argument expected-regexes) " .")))
(should (string= expected (dumb-jump-generate-rg-command "tester" "blah.el" "." regexes "elisp" nil)))))

(ert-deftest dumb-jump-generate-git-grep-command-no-ctx-test ()
(let* ((regexes (dumb-jump-get-contextual-regexes "elisp" nil 'git-grep))
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])|\\(defvar\\b\\s*tester($|[^\\w\\?\\*-])|\\(defcustom\\b\\s*tester($|[^\\w\\?\\*-])|\\(setq\\b\\s*tester($|[^\\w\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester($|[^\\w\\?\\*-])\\s*\\)?")
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])|\\(defvar\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(defcustom\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(setq\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester($|[^a-zA-Z0-9\\?\\*-])\\s*\\)?")
(excludes '("one" "two" "three"))
(expected (concat "git grep --color=never --line-number --untracked -E " (shell-quote-argument expected-regexes) " -- ./\\*.el ./\\*.el.gz \\:\\(exclude\\)one \\:\\(exclude\\)two \\:\\(exclude\\)three")))
(should (string= expected (dumb-jump-generate-git-grep-command "tester" "blah.el" "." regexes "elisp" excludes)))))

(ert-deftest dumb-jump-generate-git-grep-command-not-search-untracked-test ()
(let* ((dumb-jump-git-grep-search-untracked nil)
(regexes (dumb-jump-get-contextual-regexes "elisp" nil 'git-grep))
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])|\\(defvar\\b\\s*tester($|[^\\w\\?\\*-])|\\(defcustom\\b\\s*tester($|[^\\w\\?\\*-])|\\(setq\\b\\s*tester($|[^\\w\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester($|[^\\w\\?\\*-])\\s*\\)?")
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])|\\(defvar\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(defcustom\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(setq\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])|\\(tester\\s+|\\((defun|cl-defun)\\s*.+\\(?\\s*tester($|[^a-zA-Z0-9\\?\\*-])\\s*\\)?")
(excludes '("one" "two" "three"))
(expected (concat "git grep --color=never --line-number -E " (shell-quote-argument expected-regexes) " -- ./\\*.el ./\\*.el.gz \\:\\(exclude\\)one \\:\\(exclude\\)two \\:\\(exclude\\)three")))
(should (string= expected (dumb-jump-generate-git-grep-command "tester" "blah.el" "." regexes "elisp" excludes)))))
Expand All @@ -132,7 +132,7 @@
(let* ((system-type 'darwin)
(dumb-jump-functions-only t)
(regexes (dumb-jump-get-contextual-regexes "elisp" nil 'grep))
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])")
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])")
(expected (concat "LANG=C grep -REn -e " (shell-quote-argument expected-regexes) " ."))
(zexpected (concat "LANG=C zgrep -REn -e " (shell-quote-argument expected-regexes) " .")))
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "" nil)))
Expand All @@ -143,7 +143,7 @@
(ctx-type (dumb-jump-get-ctx-type-by-language "elisp" '(:left "(" :right nil)))
(dumb-jump-ignore-context nil) ;; overriding the default
(regexes (dumb-jump-get-contextual-regexes "elisp" ctx-type 'grep))
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])")
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])")
(expected (concat "LANG=C grep -REn -e " (shell-quote-argument expected-regexes) " .")))
;; the point context being passed should match a "function" type so only the one command
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "" nil)))))
Expand All @@ -154,7 +154,7 @@
(ctx-type (dumb-jump-get-ctx-type-by-language "elisp" '(:left "(" :right nil)))
(dumb-jump-ignore-context nil) ;; overriding the default
(regexes (dumb-jump-get-contextual-regexes "elisp" ctx-type 'grep))
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])")
(expected-regexes "\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])")
(expected (concat "grep -REn -e " (shell-quote-argument expected-regexes) " .")))
(should (string= expected (dumb-jump-generate-grep-command "tester" "blah.el" "." regexes "" nil))))))

Expand All @@ -164,10 +164,10 @@
(dumb-jump-ignore-context t)
(regexes (dumb-jump-get-contextual-regexes "elisp" ctx-type nil))
(expected-regexes (--map (concat " -e " (shell-quote-argument it))
'("\\((defun|cl-defun)\\s+tester($|[^\\w\\?\\*-])"
"\\(defvar\\b\\s*tester($|[^\\w\\?\\*-])"
"\\(defcustom\\b\\s*tester($|[^\\w\\?\\*-])"
"\\(setq\\b\\s*tester($|[^\\w\\?\\*-])"
'("\\((defun|cl-defun)\\s+tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(defvar\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(defcustom\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(setq\\b\\s*tester($|[^a-zA-Z0-9\\?\\*-])"
"\\(tester\\s+")))
(expected (concat "LANG=C grep -REn" (s-join "" expected-regexes) " .")))

Expand Down

0 comments on commit 94275ba

Please sign in to comment.