Skip to content

Commit

Permalink
fix(yas): when expanding a snippet with hippie, do not add extra parens
Browse files Browse the repository at this point in the history
Fixes #911
  • Loading branch information
Fuco1 committed Mar 30, 2024
1 parent 38afe79 commit 0bbd854
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
7 changes: 5 additions & 2 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -9756,12 +9756,15 @@ has been created."
(defadvice company--insert-candidate (after sp-company--insert-candidate activate)
"If `smartparens-mode' is active, we check if the completed string
has a pair definition. If so, we insert the closing pair."
(when (and ad-return-value smartparens-mode)
(when (and ad-return-value
smartparens-mode
(not (sp-get-enclosing-sexp)))
(sp-insert-pair))
ad-return-value)

(defadvice hippie-expand (after sp-auto-complete-advice activate)
(when smartparens-mode
(when (and smartparens-mode
(not (sp-get-enclosing-sexp)))
(sp-insert-pair)))

(defvar sp--mc/cursor-specific-vars
Expand Down
51 changes: 51 additions & 0 deletions test/smartparens-advice-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
;;; Tests for advices and compat with external packages

(require 'yasnippet)
(require 'hippie-exp)

;; These macros are copied from yasnippet-tests.el
(progn
(defmacro yas-with-snippet-dirs (dirs &rest body)
(declare (indent defun) (debug t))
`(yas-call-with-snippet-dirs
,dirs #'(lambda () ,@body)))

(defun yas-make-file-or-dirs (ass)
(let ((file-or-dir-name (car ass))
(content (cdr ass)))
(cond ((listp content)
(make-directory file-or-dir-name 'parents)
(let ((default-directory (concat default-directory "/" file-or-dir-name)))
(mapc #'yas-make-file-or-dirs content)))
((stringp content)
(with-temp-buffer
(insert content)
(write-region nil nil file-or-dir-name nil 'nomessage)))
(t
(message "[yas] oops don't know this content")))))

(defun yas-call-with-snippet-dirs (dirs fn)
(let* ((default-directory (make-temp-file "yasnippet-fixture" t))
(yas-snippet-dirs (mapcar (lambda (d) (expand-file-name (car d))) dirs)))
(with-temp-message ""
(unwind-protect
(progn
(mapc #'yas-make-file-or-dirs dirs)
(funcall fn))
(when (>= emacs-major-version 24)
(delete-directory default-directory 'recursive)))))))

(ert-deftest sp-test-advice--hippie-no-pairing-if-sexp-already-exists ()
"Test that after hippie expand expands a yasnippet template it
won't add an extra closing paren if the snippet already provides
it."
(yas-with-snippet-dirs
'((".emacs.d/snippets"
("emacs-lisp-mode" ("defun" . "(defun hello ($0))"))))
(let ((hippie-expand-try-functions-list
'(yas-hippie-try-expand)))
(sp-test-with-temp-elisp-buffer "defun|"
(yas-reload-all)
(yas-minor-mode 1)
(call-interactively 'hippie-expand)
(sp-buffer-equals "(defun hello (|))")))))

0 comments on commit 0bbd854

Please sign in to comment.