Fix mirror transformation error with expand-env

* yasnippet.el (yas-expand-snippet): Use the snippet's bindings when
moving to the first field.  This prevents getting an error when using
a variable defined in expand-env.
* yasnippet-tests.el (snippet-mirror-bindings): New test, check if
mirror transformations have access to variables defined in the snippet
environment.
This commit is contained in:
Etienne 2017-03-09 13:50:06 -05:00 committed by Noam Postavsky
parent 6e9b7f9432
commit 0d9afb2c69
2 changed files with 22 additions and 1 deletions

View File

@ -614,6 +614,26 @@ foobaz\n"))))
(should (eq 'global (nth 0 yas--ran-exit-hook)))
(should (eq 'local (nth 1 yas--ran-exit-hook))))))))
(ert-deftest snippet-mirror-bindings ()
"Check that variables defined with the expand-env field are
accessible from mirror transformations."
(with-temp-buffer
(yas-saving-variables
(let ((yas-triggers-in-field t)
(yas-good-grace nil))
(yas-with-snippet-dirs
'((".emacs.d/snippets"
("emacs-lisp-mode"
("baz" . "\
# expand-env: ((func #'upcase))
# --
hello ${1:$(when (stringp yas-text) (funcall func yas-text))} foo${1:$$(concat \"baz\")}$0"))))
(yas-reload-all)
(emacs-lisp-mode)
(yas-minor-mode +1)
(insert "baz")
(ert-simulate-command '(yas-expand))
(should (string= (yas--buffer-contents) "hello BAZ foobaz\n")))))))
(defvar yas--barbaz)
(defvar yas--foobarbaz)

View File

@ -3731,7 +3731,8 @@ considered when expanding the snippet."
(let ((first-field (car (yas--snippet-fields snippet))))
(when first-field
(sit-for 0) ;; fix issue 125
(yas--move-to-field snippet first-field)))
(yas--letenv (yas--snippet-expand-env snippet)
(yas--move-to-field snippet first-field))))
(yas--message 4 "snippet expanded.")
t))))