Skip to content

Commit

Permalink
Add search functions which preserve starting context
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuco1 committed Mar 4, 2018
1 parent 3452c21 commit 34de295
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
26 changes: 26 additions & 0 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -4241,6 +4241,32 @@ pairs!"
(sp--with-case-sensitive
(search-forward-regexp regexp bound noerror count)))

(defun sp--search-forward-in-context (regexp &optional bound noerror count)
"Just like `sp--search-forward-regexp' but only accept results in same context.

The context at point is considered the reference context."
(let ((context (sp--get-context))
(re))
(--dotimes (or count 1)
(save-excursion
(while (and (setq re (sp--search-forward-regexp regexp bound noerror))
(not (eq (sp--get-context) context)))))
(when re (goto-char re)))
re))

(defun sp--search-backward-in-context (regexp &optional bound noerror count)
"Just like `sp--search-backward-regexp' but only accept results in same context.

The context at point is considered the reference context."
(let ((context (sp--get-context))
(re))
(--dotimes (or count 1)
(save-excursion
(while (and (setq re (sp--search-backward-regexp regexp bound noerror))
(not (eq (sp--get-context) context))))
(when re (goto-char re))))
re))

(defun sp-get-quoted-string-bounds (&optional point)
"Return the bounds of the string around POINT.

Expand Down
37 changes: 37 additions & 0 deletions test/smartparens-search-fn-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ert-deftest sp-test-search-backward-in-context/code/first-match-same ()
(sp-test-with-temp-elisp-buffer "f|oo bar baz"
(should (equal (sp--search-forward-in-context "bar" nil t) 8))
(sp-buffer-equals "foo bar| baz")))

(ert-deftest sp-test-search-backward-in-context/code/first-match-different ()
(sp-test-with-temp-elisp-buffer "f|oo ; bar baz"
(should (equal (sp--search-forward-in-context "bar" nil t) nil))
(sp-buffer-equals "f|oo ; bar baz")))

(ert-deftest sp-test-search-backward-in-context/code/second-match-same ()
(sp-test-with-temp-elisp-buffer "f|oo ; bar baz
bar baz"
(should (equal (sp--search-forward-in-context "bar" nil t) 18))
(sp-buffer-equals "foo ; bar baz
bar| baz")))

(ert-deftest sp-test-search-backward-in-context/comment/first-match-same ()
(sp-test-with-temp-elisp-buffer ";f|oo bar baz"
(should (equal (sp--search-forward-in-context "bar" nil t) 9))
(sp-buffer-equals ";foo bar| baz")))

(ert-deftest sp-test-search-backward-in-context/comment/first-match-different ()
(sp-test-with-temp-elisp-buffer ";f|oo
bar baz"
(should (equal (sp--search-forward-in-context "bar" nil t) nil))
(sp-buffer-equals ";f|oo
bar baz")))

(ert-deftest sp-test-search-backward-in-context/comment/second-match-same ()
(sp-test-with-temp-elisp-buffer ";f|oo
bar baz
; bar baz"
(should (equal (sp--search-forward-in-context "bar" nil t) 19))
(sp-buffer-equals ";foo
bar baz
; bar| baz")))

0 comments on commit 34de295

Please sign in to comment.