Skip to content

Commit

Permalink
Fix mirror transformation error with expand-env
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
notetiene authored and npostavs committed Mar 9, 2017
1 parent 6e9b7f9 commit 0d9afb2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions yasnippet-tests.el
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion yasnippet.el
Expand Up @@ -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))))

Expand Down

0 comments on commit 0d9afb2

Please sign in to comment.