Skip to content

Commit

Permalink
[Fix #878] Apply bounds to textmode search to not spill outside context
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuco1 committed Oct 28, 2018
1 parent 3eecb2d commit 2c41238
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -4810,6 +4810,8 @@ to non-stringlike matching and we can use a simple
counting (stack) algorithm."
(save-excursion
(let ((restart-from (point))
(bounds (or (sp-get-comment-bounds)
(cons (point-min) (point-max))))
hit re)
(while (not hit)
(goto-char restart-from)
Expand All @@ -4818,9 +4820,11 @@ counting (stack) algorithm."
(if back (forward-char) (backward-char)))
(let* ((delimiters (-map 'car (sp--get-allowed-stringlike-list)))
(needle (sp--textmode-stringlike-regexp delimiters))
(search-fn-f (if (not back) 'sp--search-forward-regexp 'sp--search-backward-regexp)))
(-if-let ((delim type) (sp--find-next-textmode-stringlike-delimiter needle search-fn-f))
(search-fn-f (if (not back) 'sp--search-forward-regexp 'sp--search-backward-regexp))
(limit-f (if (not back) (cdr bounds) (car bounds))))
(-if-let ((delim type) (sp--find-next-textmode-stringlike-delimiter needle search-fn-f limit-f))
(let ((search-fn (if (eq type :open) 'sp--search-forward-regexp 'sp--search-backward-regexp))
(limit (if (eq type :open) (cdr bounds) (car bounds)))
(needle (sp--textmode-stringlike-regexp (list delim) (if (eq type :open) :close :open))))
(setq restart-from (point))
;; this adjustments are made because elisp regexp
Expand All @@ -4832,7 +4836,7 @@ counting (stack) algorithm."
(when (and back (eq type :close)) (forward-char (1+ (length delim))))
(when (and back (eq type :open) (not (bobp))) (forward-char 1)))
(let ((other-end (point)))
(when (sp--find-next-textmode-stringlike-delimiter needle search-fn)
(when (sp--find-next-textmode-stringlike-delimiter needle search-fn limit)
;; Beware, we also need to test the beg/end of
;; buffer, because we have that variant in the
;; regexp. In that case the match does not
Expand Down
14 changes: 14 additions & 0 deletions test/smartparens-get-stringlike-expression-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,17 @@
(sp-test-get-textmode-stringlike-expression-in-org "/bar/ asd ~a|sd~" '(:beg 11 :end 16 :op "~" :cl "~" :prefix "" :suffix ""))
(sp-test-get-textmode-stringlike-expression-in-org "|//" '(:beg 1 :end 3 :op "/" :cl "/" :prefix "" :suffix ""))
(sp-test-get-textmode-stringlike-expression-in-org "//|" '(:beg 1 :end 3 :op "/" :cl "/" :prefix "" :suffix "") t)))

(defun sp-test-get-textmode-stringlike-expression-in-html (initial result &optional back)
(sp-test-with-temp-buffer initial
(html-mode)
(should (equal (sp-get-textmode-stringlike-expression back) result))))

(ert-deftest sp-test-get-textmode-stringlike-expression-html nil
(let ((sp-pairs '((t . ((:open "'" :close "'" :actions (insert wrap autoskip navigate)))))))
(sp-test-get-textmode-stringlike-expression-in-html
"<style> 'foo.png' </style> <!-- foodoett'| -->" nil t)
(sp-test-get-textmode-stringlike-expression-in-html
"<style> 'foo.png'| </style> <!-- foodoett' -->" '(:beg 9 :end 18 :op "'" :cl "'" :prefix "" :suffix "") t)
(sp-test-get-textmode-stringlike-expression-in-html
"<style> |'foo.png' </style> <!-- foodoett' -->" '(:beg 9 :end 18 :op "'" :cl "'" :prefix "" :suffix ""))))

0 comments on commit 2c41238

Please sign in to comment.