Skip to content

Commit

Permalink
feat: sp--get-prefix/suffix consistently returns a string
Browse files Browse the repository at this point in the history
Previously in case of a pair-specific prefix/suffix we could return
nil if there was no match.
  • Loading branch information
Fuco1 committed Oct 7, 2020
1 parent 465cd36 commit 927f2b0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -5232,8 +5232,9 @@ is used to retrieve the prefix instead of the global setting."
(let* ((pref (sp-get-pair op :prefix))
(prefix
(if pref
(when (sp--looking-back pref sp-max-prefix-length)
(match-string-no-properties 0))
(if (sp--looking-back pref sp-max-prefix-length)
(match-string-no-properties 0)
"")
(-if-let (mmode-prefix (cdr (assoc major-mode sp-sexp-prefix)))
(cond
((and (eq (car mmode-prefix) 'regexp)
Expand Down Expand Up @@ -5274,8 +5275,9 @@ is used to retrieve the suffix instead of the global setting."
(let* ((suff (sp-get-pair op :suffix))
(suffix
(if suff
(when (sp--looking-at suff)
(match-string-no-properties 0))
(if (sp--looking-at suff)
(match-string-no-properties 0)
"")
(-if-let (mmode-suffix (cdr (assoc major-mode sp-sexp-suffix)))
(cond
((and (eq (car mmode-suffix) 'regexp)
Expand Down
10 changes: 10 additions & 0 deletions test/smartparens-get-prefix-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@
(sp--update-local-pairs)
(sp-skip-backward-to-symbol)
(sp-buffer-equals "asd (aaa|(abc))"))))))

(ert-deftest sp-test-get-prefix-always-return-string ()
"Previously in case of a pair-specific prefix we could return
nil if there was no match."
(let ((sp-sexp-prefix '((emacs-lisp-mode regexp "\\(?:aaa\\)"))))
(sp-test-with-temp-elisp-buffer "asd (aaa|(abc))"
(let ((sp-pairs '((t (:open "(" :close ")" :actions (insert wrap autoskip navigate)))
(emacs-lisp-mode (:open "(" :close ")" :actions (insert wrap autoskip navigate) :prefix "x")))))
(sp--update-local-pairs)
(should (equal (sp--get-prefix (point) "(") ""))))))
10 changes: 10 additions & 0 deletions test/smartparens-get-suffix-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@
(sp-test-with-temp-buffer "«asdasd»|«asdasd»"
(text-mode)
(should (equal (sp--get-suffix (point) "«") "")))))

(ert-deftest sp-test-get-suffix-always-return-string ()
"Previously in case of a pair-specific suffix we could return
nil if there was no match."
(let ((sp-sexp-suffix '((emacs-lisp-mode regexp "\\(?:aaa\\)"))))
(sp-test-with-temp-elisp-buffer "((abc)|aaa) asd"
(let ((sp-pairs '((t (:open "(" :close ")" :actions (insert wrap autoskip navigate)))
(emacs-lisp-mode (:open "(" :close ")" :actions (insert wrap autoskip navigate) :suffix "x")))))
(sp--update-local-pairs)
(should (equal (sp--get-suffix (point) "(") ""))))))

0 comments on commit 927f2b0

Please sign in to comment.