branch: master
commit eeb05f0910c13320738b21dfbe3b24af94f357cb
Author: Noam Postavsky <[email protected]>
Commit: Noam Postavsky <[email protected]>
Handle field adjust of pending active fields
* yasnippet.el (yas--on-field-overlay-modification): Call
yas--advance-end-maybe on all pending active snippets in case of
stacked expansion.
(yas-next-field): Get current snippet according to
active field bounds.
* yasnippet-tests.el (nested-snippet-expansion-4): New test.
---
yasnippet-tests.el | 20 ++++++++++++++++++++
yasnippet.el | 15 ++++++++++++---
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/yasnippet-tests.el b/yasnippet-tests.el
index f96bf4c..95606f2 100644
--- a/yasnippet-tests.el
+++ b/yasnippet-tests.el
@@ -1066,6 +1066,26 @@ hello ${1:$(when (stringp yas-text) (funcall func
yas-text))} foo${1:$$(concat \
(ert-simulate-command '(yas-next-field-or-maybe-expand))
(should (string= (buffer-string) "\\sqrt[3]{\\sqrt[5]{2}}")))))
+(ert-deftest nested-snippet-expansion-4 ()
+ "See Github #959."
+ (let ((yas-triggers-in-field t))
+ (yas-with-snippet-dirs
+ '((".emacs.d/snippets"
+ ("text-mode"
+ ("ch" . "<-${1:ch}"))))
+ (yas-reload-all)
+ (text-mode)
+ (yas-minor-mode +1)
+ (yas-expand-snippet "ch$0\n")
+ (ert-simulate-command '(yas-expand))
+ (ert-simulate-command '(forward-char 2))
+ (ert-simulate-command '(yas-expand))
+ (yas-mock-insert "abc")
+ (ert-simulate-command '(yas-next-field-or-maybe-expand))
+ (yas-mock-insert "def")
+ (ert-simulate-command '(yas-next-field-or-maybe-expand))
+ (should (string= (buffer-string) "<-<-abcdef\n")))))
+
;;; Loading
;;;
diff --git a/yasnippet.el b/yasnippet.el
index 5fa72c3..215a7c3 100644
--- a/yasnippet.el
+++ b/yasnippet.el
@@ -3326,8 +3326,9 @@ Otherwise delegate to `yas-next-field'."
If there's none, exit the snippet."
(interactive)
(unless arg (setq arg 1))
- (let* ((snippet (car (yas-active-snippets)))
- (active-field (overlay-get yas--active-field-overlay 'yas--field))
+ (let* ((active-field (overlay-get yas--active-field-overlay 'yas--field))
+ (snippet (car (yas-active-snippets (yas--field-start active-field)
+ (yas--field-end active-field))))
(target-field (yas--find-next-field arg snippet active-field)))
(yas--letenv (yas--snippet-expand-env snippet)
;; Apply transform to active field.
@@ -3745,7 +3746,15 @@ field start. This hook does nothing if an undo is in
progress."
;; We delete text starting from the END of insertion.
(yas--skip-and-clear field end))
(setf (yas--field-modified-p field) t)
- (yas--advance-end-maybe field (overlay-end overlay))
+ ;; Adjust any pending active fields in case of stacked
+ ;; expansion.
+ (let ((pfield field)
+ (psnippets (yas-active-snippets beg end)))
+ (while (and pfield psnippets)
+ (let ((psnippet (pop psnippets)))
+ (cl-assert (memq pfield (yas--snippet-fields psnippet)))
+ (yas--advance-end-maybe pfield (overlay-end overlay))
+ (setq pfield (yas--snippet-previous-active-field
psnippet)))))
(save-excursion
(yas--field-update-display field))
(yas--update-mirrors snippet)))